blob: 91ba4d1089b725565a46670155cce6f1bb6b1680 [file] [log] [blame]
Jens Geyerb9d55222014-01-10 21:26:25 +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 * JavaScript test suite
23 */
24
25var transport = new Thrift.Transport("/service");
26var protocol = new Thrift.Protocol(transport);
27var client = new ThriftTest.ThriftTestClient(protocol);
28
29// all Languages in UTF-8
30var 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ú, 粵語";
31
32function checkRecursively(map1, map2) {
33 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
34 if (!map1 || typeof map1 !== 'object') {
35 equal(map1, map2);
36 } else {
37 for (var key in map1) {
38 checkRecursively(map1[key], map2[key]);
39 }
40 }
41 }
42}
43
44module("Base Types");
45
46 test("Void", function() {
47 equal(client.testVoid(), undefined);
48 });
49 test("String", function() {
50 equal(client.testString(''), '');
51 equal(client.testString(stringTest), stringTest);
52
53 var specialCharacters = 'quote: \" backslash:' +
54 ' forwardslash-escaped: \/ ' +
55 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
56 ' now-all-of-them-together: "\\\/\b\n\r\t' +
57 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
58 equal(client.testString(specialCharacters),specialCharacters);
59 });
60 test("Double", function() {
61 equal(client.testDouble(0), 0);
62 equal(client.testDouble(-1), -1);
63 equal(client.testDouble(3.14), 3.14);
64 equal(client.testDouble(Math.pow(2,60)), Math.pow(2,60));
65 });
66 test("Byte", function() {
67 equal(client.testByte(0), 0);
68 equal(client.testByte(0x01), 0x01);
69 });
70 test("I32", function() {
71 equal(client.testI32(0), 0);
72 equal(client.testI32(Math.pow(2,30)), Math.pow(2,30));
73 equal(client.testI32(-Math.pow(2,30)), -Math.pow(2,30));
74 });
75 test("I64", function() {
76 equal(client.testI64(0), 0);
77 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
78 equal(client.testI64(Math.pow(2,52)), Math.pow(2,52));
79 equal(client.testI64(-Math.pow(2,52)), -Math.pow(2,52));
80 });
81
82
83module("Structured Types");
84
85 test("Struct", function() {
86 var structTestInput = new ThriftTest.Xtruct();
87 structTestInput.string_thing = 'worked';
88 structTestInput.byte_thing = 0x01;
89 structTestInput.i32_thing = Math.pow(2,30);
90 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
91 structTestInput.i64_thing = Math.pow(2,52);
92
93 var structTestOutput = client.testStruct(structTestInput);
94
95 equal(structTestOutput.string_thing, structTestInput.string_thing);
96 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
97 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
98 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
99
100 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
101 });
102
103 test("Nest", function() {
104 var xtrTestInput = new ThriftTest.Xtruct();
105 xtrTestInput.string_thing = 'worked';
106 xtrTestInput.byte_thing = 0x01;
107 xtrTestInput.i32_thing = Math.pow(2,30);
108 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
109 xtrTestInput.i64_thing = Math.pow(2,52);
110
111 var nestTestInput = new ThriftTest.Xtruct2();
112 nestTestInput.byte_thing = 0x02;
113 nestTestInput.struct_thing = xtrTestInput;
114 nestTestInput.i32_thing = Math.pow(2,15);
115
116 var nestTestOutput = client.testNest(nestTestInput);
117
118 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
119 equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
120 equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
121 equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
122 equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
123 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
124
125 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
126 });
127
128 test("Map", function() {
129 var mapTestInput = {7:77, 8:88, 9:99};
130
131 var mapTestOutput = client.testMap(mapTestInput);
132
133 for (var key in mapTestOutput) {
134 equal(mapTestOutput[key], mapTestInput[key]);
135 }
136 });
137
138 test("StringMap", function() {
139 var mapTestInput = {
140 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
141 "longValue":stringTest, stringTest:"long key"
142 };
143
144 var mapTestOutput = client.testStringMap(mapTestInput);
145
146 for (var key in mapTestOutput) {
147 equal(mapTestOutput[key], mapTestInput[key]);
148 }
149 });
150
151 test("Set", function() {
152 var setTestInput = [1,2,3];
153 ok(client.testSet(setTestInput), setTestInput);
154 });
155
156 test("List", function() {
157 var listTestInput = [1,2,3];
158 ok(client.testList(listTestInput), listTestInput);
159 });
160
161 test("Enum", function() {
162 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
163 });
164
165 test("TypeDef", function() {
166 equal(client.testTypedef(69), 69);
167 });
168
169
170module("deeper!");
171
172 test("MapMap", function() {
173 var mapMapTestExpectedResult = {
174 "4":{"1":1,"2":2,"3":3,"4":4},
175 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
176 };
177
178 var mapMapTestOutput = client.testMapMap(1);
179
180
181 for (var key in mapMapTestOutput) {
182 for (var key2 in mapMapTestOutput[key]) {
183 equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
184 }
185 }
186
187 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
188 });
189
190
191module("Exception");
192
193 test("Xception", function() {
194 expect(2);
195 try{
196 client.testException("Xception");
197 }catch(e){
198 equal(e.errorCode, 1001);
199 equal(e.message, "Xception");
200 }
201 });
202
203 test("no Exception", 0, function() {
204 try{
205 client.testException("no Exception");
206 }catch(e){
207 ok(false);
208 }
209 });
210
211 test("TException", function() {
212 //ThriftTest does not list TException as a legal exception so it will
213 // generate an exception on the server that does not propagate back to
214 // the client. This test has been modified to equate to "no exception"
215 expect(1);
216 try{
217 client.testException("TException");
218 } catch(e) {
219 //ok(false);
220 }
221 ok(true);
222 });
223
224
225module("Insanity");
226
227 test("testInsanity", function() {
228 var insanity = {
229 "1":{
230 "2":{
231 "userMap":{ "5":5, "8":8 },
232 "xtructs":[{
233 "string_thing":"Goodbye4",
234 "byte_thing":4,
235 "i32_thing":4,
236 "i64_thing":4
237 },
238 {
239 "string_thing":"Hello2",
240 "byte_thing":2,
241 "i32_thing":2,
242 "i64_thing":2
243 }
244 ]
245 },
246 "3":{
247 "userMap":{ "5":5, "8":8 },
248 "xtructs":[{
249 "string_thing":"Goodbye4",
250 "byte_thing":4,
251 "i32_thing":4,
252 "i64_thing":4
253 },
254 {
255 "string_thing":"Hello2",
256 "byte_thing":2,
257 "i32_thing":2,
258 "i64_thing":2
259 }
260 ]
261 }
262 },
263 "2":{ "6":{ "userMap":null, "xtructs":null } }
264 };
265 var res = client.testInsanity(new ThriftTest.Insanity());
266 ok(res, JSON.stringify(res));
267 ok(insanity, JSON.stringify(insanity));
268
269 checkRecursively(res, insanity);
270 });
271
272
273//////////////////////////////////
274//Run same tests asynchronously
275jQuery.ajaxSetup({ timeout: 0 });
276$(document).ajaxError( function() { QUnit.start(); } );
277
278module("Async Manual");
279
280 test("testI32", function() {
281 expect( 2 );
282 QUnit.stop();
283
284 var transport = new Thrift.Transport();
285 var protocol = new Thrift.Protocol(transport);
286 var client = new ThriftTest.ThriftTestClient(protocol);
287
288 var jqxhr = jQuery.ajax({
289 url: "/service",
290 data: client.send_testI32(Math.pow(-2,31)),
291 type: "POST",
292 cache: false,
293 dataType: "text",
294 success: function(res){
295 transport.setRecvBuffer( res );
296 equal(client.recv_testI32(), Math.pow(-2,31));
297 },
298 error: function() { ok(false); },
299 complete: function() {
300 ok(true);
301 QUnit.start();
302 }
303 });
304 });
305
306
307 test("testI64", function() {
308 expect( 2 );
309 QUnit.stop();
310
311 var transport = new Thrift.Transport();
312 var protocol = new Thrift.Protocol(transport);
313 var client = new ThriftTest.ThriftTestClient(protocol);
314
315 jQuery.ajax({
316 url: "/service",
317 //This is usually 2^61 but JS cannot represent anything over 2^52 accurately
318 data: client.send_testI64(Math.pow(-2,52)),
319 type: "POST",
320 cache: false,
321 dataType: "text",
322 success: function(res){
323 transport.setRecvBuffer( res );
324 //This is usually 2^61 but JS cannot represent anything over 2^52 accurately
325 equal(client.recv_testI64(), Math.pow(-2,52));
326 },
327 error: function() { ok(false); },
328 complete: function() {
329 ok(true);
330 QUnit.start();
331 }
332 });
333 });
334
335
336module("Async");
337
338 test("Double", function() {
339 expect( 1 );
340
341 QUnit.stop();
342 client.testDouble(3.14159265, function(result) {
343 equal(result, 3.14159265);
344 QUnit.start();
345 });
346 });
347
348 test("Byte", function() {
349 expect( 1 );
350
351 QUnit.stop();
352 client.testByte(0x01, function(result) {
353 equal(result, 0x01);
354 QUnit.start();
355 });
356 });
357
358 test("I32", function() {
359 expect( 3 );
360
361 QUnit.stop();
362 client.testI32(Math.pow(2,30), function(result) {
363 equal(result, Math.pow(2,30));
364 QUnit.start();
365 });
366
367 QUnit.stop();
368 var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
369 equal(result, Math.pow(-2,31));
370 });
371
372 jqxhr.success(function(result) {
373 equal(result, Math.pow(-2,31));
374 QUnit.start();
375 });
376 });
377
378 test("I64", function() {
379 expect( 4 );
380
381 QUnit.stop();
382 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
383 client.testI64(Math.pow(2,52), function(result) {
384 equal(result, Math.pow(2,52));
385 QUnit.start();
386 });
387
388 QUnit.stop();
389 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
390 client.testI64(Math.pow(-2,52), function(result) {
391 equal(result, Math.pow(-2,52));
392 })
393 .error( function(xhr, status, e) { ok(false, e.message); } )
394 .success(function(result) {
395 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
396 equal(result, Math.pow(-2,52));
397 })
398 .complete(function() {
399 ok(true);
400 QUnit.start();
401 });
402 });
403
404 test("Xception", function() {
405 expect( 2 );
406
407 QUnit.stop();
408
409 var dfd = client.testException("Xception", function(result) {
410 ok(false);
411 QUnit.start();
412 })
413 .error(function(xhr, status, e){
414 equal(e.errorCode, 1001);
415 equal(e.message, "Xception");
416 //QUnit.start();
417 //Note start is not required here because:
418 //$(document).ajaxError( function() { QUnit.start(); } );
419 });
420 });