blob: 2d8ec9b157e86ff4aaf3c07a56d2754ac1edcb7e [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);
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +090061 if (details.message) console.log(details.message);
Nobuaki Sukegawa72f88092015-05-10 23:35:29 +090062 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 });
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +090089 test("Binary (String)", function() {
90 var binary = '';
91 for (var v = 255; v >= 0; --v) {
92 binary += String.fromCharCode(v);
93 }
94 equal(client.testBinary(binary), binary);
95 });
96 test("Binary (Uint8Array)", function() {
97 var binary = '';
98 for (var v = 255; v >= 0; --v) {
99 binary += String.fromCharCode(v);
100 }
101 var arr = new Uint8Array(binary.length);
102 for (var i = 0; i < binary.length; ++i) {
103 arr[i] = binary[i].charCodeAt();
104 }
105 equal(client.testBinary(arr), binary);
106 });
Roger Meierbb267d42011-07-30 15:10:42 +0000107 test("String", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000108 equal(client.testString(''), '');
Roger Meierb661e832012-04-15 15:58:43 +0000109 equal(client.testString(stringTest), stringTest);
Roger Meierbb267d42011-07-30 15:10:42 +0000110
111 var specialCharacters = 'quote: \" backslash:' +
112 ' forwardslash-escaped: \/ ' +
113 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
114 ' now-all-of-them-together: "\\\/\b\n\r\t' +
115 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
Roger Meierb661e832012-04-15 15:58:43 +0000116 equal(client.testString(specialCharacters),specialCharacters);
Roger Meierbb267d42011-07-30 15:10:42 +0000117 });
118 test("Double", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000119 equal(client.testDouble(0), 0);
120 equal(client.testDouble(-1), -1);
Roger Meierb661e832012-04-15 15:58:43 +0000121 equal(client.testDouble(3.14), 3.14);
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000122 equal(client.testDouble(Math.pow(2,60)), Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +0000123 });
124 test("Byte", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000125 equal(client.testByte(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +0000126 equal(client.testByte(0x01), 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000127 });
128 test("I32", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000129 equal(client.testI32(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +0000130 equal(client.testI32(Math.pow(2,30)), Math.pow(2,30));
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000131 equal(client.testI32(-Math.pow(2,30)), -Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000132 });
133 test("I64", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000134 equal(client.testI64(0), 0);
henriquea2de4102014-02-07 14:12:56 +0100135 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
136 equal(client.testI64(Math.pow(2,52)), Math.pow(2,52));
137 equal(client.testI64(-Math.pow(2,52)), -Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000138 });
139
140
141module("Structured Types");
142
143 test("Struct", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000144 var structTestInput = new ThriftTest.Xtruct();
145 structTestInput.string_thing = 'worked';
146 structTestInput.byte_thing = 0x01;
147 structTestInput.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 structTestInput.i64_thing = Math.pow(2,52);
Roger Meierbb267d42011-07-30 15:10:42 +0000150
151 var structTestOutput = client.testStruct(structTestInput);
152
Roger Meierb661e832012-04-15 15:58:43 +0000153 equal(structTestOutput.string_thing, structTestInput.string_thing);
154 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
155 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
156 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Roger Meier08562732015-06-14 22:30:22 +0200157
Roger Meierb661e832012-04-15 15:58:43 +0000158 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000159 });
160
161 test("Nest", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000162 var xtrTestInput = new ThriftTest.Xtruct();
163 xtrTestInput.string_thing = 'worked';
164 xtrTestInput.byte_thing = 0x01;
165 xtrTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100166 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
167 xtrTestInput.i64_thing = Math.pow(2,52);
Roger Meier08562732015-06-14 22:30:22 +0200168
Roger Meieredf0d1d2011-08-03 21:21:43 +0000169 var nestTestInput = new ThriftTest.Xtruct2();
170 nestTestInput.byte_thing = 0x02;
171 nestTestInput.struct_thing = xtrTestInput;
172 nestTestInput.i32_thing = Math.pow(2,15);
Roger Meier08562732015-06-14 22:30:22 +0200173
Roger Meierbb267d42011-07-30 15:10:42 +0000174 var nestTestOutput = client.testNest(nestTestInput);
Roger Meier08562732015-06-14 22:30:22 +0200175
Roger Meierb661e832012-04-15 15:58:43 +0000176 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
177 equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
178 equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
179 equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
180 equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
181 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Roger Meier08562732015-06-14 22:30:22 +0200182
Roger Meierb661e832012-04-15 15:58:43 +0000183 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000184 });
185
186 test("Map", function() {
187 var mapTestInput = {7:77, 8:88, 9:99};
188
Roger Meieredf0d1d2011-08-03 21:21:43 +0000189 var mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000190
191 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000192 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000193 }
194 });
195
196 test("StringMap", function() {
197 var mapTestInput = {
198 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
199 "longValue":stringTest, stringTest:"long key"
200 };
201
Roger Meieredf0d1d2011-08-03 21:21:43 +0000202 var mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000203
204 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000205 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000206 }
207 });
208
209 test("Set", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000210 var setTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000211 ok(client.testSet(setTestInput), setTestInput);
212 });
213
214 test("List", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000215 var listTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000216 ok(client.testList(listTestInput), listTestInput);
217 });
218
219 test("Enum", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000220 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
Roger Meierbb267d42011-07-30 15:10:42 +0000221 });
222
223 test("TypeDef", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000224 equal(client.testTypedef(69), 69);
Roger Meierbb267d42011-07-30 15:10:42 +0000225 });
226
Jens Geyer329d59a2014-06-19 22:11:53 +0200227 test("Skip", function() {
228 var structTestInput = new ThriftTest.Xtruct();
229 var modifiedClient = new ThriftTest.ThriftTestClient(protocol);
230
231 modifiedClient.recv_testStruct = function() {
232 var input = modifiedClient.input;
233 var xtruct3 = new ThriftTest.Xtruct3();
234
235 input.readMessageBegin();
236 input.readStructBegin();
237
Roger Meier08562732015-06-14 22:30:22 +0200238 // read Xtruct data with Xtruct3
Jens Geyer329d59a2014-06-19 22:11:53 +0200239 input.readFieldBegin();
240 xtruct3.read(input);
241 input.readFieldEnd();
242 // read Thrift.Type.STOP message
243 input.readFieldBegin();
244 input.readFieldEnd();
245
246 input.readStructEnd();
247 input.readMessageEnd();
248
249 return xtruct3;
250 };
251
252 structTestInput.string_thing = 'worked';
253 structTestInput.byte_thing = 0x01;
254 structTestInput.i32_thing = Math.pow(2,30);
255 structTestInput.i64_thing = Math.pow(2,52);
256
257 var structTestOutput = modifiedClient.testStruct(structTestInput);
258
259 equal(structTestOutput instanceof ThriftTest.Xtruct3, true);
260 equal(structTestOutput.string_thing, structTestInput.string_thing);
261 equal(structTestOutput.changed, null);
262 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
263 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
264 });
265
Roger Meierbb267d42011-07-30 15:10:42 +0000266
267module("deeper!");
268
269 test("MapMap", function() {
270 var mapMapTestExpectedResult = {
271 "4":{"1":1,"2":2,"3":3,"4":4},
272 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
273 };
274
275 var mapMapTestOutput = client.testMapMap(1);
276
277
278 for (var key in mapMapTestOutput) {
279 for (var key2 in mapMapTestOutput[key]) {
Roger Meierb661e832012-04-15 15:58:43 +0000280 equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
Roger Meierbb267d42011-07-30 15:10:42 +0000281 }
282 }
Roger Meier08562732015-06-14 22:30:22 +0200283
Roger Meier4f8a5232011-09-09 08:17:02 +0000284 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
Roger Meierbb267d42011-07-30 15:10:42 +0000285 });
286
287
288module("Exception");
289
290 test("Xception", function() {
291 expect(2);
292 try{
293 client.testException("Xception");
294 }catch(e){
Roger Meierb661e832012-04-15 15:58:43 +0000295 equal(e.errorCode, 1001);
296 equal(e.message, "Xception");
Roger Meierbb267d42011-07-30 15:10:42 +0000297 }
298 });
299
Roger Meierb661e832012-04-15 15:58:43 +0000300 test("no Exception", 0, function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000301 try{
302 client.testException("no Exception");
303 }catch(e){
304 ok(false);
305 }
306 });
307
Roger Meier8ebe0d92013-01-06 14:33:39 +0100308 test("TException", function() {
henriquea2de4102014-02-07 14:12:56 +0100309 //ThriftTest does not list TException as a legal exception so it will
Roger Meier08562732015-06-14 22:30:22 +0200310 // generate an exception on the server that does not propagate back to
henriquea2de4102014-02-07 14:12:56 +0100311 // the client. This test has been modified to equate to "no exception"
Roger Meierbb267d42011-07-30 15:10:42 +0000312 expect(1);
313 try{
Roger Meier8ebe0d92013-01-06 14:33:39 +0100314 client.testException("TException");
Roger Meierbb267d42011-07-30 15:10:42 +0000315 } catch(e) {
henriquea2de4102014-02-07 14:12:56 +0100316 //ok(false);
Roger Meierbb267d42011-07-30 15:10:42 +0000317 }
henriquea2de4102014-02-07 14:12:56 +0100318 ok(true);
Roger Meierbb267d42011-07-30 15:10:42 +0000319 });
320
321
322module("Insanity");
323
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900324 var crazy = {
325 "userMap":{ "5":5, "8":8 },
326 "xtructs":[{
327 "string_thing":"Goodbye4",
328 "byte_thing":4,
329 "i32_thing":4,
330 "i64_thing":4
331 },
332 {
333 "string_thing":"Hello2",
334 "byte_thing":2,
335 "i32_thing":2,
336 "i64_thing":2
337 }]
338 };
Roger Meierbb267d42011-07-30 15:10:42 +0000339 test("testInsanity", function() {
340 var insanity = {
341 "1":{
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900342 "2":crazy,
343 "3":crazy
Roger Meierbb267d42011-07-30 15:10:42 +0000344 },
345 "2":{ "6":{ "userMap":null, "xtructs":null } }
346 };
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900347 var res = client.testInsanity(new ThriftTest.Insanity(crazy));
Roger Meierbb267d42011-07-30 15:10:42 +0000348 ok(res, JSON.stringify(res));
349 ok(insanity, JSON.stringify(insanity));
Roger Meier4f8a5232011-09-09 08:17:02 +0000350
351 checkRecursively(res, insanity);
Roger Meierbb267d42011-07-30 15:10:42 +0000352 });
353
354
355//////////////////////////////////
356//Run same tests asynchronously
Roger Meierbb267d42011-07-30 15:10:42 +0000357
358module("Async");
359
360 test("Double", function() {
361 expect( 1 );
362
363 QUnit.stop();
364 client.testDouble(3.14159265, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000365 equal(result, 3.14159265);
Roger Meierbb267d42011-07-30 15:10:42 +0000366 QUnit.start();
367 });
368 });
369
370 test("Byte", function() {
371 expect( 1 );
372
373 QUnit.stop();
374 client.testByte(0x01, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000375 equal(result, 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000376 QUnit.start();
377 });
378 });
379
380 test("I32", function() {
henriquea2de4102014-02-07 14:12:56 +0100381 expect( 2 );
Roger Meierbb267d42011-07-30 15:10:42 +0000382
383 QUnit.stop();
384 client.testI32(Math.pow(2,30), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000385 equal(result, Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000386 QUnit.start();
387 });
388
389 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100390 client.testI32(Math.pow(-2,31), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000391 equal(result, Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000392 QUnit.start();
393 });
394 });
395
396 test("I64", function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000397 expect( 2 );
398
399 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100400 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
401 client.testI64(Math.pow(2,52), function(result) {
402 equal(result, Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000403 QUnit.start();
henriquea2de4102014-02-07 14:12:56 +0100404 });
405
406 QUnit.stop();
407 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
408 client.testI64(Math.pow(-2,52), function(result) {
409 equal(result, Math.pow(-2,52));
410 QUnit.start();
Roger Meierbb267d42011-07-30 15:10:42 +0000411 });
412 });