blob: ee146128a2a8940bec3741b8553fe1d4e351b55f [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 Meier08562732015-06-14 22:30:22 +020020
Roger Meierbb267d42011-07-30 15:10:42 +000021/*
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.
Roger Meier08562732015-06-14 22:30:22 +020029 *
30 * Asynchronous success callbacks passed as the last parameter
henriquea2de4102014-02-07 14:12:56 +010031 * of an RPC call should be identical in both Normal and jQuery
32 * interfaces. Async success tests belong here.
Roger Meier08562732015-06-14 22:30:22 +020033 *
henriquea2de4102014-02-07 14:12:56 +010034 * 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
Roger Meier08562732015-06-14 22:30:22 +020037 * tests also belong in test-jq.js. Do not create any jQuery
henriquea2de4102014-02-07 14:12:56 +010038 * 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:
Roger Meier08562732015-06-14 22:30:22 +020046 * ++ test-nojq.js for "-gen js" only tests
henriquea2de4102014-02-07 14:12:56 +010047 * ++ 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
Nobuaki Sukegawa72f88092015-05-10 23:35:29 +090054// Work around for old API used by QUnitAdapter of jsTestDriver
55if (typeof QUnit.log == 'function') {
56 // When using real QUnit (fron PhantomJS) log failures to console
57 QUnit.log(function(details) {
58 if (!details.result) {
59 console.log('======== FAIL ========');
60 console.log('TestName: ' + details.name);
61 details.message && console.log(details.message);
62 console.log('Expected: ' + details.expected);
63 console.log('Actual : ' + details.actual);
64 console.log('======================');
65 }
66 });
67}
68
Roger Meierbb267d42011-07-30 15:10:42 +000069// all Languages in UTF-8
70var 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 Meier08562732015-06-14 22:30:22 +020071
Roger Meier4f8a5232011-09-09 08:17:02 +000072function checkRecursively(map1, map2) {
73 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
74 if (!map1 || typeof map1 !== 'object') {
Roger Meierb661e832012-04-15 15:58:43 +000075 equal(map1, map2);
Roger Meier4f8a5232011-09-09 08:17:02 +000076 } else {
77 for (var key in map1) {
78 checkRecursively(map1[key], map2[key]);
79 }
80 }
81 }
82}
Roger Meierbb267d42011-07-30 15:10:42 +000083
84module("Base Types");
85
86 test("Void", function() {
Roger Meierb661e832012-04-15 15:58:43 +000087 equal(client.testVoid(), undefined);
Roger Meierbb267d42011-07-30 15:10:42 +000088 });
89 test("String", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000090 equal(client.testString(''), '');
Roger Meierb661e832012-04-15 15:58:43 +000091 equal(client.testString(stringTest), stringTest);
Roger Meierbb267d42011-07-30 15:10:42 +000092
93 var specialCharacters = 'quote: \" backslash:' +
94 ' forwardslash-escaped: \/ ' +
95 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
96 ' now-all-of-them-together: "\\\/\b\n\r\t' +
97 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
Roger Meierb661e832012-04-15 15:58:43 +000098 equal(client.testString(specialCharacters),specialCharacters);
Roger Meierbb267d42011-07-30 15:10:42 +000099 });
100 test("Double", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000101 equal(client.testDouble(0), 0);
102 equal(client.testDouble(-1), -1);
Roger Meierb661e832012-04-15 15:58:43 +0000103 equal(client.testDouble(3.14), 3.14);
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000104 equal(client.testDouble(Math.pow(2,60)), Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +0000105 });
106 test("Byte", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000107 equal(client.testByte(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +0000108 equal(client.testByte(0x01), 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000109 });
110 test("I32", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000111 equal(client.testI32(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +0000112 equal(client.testI32(Math.pow(2,30)), Math.pow(2,30));
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000113 equal(client.testI32(-Math.pow(2,30)), -Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000114 });
115 test("I64", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000116 equal(client.testI64(0), 0);
henriquea2de4102014-02-07 14:12:56 +0100117 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
118 equal(client.testI64(Math.pow(2,52)), Math.pow(2,52));
119 equal(client.testI64(-Math.pow(2,52)), -Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000120 });
121
122
123module("Structured Types");
124
125 test("Struct", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000126 var structTestInput = new ThriftTest.Xtruct();
127 structTestInput.string_thing = 'worked';
128 structTestInput.byte_thing = 0x01;
129 structTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100130 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
131 structTestInput.i64_thing = Math.pow(2,52);
Roger Meierbb267d42011-07-30 15:10:42 +0000132
133 var structTestOutput = client.testStruct(structTestInput);
134
Roger Meierb661e832012-04-15 15:58:43 +0000135 equal(structTestOutput.string_thing, structTestInput.string_thing);
136 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
137 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
138 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Roger Meier08562732015-06-14 22:30:22 +0200139
Roger Meierb661e832012-04-15 15:58:43 +0000140 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000141 });
142
143 test("Nest", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000144 var xtrTestInput = new ThriftTest.Xtruct();
145 xtrTestInput.string_thing = 'worked';
146 xtrTestInput.byte_thing = 0x01;
147 xtrTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100148 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
149 xtrTestInput.i64_thing = Math.pow(2,52);
Roger Meier08562732015-06-14 22:30:22 +0200150
Roger Meieredf0d1d2011-08-03 21:21:43 +0000151 var nestTestInput = new ThriftTest.Xtruct2();
152 nestTestInput.byte_thing = 0x02;
153 nestTestInput.struct_thing = xtrTestInput;
154 nestTestInput.i32_thing = Math.pow(2,15);
Roger Meier08562732015-06-14 22:30:22 +0200155
Roger Meierbb267d42011-07-30 15:10:42 +0000156 var nestTestOutput = client.testNest(nestTestInput);
Roger Meier08562732015-06-14 22:30:22 +0200157
Roger Meierb661e832012-04-15 15:58:43 +0000158 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
159 equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
160 equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
161 equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
162 equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
163 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Roger Meier08562732015-06-14 22:30:22 +0200164
Roger Meierb661e832012-04-15 15:58:43 +0000165 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000166 });
167
168 test("Map", function() {
169 var mapTestInput = {7:77, 8:88, 9:99};
170
Roger Meieredf0d1d2011-08-03 21:21:43 +0000171 var mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000172
173 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000174 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000175 }
176 });
177
178 test("StringMap", function() {
179 var mapTestInput = {
180 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
181 "longValue":stringTest, stringTest:"long key"
182 };
183
Roger Meieredf0d1d2011-08-03 21:21:43 +0000184 var mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000185
186 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000187 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000188 }
189 });
190
191 test("Set", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000192 var setTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000193 ok(client.testSet(setTestInput), setTestInput);
194 });
195
196 test("List", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000197 var listTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000198 ok(client.testList(listTestInput), listTestInput);
199 });
200
201 test("Enum", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000202 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
Roger Meierbb267d42011-07-30 15:10:42 +0000203 });
204
205 test("TypeDef", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000206 equal(client.testTypedef(69), 69);
Roger Meierbb267d42011-07-30 15:10:42 +0000207 });
208
Jens Geyer329d59a2014-06-19 22:11:53 +0200209 test("Skip", function() {
210 var structTestInput = new ThriftTest.Xtruct();
211 var modifiedClient = new ThriftTest.ThriftTestClient(protocol);
212
213 modifiedClient.recv_testStruct = function() {
214 var input = modifiedClient.input;
215 var xtruct3 = new ThriftTest.Xtruct3();
216
217 input.readMessageBegin();
218 input.readStructBegin();
219
Roger Meier08562732015-06-14 22:30:22 +0200220 // read Xtruct data with Xtruct3
Jens Geyer329d59a2014-06-19 22:11:53 +0200221 input.readFieldBegin();
222 xtruct3.read(input);
223 input.readFieldEnd();
224 // read Thrift.Type.STOP message
225 input.readFieldBegin();
226 input.readFieldEnd();
227
228 input.readStructEnd();
229 input.readMessageEnd();
230
231 return xtruct3;
232 };
233
234 structTestInput.string_thing = 'worked';
235 structTestInput.byte_thing = 0x01;
236 structTestInput.i32_thing = Math.pow(2,30);
237 structTestInput.i64_thing = Math.pow(2,52);
238
239 var structTestOutput = modifiedClient.testStruct(structTestInput);
240
241 equal(structTestOutput instanceof ThriftTest.Xtruct3, true);
242 equal(structTestOutput.string_thing, structTestInput.string_thing);
243 equal(structTestOutput.changed, null);
244 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
245 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
246 });
247
Roger Meierbb267d42011-07-30 15:10:42 +0000248
249module("deeper!");
250
251 test("MapMap", function() {
252 var mapMapTestExpectedResult = {
253 "4":{"1":1,"2":2,"3":3,"4":4},
254 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
255 };
256
257 var mapMapTestOutput = client.testMapMap(1);
258
259
260 for (var key in mapMapTestOutput) {
261 for (var key2 in mapMapTestOutput[key]) {
Roger Meierb661e832012-04-15 15:58:43 +0000262 equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
Roger Meierbb267d42011-07-30 15:10:42 +0000263 }
264 }
Roger Meier08562732015-06-14 22:30:22 +0200265
Roger Meier4f8a5232011-09-09 08:17:02 +0000266 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
Roger Meierbb267d42011-07-30 15:10:42 +0000267 });
268
269
270module("Exception");
271
272 test("Xception", function() {
273 expect(2);
274 try{
275 client.testException("Xception");
276 }catch(e){
Roger Meierb661e832012-04-15 15:58:43 +0000277 equal(e.errorCode, 1001);
278 equal(e.message, "Xception");
Roger Meierbb267d42011-07-30 15:10:42 +0000279 }
280 });
281
Roger Meierb661e832012-04-15 15:58:43 +0000282 test("no Exception", 0, function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000283 try{
284 client.testException("no Exception");
285 }catch(e){
286 ok(false);
287 }
288 });
289
Roger Meier8ebe0d92013-01-06 14:33:39 +0100290 test("TException", function() {
henriquea2de4102014-02-07 14:12:56 +0100291 //ThriftTest does not list TException as a legal exception so it will
Roger Meier08562732015-06-14 22:30:22 +0200292 // generate an exception on the server that does not propagate back to
henriquea2de4102014-02-07 14:12:56 +0100293 // the client. This test has been modified to equate to "no exception"
Roger Meierbb267d42011-07-30 15:10:42 +0000294 expect(1);
295 try{
Roger Meier8ebe0d92013-01-06 14:33:39 +0100296 client.testException("TException");
Roger Meierbb267d42011-07-30 15:10:42 +0000297 } catch(e) {
henriquea2de4102014-02-07 14:12:56 +0100298 //ok(false);
Roger Meierbb267d42011-07-30 15:10:42 +0000299 }
henriquea2de4102014-02-07 14:12:56 +0100300 ok(true);
Roger Meierbb267d42011-07-30 15:10:42 +0000301 });
302
303
304module("Insanity");
305
306 test("testInsanity", function() {
307 var insanity = {
308 "1":{
Roger Meier4f8a5232011-09-09 08:17:02 +0000309 "2":{
310 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000311 "xtructs":[{
312 "string_thing":"Goodbye4",
313 "byte_thing":4,
314 "i32_thing":4,
315 "i64_thing":4
316 },
317 {
318 "string_thing":"Hello2",
319 "byte_thing":2,
320 "i32_thing":2,
321 "i64_thing":2
322 }
323 ]
324 },
Roger Meier4f8a5232011-09-09 08:17:02 +0000325 "3":{
326 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000327 "xtructs":[{
328 "string_thing":"Goodbye4",
329 "byte_thing":4,
330 "i32_thing":4,
331 "i64_thing":4
332 },
333 {
334 "string_thing":"Hello2",
335 "byte_thing":2,
336 "i32_thing":2,
337 "i64_thing":2
338 }
339 ]
340 }
341 },
342 "2":{ "6":{ "userMap":null, "xtructs":null } }
343 };
Roger Meierb661e832012-04-15 15:58:43 +0000344 var res = client.testInsanity(new ThriftTest.Insanity());
Roger Meierbb267d42011-07-30 15:10:42 +0000345 ok(res, JSON.stringify(res));
346 ok(insanity, JSON.stringify(insanity));
Roger Meier4f8a5232011-09-09 08:17:02 +0000347
348 checkRecursively(res, insanity);
Roger Meierbb267d42011-07-30 15:10:42 +0000349 });
350
351
352//////////////////////////////////
353//Run same tests asynchronously
Roger Meierbb267d42011-07-30 15:10:42 +0000354
355module("Async");
356
357 test("Double", function() {
358 expect( 1 );
359
360 QUnit.stop();
361 client.testDouble(3.14159265, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000362 equal(result, 3.14159265);
Roger Meierbb267d42011-07-30 15:10:42 +0000363 QUnit.start();
364 });
365 });
366
367 test("Byte", function() {
368 expect( 1 );
369
370 QUnit.stop();
371 client.testByte(0x01, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000372 equal(result, 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000373 QUnit.start();
374 });
375 });
376
377 test("I32", function() {
henriquea2de4102014-02-07 14:12:56 +0100378 expect( 2 );
Roger Meierbb267d42011-07-30 15:10:42 +0000379
380 QUnit.stop();
381 client.testI32(Math.pow(2,30), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000382 equal(result, Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000383 QUnit.start();
384 });
385
386 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100387 client.testI32(Math.pow(-2,31), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000388 equal(result, Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000389 QUnit.start();
390 });
391 });
392
393 test("I64", function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000394 expect( 2 );
395
396 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100397 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
398 client.testI64(Math.pow(2,52), function(result) {
399 equal(result, Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000400 QUnit.start();
henriquea2de4102014-02-07 14:12:56 +0100401 });
402
403 QUnit.stop();
404 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
405 client.testI64(Math.pow(-2,52), function(result) {
406 equal(result, Math.pow(-2,52));
407 QUnit.start();
Roger Meierbb267d42011-07-30 15:10:42 +0000408 });
409 });