blob: caf34a21de3072c80a2abe8067d4d5c884096467 [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ú, 粵語";
31
32
33module("Base Types");
34
35 test("Void", function() {
36 equals(client.testVoid(), undefined);
37 });
38 test("String", function() {
39 equals(client.testString(stringTest), stringTest);
40
41 var specialCharacters = 'quote: \" backslash:' +
42 ' forwardslash-escaped: \/ ' +
43 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
44 ' now-all-of-them-together: "\\\/\b\n\r\t' +
45 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
46 equals(client.testString(specialCharacters),specialCharacters);
47 });
48 test("Double", function() {
49 equals(client.testDouble(3.14), 3.14);
50 });
51 test("Byte", function() {
52 equals(client.testByte(0x01), 0x01);
53 });
54 test("I32", function() {
55 equals(client.testI32(Math.pow(2,30)), Math.pow(2,30));
56 });
57 test("I64", function() {
58 equals(client.testI64(Math.pow(2,60)), Math.pow(2,60));
59 });
60
61
62module("Structured Types");
63
64 test("Struct", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +000065 var structTestInput = new ThriftTest.Xtruct();
66 structTestInput.string_thing = 'worked';
67 structTestInput.byte_thing = 0x01;
68 structTestInput.i32_thing = Math.pow(2,30);
69 structTestInput.i64_thing = Math.pow(2,60);
Roger Meierbb267d42011-07-30 15:10:42 +000070
71 var structTestOutput = client.testStruct(structTestInput);
72
73 equals(structTestOutput.string_thing, structTestInput.string_thing);
74 equals(structTestOutput.byte_thing, structTestInput.byte_thing);
75 equals(structTestOutput.i32_thing, structTestInput.i32_thing);
76 equals(structTestOutput.i64_thing, structTestInput.i64_thing);
77
Roger Meieredf0d1d2011-08-03 21:21:43 +000078 equals(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +000079 });
80
81 test("Nest", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +000082 var xtrTestInput = new ThriftTest.Xtruct();
83 xtrTestInput.string_thing = 'worked';
84 xtrTestInput.byte_thing = 0x01;
85 xtrTestInput.i32_thing = Math.pow(2,30);
86 xtrTestInput.i64_thing = Math.pow(2,60);
Roger Meierbb267d42011-07-30 15:10:42 +000087
Roger Meieredf0d1d2011-08-03 21:21:43 +000088 var nestTestInput = new ThriftTest.Xtruct2();
89 nestTestInput.byte_thing = 0x02;
90 nestTestInput.struct_thing = xtrTestInput;
91 nestTestInput.i32_thing = Math.pow(2,15);
Roger Meierbb267d42011-07-30 15:10:42 +000092
93 var nestTestOutput = client.testNest(nestTestInput);
94
95 equals(nestTestOutput.byte_thing, nestTestInput.byte_thing);
96 equals(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
97 equals(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
98 equals(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
99 equals(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
100 equals(nestTestOutput.i32_thing, nestTestInput.i32_thing);
101
Roger Meieredf0d1d2011-08-03 21:21:43 +0000102 equals(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000103 });
104
105 test("Map", function() {
106 var mapTestInput = {7:77, 8:88, 9:99};
107
Roger Meieredf0d1d2011-08-03 21:21:43 +0000108 var mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000109
110 for (var key in mapTestOutput) {
111 equals(mapTestOutput[key], mapTestInput[key]);
112 }
113 });
114
115 test("StringMap", function() {
116 var mapTestInput = {
117 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
118 "longValue":stringTest, stringTest:"long key"
119 };
120
Roger Meieredf0d1d2011-08-03 21:21:43 +0000121 var mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000122
123 for (var key in mapTestOutput) {
124 equals(mapTestOutput[key], mapTestInput[key]);
125 }
126 });
127
128 test("Set", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000129 var setTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000130 ok(client.testSet(setTestInput), setTestInput);
131 });
132
133 test("List", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000134 var listTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000135 ok(client.testList(listTestInput), listTestInput);
136 });
137
138 test("Enum", function() {
139 equals(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
140 });
141
142 test("TypeDef", function() {
143 equals(client.testTypedef(69), 69);
144 });
145
146
147module("deeper!");
148
149 test("MapMap", function() {
150 var mapMapTestExpectedResult = {
151 "4":{"1":1,"2":2,"3":3,"4":4},
152 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
153 };
154
155 var mapMapTestOutput = client.testMapMap(1);
156
157
158 for (var key in mapMapTestOutput) {
159 for (var key2 in mapMapTestOutput[key]) {
160 equals(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
161 }
162 }
163
Roger Meieredf0d1d2011-08-03 21:21:43 +0000164 equals(JSON.stringify(mapMapTestOutput), JSON.stringify(mapMapTestExpectedResult));
Roger Meierbb267d42011-07-30 15:10:42 +0000165 });
166
167
168module("Exception");
169
170 test("Xception", function() {
171 expect(2);
172 try{
173 client.testException("Xception");
174 }catch(e){
175 equals(e.errorCode, 1001);
176 equals(e.message, "Xception");
177 }
178 });
179
180 test("no Exception", function() {
181 try{
182 client.testException("no Exception");
183 }catch(e){
184 ok(false);
185 }
186 });
187
188 test("unexpected Application Exception", function() {
189 expect(1);
190 try{
191 client.testException("ApplicationException");
192 } catch(e) {
193 ok(true); //@HACK: ignore faulty java server response for exceptions
194 //equals(e.message, "ApplicationException");
195 }
196 });
197
198
199module("Insanity");
200
201 test("testInsanity", function() {
202 var insanity = {
203 "1":{
204 "3":{
205 "userMap":{ "8":8, "5":5 },
206 "xtructs":[{
207 "string_thing":"Goodbye4",
208 "byte_thing":4,
209 "i32_thing":4,
210 "i64_thing":4
211 },
212 {
213 "string_thing":"Hello2",
214 "byte_thing":2,
215 "i32_thing":2,
216 "i64_thing":2
217 }
218 ]
219 },
220 "2":{
221 "userMap":{ "8":8, "5":5 },
222 "xtructs":[{
223 "string_thing":"Goodbye4",
224 "byte_thing":4,
225 "i32_thing":4,
226 "i64_thing":4
227 },
228 {
229 "string_thing":"Hello2",
230 "byte_thing":2,
231 "i32_thing":2,
232 "i64_thing":2
233 }
234 ]
235 }
236 },
237 "2":{ "6":{ "userMap":null, "xtructs":null } }
238 };
239 var res = client.testInsanity("");
240 ok(res, JSON.stringify(res));
241 ok(insanity, JSON.stringify(insanity));
Roger Meieredf0d1d2011-08-03 21:21:43 +0000242 equals(JSON.stringify(res), JSON.stringify(insanity)); //TODO: read and compare maps recursively
Roger Meierbb267d42011-07-30 15:10:42 +0000243 });
244
245
246//////////////////////////////////
247//Run same tests asynchronously
248jQuery.ajaxSetup({ timeout: 0 });
249$(document).ajaxError( function() { QUnit.start(); } );
250
251module("Async Manual");
252
253 test("testI32", function() {
254 expect( 2 );
255 QUnit.stop();
256
257 var transport = new Thrift.Transport();
258 var protocol = new Thrift.Protocol(transport);
259 var client = new ThriftTest.ThriftTestClient(protocol);
260
261 var jqxhr = jQuery.ajax({
262 url: "/service",
263 data: client.send_testI32(Math.pow(-2,31)),
264 type: "POST",
265 cache: false,
266 dataType: "text",
267 success: function(res){
268 transport.setRecvBuffer( res );
269 equals(client.recv_testI32(), Math.pow(-2,31));
270 },
271 error: function() { ok(false); },
272 complete: function() {
273 ok(true);
274 QUnit.start();
275 }
276 });
277 });
278
279
280 test("testI64", 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 jQuery.ajax({
289 url: "/service",
290 data: client.send_testI64(Math.pow(-2,61)),
291 type: "POST",
292 cache: false,
293 dataType: "text",
294 success: function(res){
295 transport.setRecvBuffer( res );
296 equals(client.recv_testI64(), Math.pow(-2,61));
297 },
298 error: function() { ok(false); },
299 complete: function() {
300 ok(true);
301 QUnit.start();
302 }
303 });
304 });
305
306
307module("Async");
308
309 test("Double", function() {
310 expect( 1 );
311
312 QUnit.stop();
313 client.testDouble(3.14159265, function(result) {
314 equals(result, 3.14159265);
315 QUnit.start();
316 });
317 });
318
319 test("Byte", function() {
320 expect( 1 );
321
322 QUnit.stop();
323 client.testByte(0x01, function(result) {
324 equals(result, 0x01);
325 QUnit.start();
326 });
327 });
328
329 test("I32", function() {
330 expect( 3 );
331
332 QUnit.stop();
333 client.testI32(Math.pow(2,30), function(result) {
334 equals(result, Math.pow(2,30));
335 QUnit.start();
336 });
337
338 QUnit.stop();
339 var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
340 equals(result, Math.pow(-2,31));
341 });
342
343 jqxhr.success(function(result) {
344 equals(result, Math.pow(-2,31));
345 QUnit.start();
346 });
347 });
348
349 test("I64", function() {
350 expect( 4 );
351
352 QUnit.stop();
353 client.testI64(Math.pow(2,60), function(result) {
354 equals(result, Math.pow(2,60));
355 QUnit.start();
356 });
357
358 QUnit.stop();
359 client.testI64(Math.pow(-2,61), function(result) {
360 equals(result, Math.pow(-2,61));
361 })
362 .error( function(e) { ok(false); } )
363 .success(function(result) {
364 equals(result, Math.pow(-2,61));
365 })
366 .complete(function() {
367 ok(true);
368 QUnit.start();
369 });
370 });
371
372 test("Xception", function() {
373 expect( 2 );
374
375 QUnit.stop();
376
377 var dfd = client.testException("Xception", function(result) {
378 ok(false);
379 QUnit.start();
380 })
381 .error(function(e){
382 equals(e.errorCode, 1001);
383 equals(e.message, "Xception");
384 QUnit.start();
385 });
386 });