blob: 7351fd95243094e49f1e5b5570b941b843053231 [file] [log] [blame]
henriquea2de4102014-02-07 14:12:56 +01001/*
Roger Meierbb267d42011-07-30 15:10:42 +00002 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Henrique Mendonça095ddb72013-09-20 19:38:03 +020019 /* jshint -W100 */
Roger Meierbb267d42011-07-30 15:10:42 +000020
21/*
henriquea2de4102014-02-07 14:12:56 +010022 * JavaScript test suite for ThriftTest.thrift. These tests
23 * will run against Normal (-gen js) and jQuery (-gen js:jquery)
24 * Apache Thrift interfaces.
25 *
26 * Synchronous blocking calls should be identical in both
27 * Normal and jQuery interfaces. All synchronous tests belong
28 * here.
29 *
30 * Asynchronous sucess callbacks passed as the last parameter
31 * of an RPC call should be identical in both Normal and jQuery
32 * interfaces. Async success tests belong here.
33 *
34 * Asynchronous exception processing is different in Normal
35 * and jQuery interfaces. Such tests belong in the test-nojq.js
36 * or test-jq.js files respectively. jQuery specific XHR object
37 * tests also belong in test-jq.js. Do not create any jQuery
38 * dependencies in this file or in test-nojq.js
39 *
40 * To compile client code for this test use:
41 * $ thrift -gen js ThriftTest.thrift
42 * -- or --
43 * $ thrift -gen js:jquery ThriftTest.thrift
44 *
45 * See also:
46 * ++ test-nojq.js for "-gen js" only tests
47 * ++ test-jq.js for "-gen js:jquery" only tests
Roger Meierbb267d42011-07-30 15:10:42 +000048 */
49
50var transport = new Thrift.Transport("/service");
51var protocol = new Thrift.Protocol(transport);
52var client = new ThriftTest.ThriftTestClient(protocol);
53
54// all Languages in UTF-8
55var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, Bahasa Melayu, مازِرونی, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, Occitan, Иронау, Papiamentu, Deitsch, Norfuk / Pitkern, Polski, پنجابی, پښتو, Português, Runa Simi, Rumantsch, Romani, Română, Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語";
Roger Meier4f8a5232011-09-09 08:17:02 +000056
57function checkRecursively(map1, map2) {
58 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
59 if (!map1 || typeof map1 !== 'object') {
Roger Meierb661e832012-04-15 15:58:43 +000060 equal(map1, map2);
Roger Meier4f8a5232011-09-09 08:17:02 +000061 } else {
62 for (var key in map1) {
63 checkRecursively(map1[key], map2[key]);
64 }
65 }
66 }
67}
Roger Meierbb267d42011-07-30 15:10:42 +000068
69module("Base Types");
70
71 test("Void", function() {
Roger Meierb661e832012-04-15 15:58:43 +000072 equal(client.testVoid(), undefined);
Roger Meierbb267d42011-07-30 15:10:42 +000073 });
74 test("String", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000075 equal(client.testString(''), '');
Roger Meierb661e832012-04-15 15:58:43 +000076 equal(client.testString(stringTest), stringTest);
Roger Meierbb267d42011-07-30 15:10:42 +000077
78 var specialCharacters = 'quote: \" backslash:' +
79 ' forwardslash-escaped: \/ ' +
80 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
81 ' now-all-of-them-together: "\\\/\b\n\r\t' +
82 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
Roger Meierb661e832012-04-15 15:58:43 +000083 equal(client.testString(specialCharacters),specialCharacters);
Roger Meierbb267d42011-07-30 15:10:42 +000084 });
85 test("Double", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000086 equal(client.testDouble(0), 0);
87 equal(client.testDouble(-1), -1);
Roger Meierb661e832012-04-15 15:58:43 +000088 equal(client.testDouble(3.14), 3.14);
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000089 equal(client.testDouble(Math.pow(2,60)), Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +000090 });
91 test("Byte", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000092 equal(client.testByte(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000093 equal(client.testByte(0x01), 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +000094 });
95 test("I32", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000096 equal(client.testI32(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000097 equal(client.testI32(Math.pow(2,30)), Math.pow(2,30));
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000098 equal(client.testI32(-Math.pow(2,30)), -Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +000099 });
100 test("I64", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000101 equal(client.testI64(0), 0);
henriquea2de4102014-02-07 14:12:56 +0100102 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
103 equal(client.testI64(Math.pow(2,52)), Math.pow(2,52));
104 equal(client.testI64(-Math.pow(2,52)), -Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000105 });
106
107
108module("Structured Types");
109
110 test("Struct", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000111 var structTestInput = new ThriftTest.Xtruct();
112 structTestInput.string_thing = 'worked';
113 structTestInput.byte_thing = 0x01;
114 structTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100115 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
116 structTestInput.i64_thing = Math.pow(2,52);
Roger Meierbb267d42011-07-30 15:10:42 +0000117
118 var structTestOutput = client.testStruct(structTestInput);
119
Roger Meierb661e832012-04-15 15:58:43 +0000120 equal(structTestOutput.string_thing, structTestInput.string_thing);
121 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
122 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
123 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Roger Meierbb267d42011-07-30 15:10:42 +0000124
Roger Meierb661e832012-04-15 15:58:43 +0000125 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000126 });
127
128 test("Nest", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000129 var xtrTestInput = new ThriftTest.Xtruct();
130 xtrTestInput.string_thing = 'worked';
131 xtrTestInput.byte_thing = 0x01;
132 xtrTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100133 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
134 xtrTestInput.i64_thing = Math.pow(2,52);
Roger Meierbb267d42011-07-30 15:10:42 +0000135
Roger Meieredf0d1d2011-08-03 21:21:43 +0000136 var nestTestInput = new ThriftTest.Xtruct2();
137 nestTestInput.byte_thing = 0x02;
138 nestTestInput.struct_thing = xtrTestInput;
139 nestTestInput.i32_thing = Math.pow(2,15);
Roger Meierbb267d42011-07-30 15:10:42 +0000140
141 var nestTestOutput = client.testNest(nestTestInput);
142
Roger Meierb661e832012-04-15 15:58:43 +0000143 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
144 equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
145 equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
146 equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
147 equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
148 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Roger Meierbb267d42011-07-30 15:10:42 +0000149
Roger Meierb661e832012-04-15 15:58:43 +0000150 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000151 });
152
153 test("Map", function() {
154 var mapTestInput = {7:77, 8:88, 9:99};
155
Roger Meieredf0d1d2011-08-03 21:21:43 +0000156 var mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000157
158 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000159 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000160 }
161 });
162
163 test("StringMap", function() {
164 var mapTestInput = {
165 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
166 "longValue":stringTest, stringTest:"long key"
167 };
168
Roger Meieredf0d1d2011-08-03 21:21:43 +0000169 var mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000170
171 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000172 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000173 }
174 });
175
176 test("Set", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000177 var setTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000178 ok(client.testSet(setTestInput), setTestInput);
179 });
180
181 test("List", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000182 var listTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000183 ok(client.testList(listTestInput), listTestInput);
184 });
185
186 test("Enum", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000187 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
Roger Meierbb267d42011-07-30 15:10:42 +0000188 });
189
190 test("TypeDef", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000191 equal(client.testTypedef(69), 69);
Roger Meierbb267d42011-07-30 15:10:42 +0000192 });
193
194
195module("deeper!");
196
197 test("MapMap", function() {
198 var mapMapTestExpectedResult = {
199 "4":{"1":1,"2":2,"3":3,"4":4},
200 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
201 };
202
203 var mapMapTestOutput = client.testMapMap(1);
204
205
206 for (var key in mapMapTestOutput) {
207 for (var key2 in mapMapTestOutput[key]) {
Roger Meierb661e832012-04-15 15:58:43 +0000208 equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
Roger Meierbb267d42011-07-30 15:10:42 +0000209 }
210 }
211
Roger Meier4f8a5232011-09-09 08:17:02 +0000212 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
Roger Meierbb267d42011-07-30 15:10:42 +0000213 });
214
215
216module("Exception");
217
218 test("Xception", function() {
219 expect(2);
220 try{
221 client.testException("Xception");
222 }catch(e){
Roger Meierb661e832012-04-15 15:58:43 +0000223 equal(e.errorCode, 1001);
224 equal(e.message, "Xception");
Roger Meierbb267d42011-07-30 15:10:42 +0000225 }
226 });
227
Roger Meierb661e832012-04-15 15:58:43 +0000228 test("no Exception", 0, function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000229 try{
230 client.testException("no Exception");
231 }catch(e){
232 ok(false);
233 }
234 });
235
Roger Meier8ebe0d92013-01-06 14:33:39 +0100236 test("TException", function() {
henriquea2de4102014-02-07 14:12:56 +0100237 //ThriftTest does not list TException as a legal exception so it will
238 // generate an exception on the server that does not propagate back to
239 // the client. This test has been modified to equate to "no exception"
Roger Meierbb267d42011-07-30 15:10:42 +0000240 expect(1);
241 try{
Roger Meier8ebe0d92013-01-06 14:33:39 +0100242 client.testException("TException");
Roger Meierbb267d42011-07-30 15:10:42 +0000243 } catch(e) {
henriquea2de4102014-02-07 14:12:56 +0100244 //ok(false);
Roger Meierbb267d42011-07-30 15:10:42 +0000245 }
henriquea2de4102014-02-07 14:12:56 +0100246 ok(true);
Roger Meierbb267d42011-07-30 15:10:42 +0000247 });
248
249
250module("Insanity");
251
252 test("testInsanity", function() {
253 var insanity = {
254 "1":{
Roger Meier4f8a5232011-09-09 08:17:02 +0000255 "2":{
256 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000257 "xtructs":[{
258 "string_thing":"Goodbye4",
259 "byte_thing":4,
260 "i32_thing":4,
261 "i64_thing":4
262 },
263 {
264 "string_thing":"Hello2",
265 "byte_thing":2,
266 "i32_thing":2,
267 "i64_thing":2
268 }
269 ]
270 },
Roger Meier4f8a5232011-09-09 08:17:02 +0000271 "3":{
272 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000273 "xtructs":[{
274 "string_thing":"Goodbye4",
275 "byte_thing":4,
276 "i32_thing":4,
277 "i64_thing":4
278 },
279 {
280 "string_thing":"Hello2",
281 "byte_thing":2,
282 "i32_thing":2,
283 "i64_thing":2
284 }
285 ]
286 }
287 },
288 "2":{ "6":{ "userMap":null, "xtructs":null } }
289 };
Roger Meierb661e832012-04-15 15:58:43 +0000290 var res = client.testInsanity(new ThriftTest.Insanity());
Roger Meierbb267d42011-07-30 15:10:42 +0000291 ok(res, JSON.stringify(res));
292 ok(insanity, JSON.stringify(insanity));
Roger Meier4f8a5232011-09-09 08:17:02 +0000293
294 checkRecursively(res, insanity);
Roger Meierbb267d42011-07-30 15:10:42 +0000295 });
296
297
298//////////////////////////////////
299//Run same tests asynchronously
Roger Meierbb267d42011-07-30 15:10:42 +0000300
301module("Async");
302
303 test("Double", function() {
304 expect( 1 );
305
306 QUnit.stop();
307 client.testDouble(3.14159265, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000308 equal(result, 3.14159265);
Roger Meierbb267d42011-07-30 15:10:42 +0000309 QUnit.start();
310 });
311 });
312
313 test("Byte", function() {
314 expect( 1 );
315
316 QUnit.stop();
317 client.testByte(0x01, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000318 equal(result, 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000319 QUnit.start();
320 });
321 });
322
323 test("I32", function() {
henriquea2de4102014-02-07 14:12:56 +0100324 expect( 2 );
Roger Meierbb267d42011-07-30 15:10:42 +0000325
326 QUnit.stop();
327 client.testI32(Math.pow(2,30), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000328 equal(result, Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000329 QUnit.start();
330 });
331
332 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100333 client.testI32(Math.pow(-2,31), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000334 equal(result, Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000335 QUnit.start();
336 });
337 });
338
339 test("I64", function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000340 expect( 2 );
341
342 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100343 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
344 client.testI64(Math.pow(2,52), function(result) {
345 equal(result, Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000346 QUnit.start();
henriquea2de4102014-02-07 14:12:56 +0100347 });
348
349 QUnit.stop();
350 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
351 client.testI64(Math.pow(-2,52), function(result) {
352 equal(result, Math.pow(-2,52));
353 QUnit.start();
Roger Meierbb267d42011-07-30 15:10:42 +0000354 });
355 });