blob: c30287fd5b0473de516ec8dc528780979556ba41 [file] [log] [blame]
Roger Meieredf0d1d2011-08-03 21:21:43 +00001/*
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 */
19
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ú, 粵語";
Roger Meier4f8a5232011-09-09 08:17:02 +000031
32function checkRecursively(map1, map2) {
33 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
34 if (!map1 || typeof map1 !== 'object') {
Roger Meierb661e832012-04-15 15:58:43 +000035 equal(map1, map2);
Roger Meier4f8a5232011-09-09 08:17:02 +000036 } else {
37 for (var key in map1) {
38 checkRecursively(map1[key], map2[key]);
39 }
40 }
41 }
42}
Roger Meierbb267d42011-07-30 15:10:42 +000043
44module("Base Types");
45
46 test("Void", function() {
Roger Meierb661e832012-04-15 15:58:43 +000047 equal(client.testVoid(), undefined);
Roger Meierbb267d42011-07-30 15:10:42 +000048 });
49 test("String", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000050 equal(client.testString(''), '');
Roger Meierb661e832012-04-15 15:58:43 +000051 equal(client.testString(stringTest), stringTest);
Roger Meierbb267d42011-07-30 15:10:42 +000052
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: !@#$%&()(&%$#{}{}<><><';
Roger Meierb661e832012-04-15 15:58:43 +000058 equal(client.testString(specialCharacters),specialCharacters);
Roger Meierbb267d42011-07-30 15:10:42 +000059 });
60 test("Double", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000061 equal(client.testDouble(0), 0);
62 equal(client.testDouble(-1), -1);
Roger Meierb661e832012-04-15 15:58:43 +000063 equal(client.testDouble(3.14), 3.14);
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000064 equal(client.testDouble(Math.pow(2,60)), Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +000065 });
66 test("Byte", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000067 equal(client.testByte(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000068 equal(client.testByte(0x01), 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +000069 });
70 test("I32", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000071 equal(client.testI32(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000072 equal(client.testI32(Math.pow(2,30)), Math.pow(2,30));
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000073 equal(client.testI32(-Math.pow(2,30)), -Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +000074 });
75 test("I64", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000076 equal(client.testI64(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000077 equal(client.testI64(Math.pow(2,60)), Math.pow(2,60));
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000078 equal(client.testI64(-Math.pow(2,60)), -Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +000079 });
80
81
82module("Structured Types");
83
84 test("Struct", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +000085 var structTestInput = new ThriftTest.Xtruct();
86 structTestInput.string_thing = 'worked';
87 structTestInput.byte_thing = 0x01;
88 structTestInput.i32_thing = Math.pow(2,30);
89 structTestInput.i64_thing = Math.pow(2,60);
Roger Meierbb267d42011-07-30 15:10:42 +000090
91 var structTestOutput = client.testStruct(structTestInput);
92
Roger Meierb661e832012-04-15 15:58:43 +000093 equal(structTestOutput.string_thing, structTestInput.string_thing);
94 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
95 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
96 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Roger Meierbb267d42011-07-30 15:10:42 +000097
Roger Meierb661e832012-04-15 15:58:43 +000098 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +000099 });
100
101 test("Nest", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000102 var xtrTestInput = new ThriftTest.Xtruct();
103 xtrTestInput.string_thing = 'worked';
104 xtrTestInput.byte_thing = 0x01;
105 xtrTestInput.i32_thing = Math.pow(2,30);
106 xtrTestInput.i64_thing = Math.pow(2,60);
Roger Meierbb267d42011-07-30 15:10:42 +0000107
Roger Meieredf0d1d2011-08-03 21:21:43 +0000108 var nestTestInput = new ThriftTest.Xtruct2();
109 nestTestInput.byte_thing = 0x02;
110 nestTestInput.struct_thing = xtrTestInput;
111 nestTestInput.i32_thing = Math.pow(2,15);
Roger Meierbb267d42011-07-30 15:10:42 +0000112
113 var nestTestOutput = client.testNest(nestTestInput);
114
Roger Meierb661e832012-04-15 15:58:43 +0000115 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
116 equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
117 equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
118 equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
119 equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
120 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Roger Meierbb267d42011-07-30 15:10:42 +0000121
Roger Meierb661e832012-04-15 15:58:43 +0000122 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000123 });
124
125 test("Map", function() {
126 var mapTestInput = {7:77, 8:88, 9:99};
127
Roger Meieredf0d1d2011-08-03 21:21:43 +0000128 var mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000129
130 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000131 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000132 }
133 });
134
135 test("StringMap", function() {
136 var mapTestInput = {
137 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
138 "longValue":stringTest, stringTest:"long key"
139 };
140
Roger Meieredf0d1d2011-08-03 21:21:43 +0000141 var mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000142
143 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000144 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000145 }
146 });
147
148 test("Set", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000149 var setTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000150 ok(client.testSet(setTestInput), setTestInput);
151 });
152
153 test("List", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000154 var listTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000155 ok(client.testList(listTestInput), listTestInput);
156 });
157
158 test("Enum", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000159 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
Roger Meierbb267d42011-07-30 15:10:42 +0000160 });
161
162 test("TypeDef", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000163 equal(client.testTypedef(69), 69);
Roger Meierbb267d42011-07-30 15:10:42 +0000164 });
165
166
167module("deeper!");
168
169 test("MapMap", function() {
170 var mapMapTestExpectedResult = {
171 "4":{"1":1,"2":2,"3":3,"4":4},
172 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
173 };
174
175 var mapMapTestOutput = client.testMapMap(1);
176
177
178 for (var key in mapMapTestOutput) {
179 for (var key2 in mapMapTestOutput[key]) {
Roger Meierb661e832012-04-15 15:58:43 +0000180 equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
Roger Meierbb267d42011-07-30 15:10:42 +0000181 }
182 }
183
Roger Meier4f8a5232011-09-09 08:17:02 +0000184 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
Roger Meierbb267d42011-07-30 15:10:42 +0000185 });
186
187
188module("Exception");
189
190 test("Xception", function() {
191 expect(2);
192 try{
193 client.testException("Xception");
194 }catch(e){
Roger Meierb661e832012-04-15 15:58:43 +0000195 equal(e.errorCode, 1001);
196 equal(e.message, "Xception");
Roger Meierbb267d42011-07-30 15:10:42 +0000197 }
198 });
199
Roger Meierb661e832012-04-15 15:58:43 +0000200 test("no Exception", 0, function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000201 try{
202 client.testException("no Exception");
203 }catch(e){
204 ok(false);
205 }
206 });
207
208 test("unexpected Application Exception", function() {
209 expect(1);
210 try{
211 client.testException("ApplicationException");
212 } catch(e) {
213 ok(true); //@HACK: ignore faulty java server response for exceptions
Roger Meierb661e832012-04-15 15:58:43 +0000214 //equal(e.message, "ApplicationException");
Roger Meierbb267d42011-07-30 15:10:42 +0000215 }
216 });
217
218
219module("Insanity");
220
221 test("testInsanity", function() {
222 var insanity = {
223 "1":{
Roger Meier4f8a5232011-09-09 08:17:02 +0000224 "2":{
225 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000226 "xtructs":[{
227 "string_thing":"Goodbye4",
228 "byte_thing":4,
229 "i32_thing":4,
230 "i64_thing":4
231 },
232 {
233 "string_thing":"Hello2",
234 "byte_thing":2,
235 "i32_thing":2,
236 "i64_thing":2
237 }
238 ]
239 },
Roger Meier4f8a5232011-09-09 08:17:02 +0000240 "3":{
241 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000242 "xtructs":[{
243 "string_thing":"Goodbye4",
244 "byte_thing":4,
245 "i32_thing":4,
246 "i64_thing":4
247 },
248 {
249 "string_thing":"Hello2",
250 "byte_thing":2,
251 "i32_thing":2,
252 "i64_thing":2
253 }
254 ]
255 }
256 },
257 "2":{ "6":{ "userMap":null, "xtructs":null } }
258 };
Roger Meierb661e832012-04-15 15:58:43 +0000259 var res = client.testInsanity(new ThriftTest.Insanity());
Roger Meierbb267d42011-07-30 15:10:42 +0000260 ok(res, JSON.stringify(res));
261 ok(insanity, JSON.stringify(insanity));
Roger Meier4f8a5232011-09-09 08:17:02 +0000262
263 checkRecursively(res, insanity);
Roger Meierbb267d42011-07-30 15:10:42 +0000264 });
265
266
267//////////////////////////////////
268//Run same tests asynchronously
269jQuery.ajaxSetup({ timeout: 0 });
270$(document).ajaxError( function() { QUnit.start(); } );
271
272module("Async Manual");
273
274 test("testI32", function() {
275 expect( 2 );
276 QUnit.stop();
277
278 var transport = new Thrift.Transport();
279 var protocol = new Thrift.Protocol(transport);
280 var client = new ThriftTest.ThriftTestClient(protocol);
281
282 var jqxhr = jQuery.ajax({
283 url: "/service",
284 data: client.send_testI32(Math.pow(-2,31)),
285 type: "POST",
286 cache: false,
287 dataType: "text",
288 success: function(res){
289 transport.setRecvBuffer( res );
Roger Meierb661e832012-04-15 15:58:43 +0000290 equal(client.recv_testI32(), Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000291 },
292 error: function() { ok(false); },
293 complete: function() {
294 ok(true);
295 QUnit.start();
296 }
297 });
298 });
299
300
301 test("testI64", function() {
302 expect( 2 );
303 QUnit.stop();
304
305 var transport = new Thrift.Transport();
306 var protocol = new Thrift.Protocol(transport);
307 var client = new ThriftTest.ThriftTestClient(protocol);
308
309 jQuery.ajax({
310 url: "/service",
311 data: client.send_testI64(Math.pow(-2,61)),
312 type: "POST",
313 cache: false,
314 dataType: "text",
315 success: function(res){
316 transport.setRecvBuffer( res );
Roger Meierb661e832012-04-15 15:58:43 +0000317 equal(client.recv_testI64(), Math.pow(-2,61));
Roger Meierbb267d42011-07-30 15:10:42 +0000318 },
319 error: function() { ok(false); },
320 complete: function() {
321 ok(true);
322 QUnit.start();
323 }
324 });
325 });
326
327
328module("Async");
329
330 test("Double", function() {
331 expect( 1 );
332
333 QUnit.stop();
334 client.testDouble(3.14159265, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000335 equal(result, 3.14159265);
Roger Meierbb267d42011-07-30 15:10:42 +0000336 QUnit.start();
337 });
338 });
339
340 test("Byte", function() {
341 expect( 1 );
342
343 QUnit.stop();
344 client.testByte(0x01, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000345 equal(result, 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000346 QUnit.start();
347 });
348 });
349
350 test("I32", function() {
351 expect( 3 );
352
353 QUnit.stop();
354 client.testI32(Math.pow(2,30), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000355 equal(result, Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000356 QUnit.start();
357 });
358
359 QUnit.stop();
360 var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000361 equal(result, Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000362 });
363
364 jqxhr.success(function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000365 equal(result, Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000366 QUnit.start();
367 });
368 });
369
370 test("I64", function() {
371 expect( 4 );
372
373 QUnit.stop();
374 client.testI64(Math.pow(2,60), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000375 equal(result, Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +0000376 QUnit.start();
377 });
378
379 QUnit.stop();
380 client.testI64(Math.pow(-2,61), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000381 equal(result, Math.pow(-2,61));
Roger Meierbb267d42011-07-30 15:10:42 +0000382 })
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000383 .error( function(xhr, status, e) { ok(false, e.message); } )
Roger Meierbb267d42011-07-30 15:10:42 +0000384 .success(function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000385 equal(result, Math.pow(-2,61));
Roger Meierbb267d42011-07-30 15:10:42 +0000386 })
387 .complete(function() {
388 ok(true);
389 QUnit.start();
390 });
391 });
392
393 test("Xception", function() {
394 expect( 2 );
395
396 QUnit.stop();
397
398 var dfd = client.testException("Xception", function(result) {
399 ok(false);
400 QUnit.start();
401 })
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000402 .error(function(xhr, status, e){
Roger Meierb661e832012-04-15 15:58:43 +0000403 equal(e.errorCode, 1001);
404 equal(e.message, "Xception");
Roger Meierbb267d42011-07-30 15:10:42 +0000405 QUnit.start();
406 });
407 });