blob: 1504f623dd3bccbb8ab53c1343c9c6833b1a7fb8 [file] [log] [blame]
henriquea2de4102014-02-07 14:12:56 +01001/*
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 */
Henrique Mendonça095ddb72013-09-20 19:38:03 +020019 /* jshint -W100 */
Roger Meierbb267d42011-07-30 15:10:42 +000020
21/*
henriquea2de4102014-02-07 14:12:56 +010022 * JavaScript test suite for ThriftTest.thrift. These tests
23 * will run against Normal (-gen js) and jQuery (-gen js:jquery)
24 * Apache Thrift interfaces.
25 *
26 * Synchronous blocking calls should be identical in both
27 * Normal and jQuery interfaces. All synchronous tests belong
28 * here.
29 *
30 * Asynchronous sucess callbacks passed as the last parameter
31 * of an RPC call should be identical in both Normal and jQuery
32 * interfaces. Async success tests belong here.
33 *
34 * Asynchronous exception processing is different in Normal
35 * and jQuery interfaces. Such tests belong in the test-nojq.js
36 * or test-jq.js files respectively. jQuery specific XHR object
37 * tests also belong in test-jq.js. Do not create any jQuery
38 * dependencies in this file or in test-nojq.js
39 *
40 * To compile client code for this test use:
41 * $ thrift -gen js ThriftTest.thrift
42 * -- or --
43 * $ thrift -gen js:jquery ThriftTest.thrift
44 *
45 * See also:
46 * ++ test-nojq.js for "-gen js" only tests
47 * ++ test-jq.js for "-gen js:jquery" only tests
Roger Meierbb267d42011-07-30 15:10:42 +000048 */
49
50var transport = new Thrift.Transport("/service");
51var protocol = new Thrift.Protocol(transport);
52var client = new ThriftTest.ThriftTestClient(protocol);
53
54// all Languages in UTF-8
55var 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 +000056
57function checkRecursively(map1, map2) {
58 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
59 if (!map1 || typeof map1 !== 'object') {
Roger Meierb661e832012-04-15 15:58:43 +000060 equal(map1, map2);
Roger Meier4f8a5232011-09-09 08:17:02 +000061 } else {
62 for (var key in map1) {
63 checkRecursively(map1[key], map2[key]);
64 }
65 }
66 }
67}
Roger Meierbb267d42011-07-30 15:10:42 +000068
69module("Base Types");
70
71 test("Void", function() {
Roger Meierb661e832012-04-15 15:58:43 +000072 equal(client.testVoid(), undefined);
Roger Meierbb267d42011-07-30 15:10:42 +000073 });
74 test("String", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000075 equal(client.testString(''), '');
Roger Meierb661e832012-04-15 15:58:43 +000076 equal(client.testString(stringTest), stringTest);
Roger Meierbb267d42011-07-30 15:10:42 +000077
78 var specialCharacters = 'quote: \" backslash:' +
79 ' forwardslash-escaped: \/ ' +
80 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
81 ' now-all-of-them-together: "\\\/\b\n\r\t' +
82 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
Roger Meierb661e832012-04-15 15:58:43 +000083 equal(client.testString(specialCharacters),specialCharacters);
Roger Meierbb267d42011-07-30 15:10:42 +000084 });
85 test("Double", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000086 equal(client.testDouble(0), 0);
87 equal(client.testDouble(-1), -1);
Roger Meierb661e832012-04-15 15:58:43 +000088 equal(client.testDouble(3.14), 3.14);
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000089 equal(client.testDouble(Math.pow(2,60)), Math.pow(2,60));
Roger Meierbb267d42011-07-30 15:10:42 +000090 });
91 test("Byte", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000092 equal(client.testByte(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000093 equal(client.testByte(0x01), 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +000094 });
95 test("I32", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000096 equal(client.testI32(0), 0);
Roger Meierb661e832012-04-15 15:58:43 +000097 equal(client.testI32(Math.pow(2,30)), Math.pow(2,30));
Henrique Mendonca6af2ec02012-09-27 13:14:36 +000098 equal(client.testI32(-Math.pow(2,30)), -Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +000099 });
100 test("I64", function() {
Henrique Mendonca6af2ec02012-09-27 13:14:36 +0000101 equal(client.testI64(0), 0);
henriquea2de4102014-02-07 14:12:56 +0100102 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
103 equal(client.testI64(Math.pow(2,52)), Math.pow(2,52));
104 equal(client.testI64(-Math.pow(2,52)), -Math.pow(2,52));
Roger Meierbb267d42011-07-30 15:10:42 +0000105 });
106
107
108module("Structured Types");
109
110 test("Struct", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000111 var structTestInput = new ThriftTest.Xtruct();
112 structTestInput.string_thing = 'worked';
113 structTestInput.byte_thing = 0x01;
114 structTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100115 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
116 structTestInput.i64_thing = Math.pow(2,52);
Roger Meierbb267d42011-07-30 15:10:42 +0000117
118 var structTestOutput = client.testStruct(structTestInput);
119
Roger Meierb661e832012-04-15 15:58:43 +0000120 equal(structTestOutput.string_thing, structTestInput.string_thing);
121 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
122 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
123 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Roger Meierbb267d42011-07-30 15:10:42 +0000124
Roger Meierb661e832012-04-15 15:58:43 +0000125 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000126 });
127
128 test("Nest", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000129 var xtrTestInput = new ThriftTest.Xtruct();
130 xtrTestInput.string_thing = 'worked';
131 xtrTestInput.byte_thing = 0x01;
132 xtrTestInput.i32_thing = Math.pow(2,30);
henriquea2de4102014-02-07 14:12:56 +0100133 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
134 xtrTestInput.i64_thing = Math.pow(2,52);
Roger Meierbb267d42011-07-30 15:10:42 +0000135
Roger Meieredf0d1d2011-08-03 21:21:43 +0000136 var nestTestInput = new ThriftTest.Xtruct2();
137 nestTestInput.byte_thing = 0x02;
138 nestTestInput.struct_thing = xtrTestInput;
139 nestTestInput.i32_thing = Math.pow(2,15);
Roger Meierbb267d42011-07-30 15:10:42 +0000140
141 var nestTestOutput = client.testNest(nestTestInput);
142
Roger Meierb661e832012-04-15 15:58:43 +0000143 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
144 equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
145 equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
146 equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
147 equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
148 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Roger Meierbb267d42011-07-30 15:10:42 +0000149
Roger Meierb661e832012-04-15 15:58:43 +0000150 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000151 });
152
153 test("Map", function() {
154 var mapTestInput = {7:77, 8:88, 9:99};
155
Roger Meieredf0d1d2011-08-03 21:21:43 +0000156 var mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000157
158 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000159 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000160 }
161 });
162
163 test("StringMap", function() {
164 var mapTestInput = {
165 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
166 "longValue":stringTest, stringTest:"long key"
167 };
168
Roger Meieredf0d1d2011-08-03 21:21:43 +0000169 var mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000170
171 for (var key in mapTestOutput) {
Roger Meierb661e832012-04-15 15:58:43 +0000172 equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000173 }
174 });
175
176 test("Set", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000177 var setTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000178 ok(client.testSet(setTestInput), setTestInput);
179 });
180
181 test("List", function() {
Roger Meieredf0d1d2011-08-03 21:21:43 +0000182 var listTestInput = [1,2,3];
Roger Meierbb267d42011-07-30 15:10:42 +0000183 ok(client.testList(listTestInput), listTestInput);
184 });
185
186 test("Enum", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000187 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
Roger Meierbb267d42011-07-30 15:10:42 +0000188 });
189
190 test("TypeDef", function() {
Roger Meierb661e832012-04-15 15:58:43 +0000191 equal(client.testTypedef(69), 69);
Roger Meierbb267d42011-07-30 15:10:42 +0000192 });
193
Jens Geyer329d59a2014-06-19 22:11:53 +0200194 test("Skip", function() {
195 var structTestInput = new ThriftTest.Xtruct();
196 var modifiedClient = new ThriftTest.ThriftTestClient(protocol);
197
198 modifiedClient.recv_testStruct = function() {
199 var input = modifiedClient.input;
200 var xtruct3 = new ThriftTest.Xtruct3();
201
202 input.readMessageBegin();
203 input.readStructBegin();
204
205 // read Xtruct data with Xtruct3
206 input.readFieldBegin();
207 xtruct3.read(input);
208 input.readFieldEnd();
209 // read Thrift.Type.STOP message
210 input.readFieldBegin();
211 input.readFieldEnd();
212
213 input.readStructEnd();
214 input.readMessageEnd();
215
216 return xtruct3;
217 };
218
219 structTestInput.string_thing = 'worked';
220 structTestInput.byte_thing = 0x01;
221 structTestInput.i32_thing = Math.pow(2,30);
222 structTestInput.i64_thing = Math.pow(2,52);
223
224 var structTestOutput = modifiedClient.testStruct(structTestInput);
225
226 equal(structTestOutput instanceof ThriftTest.Xtruct3, true);
227 equal(structTestOutput.string_thing, structTestInput.string_thing);
228 equal(structTestOutput.changed, null);
229 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
230 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
231 });
232
Roger Meierbb267d42011-07-30 15:10:42 +0000233
234module("deeper!");
235
236 test("MapMap", function() {
237 var mapMapTestExpectedResult = {
238 "4":{"1":1,"2":2,"3":3,"4":4},
239 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
240 };
241
242 var mapMapTestOutput = client.testMapMap(1);
243
244
245 for (var key in mapMapTestOutput) {
246 for (var key2 in mapMapTestOutput[key]) {
Roger Meierb661e832012-04-15 15:58:43 +0000247 equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
Roger Meierbb267d42011-07-30 15:10:42 +0000248 }
249 }
250
Roger Meier4f8a5232011-09-09 08:17:02 +0000251 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
Roger Meierbb267d42011-07-30 15:10:42 +0000252 });
253
254
255module("Exception");
256
257 test("Xception", function() {
258 expect(2);
259 try{
260 client.testException("Xception");
261 }catch(e){
Roger Meierb661e832012-04-15 15:58:43 +0000262 equal(e.errorCode, 1001);
263 equal(e.message, "Xception");
Roger Meierbb267d42011-07-30 15:10:42 +0000264 }
265 });
266
Roger Meierb661e832012-04-15 15:58:43 +0000267 test("no Exception", 0, function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000268 try{
269 client.testException("no Exception");
270 }catch(e){
271 ok(false);
272 }
273 });
274
Roger Meier8ebe0d92013-01-06 14:33:39 +0100275 test("TException", function() {
henriquea2de4102014-02-07 14:12:56 +0100276 //ThriftTest does not list TException as a legal exception so it will
277 // generate an exception on the server that does not propagate back to
278 // the client. This test has been modified to equate to "no exception"
Roger Meierbb267d42011-07-30 15:10:42 +0000279 expect(1);
280 try{
Roger Meier8ebe0d92013-01-06 14:33:39 +0100281 client.testException("TException");
Roger Meierbb267d42011-07-30 15:10:42 +0000282 } catch(e) {
henriquea2de4102014-02-07 14:12:56 +0100283 //ok(false);
Roger Meierbb267d42011-07-30 15:10:42 +0000284 }
henriquea2de4102014-02-07 14:12:56 +0100285 ok(true);
Roger Meierbb267d42011-07-30 15:10:42 +0000286 });
287
288
289module("Insanity");
290
291 test("testInsanity", function() {
292 var insanity = {
293 "1":{
Roger Meier4f8a5232011-09-09 08:17:02 +0000294 "2":{
295 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000296 "xtructs":[{
297 "string_thing":"Goodbye4",
298 "byte_thing":4,
299 "i32_thing":4,
300 "i64_thing":4
301 },
302 {
303 "string_thing":"Hello2",
304 "byte_thing":2,
305 "i32_thing":2,
306 "i64_thing":2
307 }
308 ]
309 },
Roger Meier4f8a5232011-09-09 08:17:02 +0000310 "3":{
311 "userMap":{ "5":5, "8":8 },
Roger Meierbb267d42011-07-30 15:10:42 +0000312 "xtructs":[{
313 "string_thing":"Goodbye4",
314 "byte_thing":4,
315 "i32_thing":4,
316 "i64_thing":4
317 },
318 {
319 "string_thing":"Hello2",
320 "byte_thing":2,
321 "i32_thing":2,
322 "i64_thing":2
323 }
324 ]
325 }
326 },
327 "2":{ "6":{ "userMap":null, "xtructs":null } }
328 };
Roger Meierb661e832012-04-15 15:58:43 +0000329 var res = client.testInsanity(new ThriftTest.Insanity());
Roger Meierbb267d42011-07-30 15:10:42 +0000330 ok(res, JSON.stringify(res));
331 ok(insanity, JSON.stringify(insanity));
Roger Meier4f8a5232011-09-09 08:17:02 +0000332
333 checkRecursively(res, insanity);
Roger Meierbb267d42011-07-30 15:10:42 +0000334 });
335
336
337//////////////////////////////////
338//Run same tests asynchronously
Roger Meierbb267d42011-07-30 15:10:42 +0000339
340module("Async");
341
342 test("Double", function() {
343 expect( 1 );
344
345 QUnit.stop();
346 client.testDouble(3.14159265, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000347 equal(result, 3.14159265);
Roger Meierbb267d42011-07-30 15:10:42 +0000348 QUnit.start();
349 });
350 });
351
352 test("Byte", function() {
353 expect( 1 );
354
355 QUnit.stop();
356 client.testByte(0x01, function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000357 equal(result, 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000358 QUnit.start();
359 });
360 });
361
362 test("I32", function() {
henriquea2de4102014-02-07 14:12:56 +0100363 expect( 2 );
Roger Meierbb267d42011-07-30 15:10:42 +0000364
365 QUnit.stop();
366 client.testI32(Math.pow(2,30), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000367 equal(result, Math.pow(2,30));
Roger Meierbb267d42011-07-30 15:10:42 +0000368 QUnit.start();
369 });
370
371 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100372 client.testI32(Math.pow(-2,31), function(result) {
Roger Meierb661e832012-04-15 15:58:43 +0000373 equal(result, Math.pow(-2,31));
Roger Meierbb267d42011-07-30 15:10:42 +0000374 QUnit.start();
375 });
376 });
377
378 test("I64", function() {
Roger Meierbb267d42011-07-30 15:10:42 +0000379 expect( 2 );
380
381 QUnit.stop();
henriquea2de4102014-02-07 14:12:56 +0100382 //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));
Roger Meierbb267d42011-07-30 15:10:42 +0000385 QUnit.start();
henriquea2de4102014-02-07 14:12:56 +0100386 });
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 QUnit.start();
Roger Meierbb267d42011-07-30 15:10:42 +0000393 });
394 });