blob: 845171b8655b5b1da5ab26a81a6c78932354277a [file] [log] [blame]
Philip Frank8fdd6102017-12-06 12:38:05 +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 */
20
21/*
22 * Fully Async JavaScript test suite for ThriftTest.thrift.
23 * These tests are designed to exercise the WebSocket transport
24 * (which is exclusively async).
25 *
26 * To compile client code for this test use:
Brian Forbisb5d6ea32018-08-25 23:39:29 -040027 * $ thrift -gen js:es6 ThriftTest.thrift
Philip Frank8fdd6102017-12-06 12:38:05 +010028 */
29
30
31
32// all Languages in UTF-8
Philip Frank8fdd6102017-12-06 12:38:05 +010033
Brian Forbisb5d6ea32018-08-25 23:39:29 -040034const 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ú, 粵語";
35
36function checkRecursively(assert, map1, map2) {
Philip Frank8fdd6102017-12-06 12:38:05 +010037 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
38 if (!map1 || typeof map1 !== 'object') {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040039 assert.equal(map1, map2);
Philip Frank8fdd6102017-12-06 12:38:05 +010040 } else {
41 for (var key in map1) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040042 checkRecursively(assert, map1[key], map2[key]);
Philip Frank8fdd6102017-12-06 12:38:05 +010043 }
44 }
45 }
46}
47
Brian Forbisb5d6ea32018-08-25 23:39:29 -040048QUnit.module('Base Types');
Philip Frank8fdd6102017-12-06 12:38:05 +010049
Brian Forbisb5d6ea32018-08-25 23:39:29 -040050 QUnit.test('Void', function( assert ) {
51 assert.expect(1);
52 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +010053 client.testVoid().then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040054 assert.equal(result, undefined);
55 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010056 });
57 });
58
Brian Forbisb5d6ea32018-08-25 23:39:29 -040059 QUnit.test('String', function( assert ) {
60 assert.expect(3);
61 const done = assert.async(3);
Philip Frank8fdd6102017-12-06 12:38:05 +010062 client.testString('').then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040063 assert.equal(result, '');
64 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010065 });
66 client.testString(stringTest).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040067 assert.equal(result, stringTest);
68 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010069 });
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: !@#$%&()(&%$#{}{}<><><';
75 client.testString(specialCharacters).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040076 assert.equal(result, specialCharacters);
77 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010078 });
79 });
80
Brian Forbisb5d6ea32018-08-25 23:39:29 -040081 QUnit.test('Double', function( assert ) {
82 assert.expect(4);
83 const done = assert.async(4);
Philip Frank8fdd6102017-12-06 12:38:05 +010084 client.testDouble(0).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040085 assert.equal(result, 0);
86 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010087 });
88 client.testDouble(-1).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040089 assert.equal(result, -1);
90 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010091 });
92 client.testDouble(3.14).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040093 assert.equal(result, 3.14);
94 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010095 });
96 client.testDouble(Math.pow(2, 60)).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040097 assert.equal(result, Math.pow(2, 60));
98 done();
Philip Frank8fdd6102017-12-06 12:38:05 +010099 });
100 });
101 // TODO: add testBinary()
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400102 QUnit.test('Byte', function( assert ) {
103 assert.expect(2);
104 const done = assert.async(2);
Philip Frank8fdd6102017-12-06 12:38:05 +0100105 client.testByte(0).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400106 assert.equal(result, 0);
107 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100108 });
109 client.testByte(0x01).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400110 assert.equal(result, 0x01);
111 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100112 });
113 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400114 QUnit.test('I32', function( assert ) {
115 assert.expect(3);
116 const done = assert.async(3);
Philip Frank8fdd6102017-12-06 12:38:05 +0100117 client.testI32(0).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400118 assert.equal(result, 0);
119 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100120 });
121 client.testI32(Math.pow(2, 30)).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400122 assert.equal(result, Math.pow(2, 30));
123 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100124 });
125 client.testI32(-Math.pow(2, 30)).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400126 assert.equal(result, -Math.pow(2, 30));
127 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100128 });
129 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400130 QUnit.test('I64', function( assert ) {
131 assert.expect(3);
132 const done = assert.async(3);
Philip Frank8fdd6102017-12-06 12:38:05 +0100133 client.testI64(0).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400134 assert.equal(result, 0);
135 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100136 });
137 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
138 client.testI64(Math.pow(2, 52)).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400139 assert.equal(result, Math.pow(2, 52));
140 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100141 });
142 client.testI64(-Math.pow(2, 52)).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400143 assert.equal(result, -Math.pow(2, 52));
144 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100145 });
146 });
147
148
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400149QUnit.module('Structured Types');
Philip Frank8fdd6102017-12-06 12:38:05 +0100150
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400151 QUnit.test('Struct', function( assert ) {
152 assert.expect(5);
153 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100154 var structTestInput = new ThriftTest.Xtruct();
155 structTestInput.string_thing = 'worked';
156 structTestInput.byte_thing = 0x01;
157 structTestInput.i32_thing = Math.pow(2, 30);
158 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
159 structTestInput.i64_thing = Math.pow(2, 52);
160
161 client.testStruct(structTestInput).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400162 assert.equal(result.string_thing, structTestInput.string_thing);
163 assert.equal(result.byte_thing, structTestInput.byte_thing);
164 assert.equal(result.i32_thing, structTestInput.i32_thing);
165 assert.equal(result.i64_thing, structTestInput.i64_thing);
166 assert.equal(JSON.stringify(result), JSON.stringify(structTestInput));
167 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100168 });
169 });
170
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400171 QUnit.test('Nest', function( assert ) {
172 assert.expect(7);
173 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100174 var xtrTestInput = new ThriftTest.Xtruct();
175 xtrTestInput.string_thing = 'worked';
176 xtrTestInput.byte_thing = 0x01;
177 xtrTestInput.i32_thing = Math.pow(2, 30);
178 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
179 xtrTestInput.i64_thing = Math.pow(2, 52);
180
181 var nestTestInput = new ThriftTest.Xtruct2();
182 nestTestInput.byte_thing = 0x02;
183 nestTestInput.struct_thing = xtrTestInput;
184 nestTestInput.i32_thing = Math.pow(2, 15);
185
186 client.testNest(nestTestInput).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400187 assert.equal(result.byte_thing, nestTestInput.byte_thing);
188 assert.equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
189 assert.equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
190 assert.equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
191 assert.equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
192 assert.equal(result.i32_thing, nestTestInput.i32_thing);
193 assert.equal(JSON.stringify(result), JSON.stringify(nestTestInput));
194 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100195 });
196 });
197
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400198 QUnit.test('Map', function( assert ) {
199 assert.expect(3);
200 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100201 var mapTestInput = {7: 77, 8: 88, 9: 99};
202
203 client.testMap(mapTestInput).then(function(result) {
204 for (var key in result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400205 assert.equal(result[key], mapTestInput[key]);
Philip Frank8fdd6102017-12-06 12:38:05 +0100206 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400207 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100208 });
209 });
210
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400211 QUnit.test('StringMap', function( assert ) {
212 assert.expect(6);
213 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100214 var mapTestInput = {
215 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key',
216 'longValue': stringTest, stringTest: 'long key'
217 };
218
219 client.testStringMap(mapTestInput).then(function(result) {
220 for (var key in result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400221 assert.equal(result[key], mapTestInput[key]);
Philip Frank8fdd6102017-12-06 12:38:05 +0100222 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400223 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100224 });
225 });
226
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400227 QUnit.test('Set', function( assert ) {
228 assert.expect(1);
229 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100230 var setTestInput = [1, 2, 3];
231 client.testSet(setTestInput).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400232 assert.ok(result, setTestInput);
233 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100234 });
235 });
236
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400237 QUnit.test('List', function( assert ) {
238 assert.expect(1);
239 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100240 var listTestInput = [1, 2, 3];
241 client.testList(listTestInput).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400242 assert.ok(result, listTestInput);
243 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100244 });
245 });
246
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400247 QUnit.test('Enum', function( assert ) {
248 assert.expect(1);
249 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100250 client.testEnum(ThriftTest.Numberz.ONE).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400251 assert.equal(result, ThriftTest.Numberz.ONE);
252 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100253 });
254 });
255
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400256 QUnit.test('TypeDef', function( assert ) {
257 assert.expect(1);
258 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100259 client.testTypedef(69).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400260 assert.equal(result, 69);
261 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100262 });
263 });
264
265
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400266QUnit.module('deeper!');
Philip Frank8fdd6102017-12-06 12:38:05 +0100267
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400268 QUnit.test('MapMap', function( assert ) {
269 assert.expect(16);
270 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100271 var mapMapTestExpectedResult = {
272 '4': {'1': 1, '2': 2, '3': 3, '4': 4},
273 '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1}
274 };
275
276 client.testMapMap(1).then(function(result) {
277 for (var key in result) {
278 for (var key2 in result[key]) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400279 assert.equal(result[key][key2], mapMapTestExpectedResult[key][key2]);
Philip Frank8fdd6102017-12-06 12:38:05 +0100280 }
281 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400282 checkRecursively(assert, result, mapMapTestExpectedResult);
283 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100284 });
285 });
286
287
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400288QUnit.module('Exception');
Philip Frank8fdd6102017-12-06 12:38:05 +0100289
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400290 QUnit.test('Xception', function( assert ) {
291 assert.expect(2);
292 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100293 client.testException('Xception').then(function(res) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400294 assert.ok(false);
Philip Frank8fdd6102017-12-06 12:38:05 +0100295 }).catch(function(e) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400296
297 console.log(`Exception exception e`);
298 console.log(e);
299 console.log(JSON.stringify(e, null, 2));
300
301 assert.equal(e.errorCode, 1001);
302 assert.equal(e.message, 'Xception');
303 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100304 });
305 });
306
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400307 QUnit.test('no Exception', function( assert ) {
308 assert.expect(1);
309 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100310 client.testException('no Exception').then(function(e) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400311 assert.ok(!e);
312 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100313 });
314 });
315
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400316QUnit.module('Insanity');
Philip Frank8fdd6102017-12-06 12:38:05 +0100317
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400318 QUnit.test('testInsanity', function( assert ) {
319 assert.expect(24);
320 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100321 var insanity = {
322 '1': {
323 '2': {
324 'userMap': { '5': 5, '8': 8 },
325 'xtructs': [{
326 'string_thing': 'Goodbye4',
327 'byte_thing': 4,
328 'i32_thing': 4,
329 'i64_thing': 4
330 },
331 {
332 'string_thing': 'Hello2',
333 'byte_thing': 2,
334 'i32_thing': 2,
335 'i64_thing': 2
336 }
337 ]
338 },
339 '3': {
340 'userMap': { '5': 5, '8': 8 },
341 'xtructs': [{
342 'string_thing': 'Goodbye4',
343 'byte_thing': 4,
344 'i32_thing': 4,
345 'i64_thing': 4
346 },
347 {
348 'string_thing': 'Hello2',
349 'byte_thing': 2,
350 'i32_thing': 2,
351 'i64_thing': 2
352 }
353 ]
354 }
355 },
356 '2': { '6': { 'userMap': null, 'xtructs': null } }
357 };
358 client.testInsanity(new ThriftTest.Insanity()).then(function(res) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400359 assert.ok(res, JSON.stringify(res));
360 assert.ok(insanity, JSON.stringify(insanity));
361 checkRecursively(assert, res, insanity);
362 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100363 });
364 });
365
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400366QUnit.module('Oneway');
367 QUnit.test('testOneway', function( assert ) {
368 assert.expect(1);
369 const done = assert.async();
Philip Frank8fdd6102017-12-06 12:38:05 +0100370 client.testOneway(1).then(function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400371 assert.equal(result, undefined);
372 done();
Philip Frank8fdd6102017-12-06 12:38:05 +0100373 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400374 });