]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
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: | |
27 | * $ thrift -gen js ThriftTest.thrift | |
28 | */ | |
29 | ||
30 | ||
31 | ||
32 | // all Languages in UTF-8 | |
33 | const 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ú, 粵語"; | |
34 | ||
35 | function checkRecursively(assert, map1, map2) { | |
36 | if (typeof map1 !== 'function' && typeof map2 !== 'function') { | |
37 | if (!map1 || typeof map1 !== 'object') { | |
38 | assert.equal(map1, map2); | |
39 | } else { | |
40 | for (let key in map1) { | |
41 | checkRecursively(assert, map1[key], map2[key]); | |
42 | } | |
43 | } | |
44 | } | |
45 | } | |
46 | ||
47 | QUnit.module('Base Types'); | |
48 | ||
49 | QUnit.test('Void', function(assert) { | |
50 | assert.expect(1); | |
51 | const done = assert.async(); | |
52 | client.testVoid(function(result) { | |
53 | assert.equal(result, undefined); | |
54 | done(); | |
55 | }); | |
56 | }); | |
57 | ||
58 | ||
59 | QUnit.test('String', function(assert) { | |
60 | assert.expect(3); | |
61 | const done = assert.async(3); | |
62 | client.testString('', function(result) { | |
63 | assert.equal(result, ''); | |
64 | done(); | |
65 | }); | |
66 | client.testString(stringTest, function(result) { | |
67 | assert.equal(result, stringTest); | |
68 | done(); | |
69 | }); | |
70 | ||
71 | const specialCharacters = 'quote: \" backslash:' + | |
72 | ' forwardslash-escaped: \/ ' + | |
73 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + | |
74 | ' now-all-of-them-together: "\\\/\b\n\r\t' + | |
75 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; | |
76 | client.testString(specialCharacters, function(result) { | |
77 | assert.equal(result, specialCharacters); | |
78 | done(); | |
79 | }); | |
80 | }); | |
81 | QUnit.test('Double', function(assert) { | |
82 | assert.expect(4); | |
83 | const done = assert.async(4); | |
84 | client.testDouble(0, function(result) { | |
85 | assert.equal(result, 0); | |
86 | done(); | |
87 | }); | |
88 | client.testDouble(-1, function(result) { | |
89 | assert.equal(result, -1); | |
90 | done(); | |
91 | }); | |
92 | client.testDouble(3.14, function(result) { | |
93 | assert.equal(result, 3.14); | |
94 | done(); | |
95 | }); | |
96 | client.testDouble(Math.pow(2, 60), function(result) { | |
97 | assert.equal(result, Math.pow(2, 60)); | |
98 | done(); | |
99 | }); | |
100 | }); | |
101 | // TODO: add testBinary() | |
102 | QUnit.test('Byte', function(assert) { | |
103 | assert.expect(2); | |
104 | const done = assert.async(2); | |
105 | client.testByte(0, function(result) { | |
106 | assert.equal(result, 0); | |
107 | done(); | |
108 | }); | |
109 | client.testByte(0x01, function(result) { | |
110 | assert.equal(result, 0x01); | |
111 | done(); | |
112 | }); | |
113 | }); | |
114 | QUnit.test('I32', function(assert) { | |
115 | assert.expect(3); | |
116 | const done = assert.async(3); | |
117 | client.testI32(0, function(result) { | |
118 | assert.equal(result, 0); | |
119 | done(); | |
120 | }); | |
121 | client.testI32(Math.pow(2, 30), function(result) { | |
122 | assert.equal(result, Math.pow(2, 30)); | |
123 | done(); | |
124 | }); | |
125 | client.testI32(-Math.pow(2, 30), function(result) { | |
126 | assert.equal(result, -Math.pow(2, 30)); | |
127 | done(); | |
128 | }); | |
129 | }); | |
130 | QUnit.test('I64', function(assert) { | |
131 | assert.expect(3); | |
132 | const done = assert.async(3); | |
133 | client.testI64(0, function(result) { | |
134 | assert.equal(result, 0); | |
135 | done(); | |
136 | }); | |
137 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately | |
138 | client.testI64(Math.pow(2, 52), function(result) { | |
139 | assert.equal(result, Math.pow(2, 52)); | |
140 | done(); | |
141 | }); | |
142 | client.testI64(-Math.pow(2, 52), function(result) { | |
143 | assert.equal(result, -Math.pow(2, 52)); | |
144 | done(); | |
145 | }); | |
146 | }); | |
147 | ||
148 | ||
149 | ||
150 | ||
151 | QUnit.module('Structured Types'); | |
152 | ||
153 | QUnit.test('Struct', function(assert) { | |
154 | assert.expect(5); | |
155 | const done = assert.async(); | |
156 | const structTestInput = new ThriftTest.Xtruct(); | |
157 | structTestInput.string_thing = 'worked'; | |
158 | structTestInput.byte_thing = 0x01; | |
159 | structTestInput.i32_thing = Math.pow(2, 30); | |
160 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately | |
161 | structTestInput.i64_thing = Math.pow(2, 52); | |
162 | ||
163 | client.testStruct(structTestInput, function(result) { | |
164 | assert.equal(result.string_thing, structTestInput.string_thing); | |
165 | assert.equal(result.byte_thing, structTestInput.byte_thing); | |
166 | assert.equal(result.i32_thing, structTestInput.i32_thing); | |
167 | assert.equal(result.i64_thing, structTestInput.i64_thing); | |
168 | assert.equal(JSON.stringify(result), JSON.stringify(structTestInput)); | |
169 | done(); | |
170 | }); | |
171 | }); | |
172 | ||
173 | QUnit.test('Nest', function(assert) { | |
174 | assert.expect(7); | |
175 | const done = assert.async(); | |
176 | const xtrTestInput = new ThriftTest.Xtruct(); | |
177 | xtrTestInput.string_thing = 'worked'; | |
178 | xtrTestInput.byte_thing = 0x01; | |
179 | xtrTestInput.i32_thing = Math.pow(2, 30); | |
180 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately | |
181 | xtrTestInput.i64_thing = Math.pow(2, 52); | |
182 | ||
183 | const nestTestInput = new ThriftTest.Xtruct2(); | |
184 | nestTestInput.byte_thing = 0x02; | |
185 | nestTestInput.struct_thing = xtrTestInput; | |
186 | nestTestInput.i32_thing = Math.pow(2, 15); | |
187 | ||
188 | client.testNest(nestTestInput, function(result) { | |
189 | assert.equal(result.byte_thing, nestTestInput.byte_thing); | |
190 | assert.equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing); | |
191 | assert.equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing); | |
192 | assert.equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing); | |
193 | assert.equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing); | |
194 | assert.equal(result.i32_thing, nestTestInput.i32_thing); | |
195 | assert.equal(JSON.stringify(result), JSON.stringify(nestTestInput)); | |
196 | done(); | |
197 | }); | |
198 | }); | |
199 | ||
200 | QUnit.test('Map', function(assert) { | |
201 | assert.expect(3); | |
202 | const done = assert.async(); | |
203 | const mapTestInput = {7: 77, 8: 88, 9: 99}; | |
204 | ||
205 | client.testMap(mapTestInput, function(result) { | |
206 | for (let key in result) { | |
207 | assert.equal(result[key], mapTestInput[key]); | |
208 | } | |
209 | done(); | |
210 | }); | |
211 | }); | |
212 | ||
213 | QUnit.test('StringMap', function(assert) { | |
214 | assert.expect(6); | |
215 | const done = assert.async(); | |
216 | const mapTestInput = { | |
217 | 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key', | |
218 | 'longValue': stringTest, stringTest: 'long key' | |
219 | }; | |
220 | ||
221 | client.testStringMap(mapTestInput, function(result) { | |
222 | for (let key in result) { | |
223 | assert.equal(result[key], mapTestInput[key]); | |
224 | } | |
225 | done(); | |
226 | }); | |
227 | }); | |
228 | ||
229 | QUnit.test('Set', function(assert) { | |
230 | assert.expect(1); | |
231 | const done = assert.async(); | |
232 | const setTestInput = [1, 2, 3]; | |
233 | client.testSet(setTestInput, function(result) { | |
234 | assert.ok(result, setTestInput); | |
235 | done(); | |
236 | }); | |
237 | }); | |
238 | ||
239 | QUnit.test('List', function(assert) { | |
240 | assert.expect(1); | |
241 | const done = assert.async(); | |
242 | const listTestInput = [1, 2, 3]; | |
243 | client.testList(listTestInput, function(result) { | |
244 | assert.ok(result, listTestInput); | |
245 | done(); | |
246 | }); | |
247 | }); | |
248 | ||
249 | QUnit.test('Enum', function(assert) { | |
250 | assert.expect(1); | |
251 | const done = assert.async(); | |
252 | client.testEnum(ThriftTest.Numberz.ONE, function(result) { | |
253 | assert.equal(result, ThriftTest.Numberz.ONE); | |
254 | done(); | |
255 | }); | |
256 | }); | |
257 | ||
258 | QUnit.test('TypeDef', function(assert) { | |
259 | assert.expect(1); | |
260 | const done = assert.async(); | |
261 | client.testTypedef(69, function(result) { | |
262 | assert.equal(result, 69); | |
263 | done(); | |
264 | }); | |
265 | }); | |
266 | ||
267 | ||
268 | QUnit.module('deeper!'); | |
269 | ||
270 | QUnit.test('MapMap', function(assert) { | |
271 | assert.expect(16); | |
272 | const done = assert.async(); | |
273 | const mapMapTestExpectedResult = { | |
274 | '4': {'1': 1, '2': 2, '3': 3, '4': 4}, | |
275 | '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1} | |
276 | }; | |
277 | ||
278 | client.testMapMap(1, function(result) { | |
279 | for (let key in result) { | |
280 | for (let key2 in result[key]) { | |
281 | assert.equal(result[key][key2], mapMapTestExpectedResult[key][key2]); | |
282 | } | |
283 | } | |
284 | checkRecursively(assert, result, mapMapTestExpectedResult); | |
285 | done(); | |
286 | }); | |
287 | }); | |
288 | ||
289 | ||
290 | QUnit.module('Exception'); | |
291 | ||
292 | QUnit.test('Xception', function(assert) { | |
293 | assert.expect(2); | |
294 | const done = assert.async(); | |
295 | client.testException('Xception', function(e) { | |
296 | assert.equal(e.errorCode, 1001); | |
297 | assert.equal(e.message, 'Xception'); | |
298 | done(); | |
299 | }); | |
300 | }); | |
301 | ||
302 | QUnit.test('no Exception', function(assert) { | |
303 | assert.expect(1); | |
304 | const done = assert.async(); | |
305 | client.testException('no Exception', function(e) { | |
306 | assert.ok(!e); | |
307 | done(); | |
308 | }); | |
309 | }); | |
310 | ||
311 | QUnit.module('Insanity'); | |
312 | ||
313 | QUnit.test('testInsanity', function(assert) { | |
314 | assert.expect(24); | |
315 | const done = assert.async(); | |
316 | const insanity = { | |
317 | '1': { | |
318 | '2': { | |
319 | 'userMap': { '5': 5, '8': 8 }, | |
320 | 'xtructs': [{ | |
321 | 'string_thing': 'Goodbye4', | |
322 | 'byte_thing': 4, | |
323 | 'i32_thing': 4, | |
324 | 'i64_thing': 4 | |
325 | }, | |
326 | { | |
327 | 'string_thing': 'Hello2', | |
328 | 'byte_thing': 2, | |
329 | 'i32_thing': 2, | |
330 | 'i64_thing': 2 | |
331 | } | |
332 | ] | |
333 | }, | |
334 | '3': { | |
335 | 'userMap': { '5': 5, '8': 8 }, | |
336 | 'xtructs': [{ | |
337 | 'string_thing': 'Goodbye4', | |
338 | 'byte_thing': 4, | |
339 | 'i32_thing': 4, | |
340 | 'i64_thing': 4 | |
341 | }, | |
342 | { | |
343 | 'string_thing': 'Hello2', | |
344 | 'byte_thing': 2, | |
345 | 'i32_thing': 2, | |
346 | 'i64_thing': 2 | |
347 | } | |
348 | ] | |
349 | } | |
350 | }, | |
351 | '2': { '6': { 'userMap': null, 'xtructs': null } } | |
352 | }; | |
353 | client.testInsanity(new ThriftTest.Insanity(), function(res) { | |
354 | assert.ok(res, JSON.stringify(res)); | |
355 | assert.ok(insanity, JSON.stringify(insanity)); | |
356 | checkRecursively(assert, res, insanity); | |
357 | done(); | |
358 | }); | |
359 | }); | |
360 | ||
361 | QUnit.module('Oneway'); | |
362 | ||
363 | QUnit.test('testOneway', function(assert) { | |
364 | assert.expect(1); | |
365 | const done = assert.async(); | |
366 | client.testOneway(1, function(result) { | |
367 | assert.equal(result, undefined); | |
368 | done(); | |
369 | }); | |
370 | }); |