blob: b4e985426452b67127c557dd73b55c207b92debc [file] [log] [blame]
henrique2a7dccc2014-03-07 22:16:51 +01001/*
2 * 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 */
19 /* jshint -W100 */
Kazuki Matsudab909a382016-02-13 19:36:09 +090020
henrique2a7dccc2014-03-07 22:16:51 +010021/*
Kazuki Matsudab909a382016-02-13 19:36:09 +090022 * Fully Async JavaScript test suite for ThriftTest.thrift.
henrique2a7dccc2014-03-07 22:16:51 +010023 * These tests are designed to exercise the WebSocket transport
24 * (which is exclusively async).
25 *
26 * To compile client code for this test use:
27 * $ thrift -gen js ThriftTest.thrift
28 */
29
30
31
32// all Languages in UTF-8
33var 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ú, 粵語";
Kazuki Matsudab909a382016-02-13 19:36:09 +090034
henrique2a7dccc2014-03-07 22:16:51 +010035function checkRecursively(map1, map2) {
36 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
37 if (!map1 || typeof map1 !== 'object') {
38 equal(map1, map2);
39 } else {
40 for (var key in map1) {
41 checkRecursively(map1[key], map2[key]);
42 }
43 }
44 }
45}
46
Kazuki Matsudab909a382016-02-13 19:36:09 +090047module('Base Types');
henrique2a7dccc2014-03-07 22:16:51 +010048
Kazuki Matsudab909a382016-02-13 19:36:09 +090049 asyncTest('Void', function() {
50 expect(1);
henrique2a7dccc2014-03-07 22:16:51 +010051 client.testVoid(function(result) {
52 equal(result, undefined);
53 QUnit.start();
54 });
55 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090056
57
58 asyncTest('String', function() {
59 expect(3);
henrique2a7dccc2014-03-07 22:16:51 +010060 QUnit.stop(2);
Kazuki Matsudab909a382016-02-13 19:36:09 +090061 client.testString('', function(result) {
henrique2a7dccc2014-03-07 22:16:51 +010062 equal(result, '');
63 QUnit.start();
64 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090065 client.testString(stringTest, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +010066 equal(result, stringTest);
67 QUnit.start();
68 });
69
70 var specialCharacters = 'quote: \" backslash:' +
71 ' forwardslash-escaped: \/ ' +
72 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
73 ' now-all-of-them-together: "\\\/\b\n\r\t' +
74 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
Kazuki Matsudab909a382016-02-13 19:36:09 +090075 client.testString(specialCharacters, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +010076 equal(result, specialCharacters);
77 QUnit.start();
78 });
79 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090080 asyncTest('Double', function() {
81 expect(4);
henrique2a7dccc2014-03-07 22:16:51 +010082 QUnit.stop(3);
Kazuki Matsudab909a382016-02-13 19:36:09 +090083 client.testDouble(0, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +010084 equal(result, 0);
85 QUnit.start();
86 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090087 client.testDouble(-1, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +010088 equal(result, -1);
89 QUnit.start();
90 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090091 client.testDouble(3.14, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +010092 equal(result, 3.14);
93 QUnit.start();
94 });
Kazuki Matsudab909a382016-02-13 19:36:09 +090095 client.testDouble(Math.pow(2, 60), function(result) {
96 equal(result, Math.pow(2, 60));
henrique2a7dccc2014-03-07 22:16:51 +010097 QUnit.start();
98 });
99 });
Kazuki Matsudab909a382016-02-13 19:36:09 +0900100 // TODO: add testBinary()
101 asyncTest('Byte', function() {
102 expect(2);
henrique2a7dccc2014-03-07 22:16:51 +0100103 QUnit.stop();
104 client.testByte(0, function(result) {
105 equal(result, 0);
106 QUnit.start();
107 });
108 client.testByte(0x01, function(result) {
109 equal(result, 0x01);
110 QUnit.start();
111 });
112 });
Kazuki Matsudab909a382016-02-13 19:36:09 +0900113 asyncTest('I32', function() {
114 expect(3);
henrique2a7dccc2014-03-07 22:16:51 +0100115 QUnit.stop(2);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900116 client.testI32(0, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100117 equal(result, 0);
118 QUnit.start();
119 });
Kazuki Matsudab909a382016-02-13 19:36:09 +0900120 client.testI32(Math.pow(2, 30), function(result) {
121 equal(result, Math.pow(2, 30));
henrique2a7dccc2014-03-07 22:16:51 +0100122 QUnit.start();
123 });
Kazuki Matsudab909a382016-02-13 19:36:09 +0900124 client.testI32(-Math.pow(2, 30), function(result) {
125 equal(result, -Math.pow(2, 30));
henrique2a7dccc2014-03-07 22:16:51 +0100126 QUnit.start();
127 });
128 });
Kazuki Matsudab909a382016-02-13 19:36:09 +0900129 asyncTest('I64', function() {
130 expect(3);
henrique2a7dccc2014-03-07 22:16:51 +0100131 QUnit.stop(2);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900132 client.testI64(0, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100133 equal(result, 0);
134 QUnit.start();
135 });
136 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900137 client.testI64(Math.pow(2, 52), function(result) {
138 equal(result, Math.pow(2, 52));
henrique2a7dccc2014-03-07 22:16:51 +0100139 QUnit.start();
140 });
Kazuki Matsudab909a382016-02-13 19:36:09 +0900141 client.testI64(-Math.pow(2, 52), function(result) {
142 equal(result, -Math.pow(2, 52));
henrique2a7dccc2014-03-07 22:16:51 +0100143 QUnit.start();
144 });
145 });
henrique2a7dccc2014-03-07 22:16:51 +0100146
henrique2a7dccc2014-03-07 22:16:51 +0100147
henrique2a7dccc2014-03-07 22:16:51 +0100148
Kazuki Matsudab909a382016-02-13 19:36:09 +0900149
150module('Structured Types');
151
152 asyncTest('Struct', function() {
153 expect(5);
henrique2a7dccc2014-03-07 22:16:51 +0100154 var structTestInput = new ThriftTest.Xtruct();
155 structTestInput.string_thing = 'worked';
156 structTestInput.byte_thing = 0x01;
Kazuki Matsudab909a382016-02-13 19:36:09 +0900157 structTestInput.i32_thing = Math.pow(2, 30);
henrique2a7dccc2014-03-07 22:16:51 +0100158 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900159 structTestInput.i64_thing = Math.pow(2, 52);
henrique2a7dccc2014-03-07 22:16:51 +0100160
Kazuki Matsudab909a382016-02-13 19:36:09 +0900161 client.testStruct(structTestInput, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100162 equal(result.string_thing, structTestInput.string_thing);
163 equal(result.byte_thing, structTestInput.byte_thing);
164 equal(result.i32_thing, structTestInput.i32_thing);
165 equal(result.i64_thing, structTestInput.i64_thing);
166 equal(JSON.stringify(result), JSON.stringify(structTestInput));
Kazuki Matsudab909a382016-02-13 19:36:09 +0900167 QUnit.start();
henrique2a7dccc2014-03-07 22:16:51 +0100168 });
169 });
170
Kazuki Matsudab909a382016-02-13 19:36:09 +0900171 asyncTest('Nest', function() {
172 expect(7);
henrique2a7dccc2014-03-07 22:16:51 +0100173 var xtrTestInput = new ThriftTest.Xtruct();
174 xtrTestInput.string_thing = 'worked';
175 xtrTestInput.byte_thing = 0x01;
Kazuki Matsudab909a382016-02-13 19:36:09 +0900176 xtrTestInput.i32_thing = Math.pow(2, 30);
henrique2a7dccc2014-03-07 22:16:51 +0100177 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900178 xtrTestInput.i64_thing = Math.pow(2, 52);
179
henrique2a7dccc2014-03-07 22:16:51 +0100180 var nestTestInput = new ThriftTest.Xtruct2();
181 nestTestInput.byte_thing = 0x02;
182 nestTestInput.struct_thing = xtrTestInput;
Kazuki Matsudab909a382016-02-13 19:36:09 +0900183 nestTestInput.i32_thing = Math.pow(2, 15);
184
185 client.testNest(nestTestInput, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100186 equal(result.byte_thing, nestTestInput.byte_thing);
187 equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
188 equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
189 equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
190 equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900191 equal(result.i32_thing, nestTestInput.i32_thing);
henrique2a7dccc2014-03-07 22:16:51 +0100192 equal(JSON.stringify(result), JSON.stringify(nestTestInput));
Kazuki Matsudab909a382016-02-13 19:36:09 +0900193 QUnit.start();
henrique2a7dccc2014-03-07 22:16:51 +0100194 });
195 });
196
Kazuki Matsudab909a382016-02-13 19:36:09 +0900197 asyncTest('Map', function() {
198 expect(3);
199 var mapTestInput = {7: 77, 8: 88, 9: 99};
henrique2a7dccc2014-03-07 22:16:51 +0100200
Kazuki Matsudab909a382016-02-13 19:36:09 +0900201 client.testMap(mapTestInput, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100202 for (var key in result) {
203 equal(result[key], mapTestInput[key]);
204 }
205 QUnit.start();
206 });
207 });
208
Kazuki Matsudab909a382016-02-13 19:36:09 +0900209 asyncTest('StringMap', function() {
210 expect(6);
211 var mapTestInput = {
212 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key',
213 'longValue': stringTest, stringTest: 'long key'
214 };
215
216 client.testStringMap(mapTestInput, function(result) {
217 for (var key in result) {
218 equal(result[key], mapTestInput[key]);
219 }
220 QUnit.start();
221 });
222 });
223
224 asyncTest('Set', function() {
225 expect(1);
226 var setTestInput = [1, 2, 3];
227 client.testSet(setTestInput, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100228 ok(result, setTestInput);
229 QUnit.start();
230 });
231 });
232
Kazuki Matsudab909a382016-02-13 19:36:09 +0900233 asyncTest('List', function() {
234 expect(1);
235 var listTestInput = [1, 2, 3];
236 client.testList(listTestInput, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100237 ok(result, listTestInput);
238 QUnit.start();
239 });
240 });
241
Kazuki Matsudab909a382016-02-13 19:36:09 +0900242 asyncTest('Enum', function() {
243 expect(1);
244 client.testEnum(ThriftTest.Numberz.ONE, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100245 equal(result, ThriftTest.Numberz.ONE);
246 QUnit.start();
247 });
248 });
249
Kazuki Matsudab909a382016-02-13 19:36:09 +0900250 asyncTest('TypeDef', function() {
251 expect(1);
252 client.testTypedef(69, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100253 equal(result, 69);
254 QUnit.start();
255 });
256 });
257
258
Kazuki Matsudab909a382016-02-13 19:36:09 +0900259module('deeper!');
henrique2a7dccc2014-03-07 22:16:51 +0100260
Kazuki Matsudab909a382016-02-13 19:36:09 +0900261 asyncTest('MapMap', function() {
262 expect(16);
henrique2a7dccc2014-03-07 22:16:51 +0100263 var mapMapTestExpectedResult = {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900264 '4': {'1': 1, '2': 2, '3': 3, '4': 4},
265 '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1}
henrique2a7dccc2014-03-07 22:16:51 +0100266 };
267
Kazuki Matsudab909a382016-02-13 19:36:09 +0900268 client.testMapMap(1, function(result) {
henrique2a7dccc2014-03-07 22:16:51 +0100269 for (var key in result) {
270 for (var key2 in result[key]) {
271 equal(result[key][key2], mapMapTestExpectedResult[key][key2]);
272 }
273 }
274 checkRecursively(result, mapMapTestExpectedResult);
275 QUnit.start();
276 });
277 });
278
279
Kazuki Matsudab909a382016-02-13 19:36:09 +0900280module('Exception');
henrique2a7dccc2014-03-07 22:16:51 +0100281
Kazuki Matsudab909a382016-02-13 19:36:09 +0900282 asyncTest('Xception', function() {
henrique2a7dccc2014-03-07 22:16:51 +0100283 expect(2);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900284 client.testException('Xception', function(e) {
henrique2a7dccc2014-03-07 22:16:51 +0100285 equal(e.errorCode, 1001);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900286 equal(e.message, 'Xception');
henrique2a7dccc2014-03-07 22:16:51 +0100287 QUnit.start();
288 });
289 });
290
Kazuki Matsudab909a382016-02-13 19:36:09 +0900291 asyncTest('no Exception', 0, function() {
292 expect(1);
293 client.testException('no Exception', function(e) {
henrique2a7dccc2014-03-07 22:16:51 +0100294 ok(!e);
295 QUnit.start();
296 });
297 });
298
Kazuki Matsudab909a382016-02-13 19:36:09 +0900299module('Insanity');
henrique2a7dccc2014-03-07 22:16:51 +0100300
Kazuki Matsudab909a382016-02-13 19:36:09 +0900301 asyncTest('testInsanity', function() {
302 expect(24);
henrique2a7dccc2014-03-07 22:16:51 +0100303 var insanity = {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900304 '1': {
305 '2': {
306 'userMap': { '5': 5, '8': 8 },
307 'xtructs': [{
308 'string_thing': 'Goodbye4',
309 'byte_thing': 4,
310 'i32_thing': 4,
311 'i64_thing': 4
henrique2a7dccc2014-03-07 22:16:51 +0100312 },
313 {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900314 'string_thing': 'Hello2',
315 'byte_thing': 2,
316 'i32_thing': 2,
317 'i64_thing': 2
henrique2a7dccc2014-03-07 22:16:51 +0100318 }
319 ]
320 },
Kazuki Matsudab909a382016-02-13 19:36:09 +0900321 '3': {
322 'userMap': { '5': 5, '8': 8 },
323 'xtructs': [{
324 'string_thing': 'Goodbye4',
325 'byte_thing': 4,
326 'i32_thing': 4,
327 'i64_thing': 4
henrique2a7dccc2014-03-07 22:16:51 +0100328 },
329 {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900330 'string_thing': 'Hello2',
331 'byte_thing': 2,
332 'i32_thing': 2,
333 'i64_thing': 2
henrique2a7dccc2014-03-07 22:16:51 +0100334 }
335 ]
336 }
337 },
Kazuki Matsudab909a382016-02-13 19:36:09 +0900338 '2': { '6': { 'userMap': null, 'xtructs': null } }
henrique2a7dccc2014-03-07 22:16:51 +0100339 };
Kazuki Matsudab909a382016-02-13 19:36:09 +0900340 client.testInsanity(new ThriftTest.Insanity(), function(res) {
henrique2a7dccc2014-03-07 22:16:51 +0100341 ok(res, JSON.stringify(res));
342 ok(insanity, JSON.stringify(insanity));
343 checkRecursively(res, insanity);
344 QUnit.start();
345 });
346 });
347
Philip Frank8fdd6102017-12-06 12:38:05 +0100348module('Oneway');
henrique2a7dccc2014-03-07 22:16:51 +0100349
Philip Frank8fdd6102017-12-06 12:38:05 +0100350 asyncTest('testOneway', function() {
351 expect(1);
352 client.testOneway(1, function(result) {
353 equal(result, undefined);
354 QUnit.start();
355 });
356 });