blob: 5039edb47c9eca1a5ee7d785cbb6aaf9be0ab00f [file] [log] [blame]
Roger Meierb3b07d62011-02-22 20:55:21 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Bryan Duxbury3ff373e2010-08-15 23:19:06 +00002<!--
3 Licensed to the Apache Software Foundation (ASF) under one
4 or more contributor license agreements. See the NOTICE file
5 distributed with this work for additional information
6 regarding copyright ownership. The ASF licenses this file
7 to you under the Apache License, Version 2.0 (the
8 "License"); you may not use this file except in compliance
9 with the License. You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing,
14 software distributed under the License is distributed on an
15 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 KIND, either express or implied. See the License for the
17 specific language governing permissions and limitations
18 under the License.
19-->
T Jake Lucianiefabb892010-07-28 22:31:12 +000020<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
T Jake Luciani322caa22010-02-15 03:24:55 +000021<head>
T Jake Lucianiefabb892010-07-28 22:31:12 +000022 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
23 <title>Thrift Javascript Bindings: Unit Test</title>
T Jake Luciani322caa22010-02-15 03:24:55 +000024
T Jake Lucianiefabb892010-07-28 22:31:12 +000025 <script src="/thrift.js" type="text/javascript" charset="utf-8"></script>
26 <script src="gen-js/ThriftTest_types.js" type="text/javascript" charset="utf-8"></script>
27 <script src="gen-js/ThriftTest.js" type="text/javascript" charset="utf-8"></script>
T Jake Luciani322caa22010-02-15 03:24:55 +000028
T Jake Lucianiefabb892010-07-28 22:31:12 +000029 <!-- jQuery -->
Roger Meier08b30992011-04-06 21:30:53 +000030 <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js" charset="utf-8"></script>
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000031
32 <!-- json2 -->
33 <script type="text/javascript" src="json2.js" charset="utf-8"></script>
T Jake Lucianiefabb892010-07-28 22:31:12 +000034
35 <!-- QUnit Test framework-->
Roger Meier5860f882011-05-22 09:59:34 +000036 <script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js" charset="utf-8"></script>
37 <link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
T Jake Lucianiefabb892010-07-28 22:31:12 +000038
39 <script type="text/javascript" charset="utf-8">
40 //<![CDATA[
41 $(document).ready(function(){
Roger Meier08b30992011-04-06 21:30:53 +000042 var transport = new Thrift.Transport("/service");
43 var protocol = new Thrift.Protocol(transport);
44 var client = new ThriftTest.ThriftTestClient(protocol);
T Jake Lucianiefabb892010-07-28 22:31:12 +000045
Roger Meier9d8e8f82011-06-14 19:38:27 +000046 // all Languages in UTF-8
47 var 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ú, 粵語";
48
T Jake Lucianiefabb892010-07-28 22:31:12 +000049
50 module("Base Types");
Roger Meier9d8e8f82011-06-14 19:38:27 +000051
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000052 test("Void", function() {
53 equals(client.testVoid(), undefined);
T Jake Lucianiefabb892010-07-28 22:31:12 +000054 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000055 test("String", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000056 equals(client.testString(stringTest), stringTest);
Roger Meierc9912702010-10-15 13:16:05 +000057
58 var specialCharacters = 'quote: \" backslash:' +
59 ' forwardslash-escaped: \/ ' +
60 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
61 ' now-all-of-them-together: "\\\/\b\n\r\t' +
62 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
63 equals(client.testString(specialCharacters),specialCharacters);
T Jake Lucianiefabb892010-07-28 22:31:12 +000064 });
65 test("Double", function() {
66 equals(client.testDouble(3.14), 3.14);
67 });
68 test("Byte", function() {
69 equals(client.testByte(0x01), 0x01);
70 });
71 test("I32", function() {
72 equals(client.testI32(Math.pow(2,30)), Math.pow(2,30));
73 });
74 test("I64", function() {
75 equals(client.testI64(Math.pow(2,60)), Math.pow(2,60));
76 });
77
78
79 module("Structured Types");
80
T Jake Lucianiefabb892010-07-28 22:31:12 +000081 test("Struct", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000082 var structTestInput = new ThriftTest.Xtruct()
83 structTestInput.string_thing = 'worked'
84 structTestInput.byte_thing = 0x01
85 structTestInput.i32_thing = Math.pow(2,30)
86 structTestInput.i64_thing = Math.pow(2,60)
87
88 var structTestOutput = client.testStruct(structTestInput);
89
90 equals(structTestOutput.string_thing, structTestInput.string_thing);
91 equals(structTestOutput.byte_thing, structTestInput.byte_thing);
92 equals(structTestOutput.i32_thing, structTestInput.i32_thing);
93 equals(structTestOutput.i64_thing, structTestInput.i64_thing);
94
95 equals(JSON.stringify(structTestOutput), JSON.stringify(structTestInput))
T Jake Lucianiefabb892010-07-28 22:31:12 +000096 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000097
T Jake Lucianiefabb892010-07-28 22:31:12 +000098 test("Nest", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000099 var xtrTestInput = new ThriftTest.Xtruct()
100 xtrTestInput.string_thing = 'worked'
101 xtrTestInput.byte_thing = 0x01
102 xtrTestInput.i32_thing = Math.pow(2,30)
103 xtrTestInput.i64_thing = Math.pow(2,60)
104
105 var nestTestInput = new ThriftTest.Xtruct2()
106 nestTestInput.byte_thing = 0x02
107 nestTestInput.struct_thing = xtrTestInput
108 nestTestInput.i32_thing = Math.pow(2,15)
109
110 var nestTestOutput = client.testNest(nestTestInput);
111
112 equals(nestTestOutput.byte_thing, nestTestInput.byte_thing);
113 equals(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
114 equals(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
115 equals(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
116 equals(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
117 equals(nestTestOutput.i32_thing, nestTestInput.i32_thing);
118
119 equals(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput))
T Jake Lucianiefabb892010-07-28 22:31:12 +0000120 });
Roger Meier9d8e8f82011-06-14 19:38:27 +0000121
T Jake Lucianiefabb892010-07-28 22:31:12 +0000122 test("Map", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000123 var mapTestInput = {7:77, 8:88, 9:99};
124
125 var mapTestOutput = client.testMap(mapTestInput)
Roger Meier9d8e8f82011-06-14 19:38:27 +0000126
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000127 for (var key in mapTestOutput) {
128 equals(mapTestOutput[key], mapTestInput[key]);
129 }
T Jake Lucianiefabb892010-07-28 22:31:12 +0000130 });
Roger Meier9d8e8f82011-06-14 19:38:27 +0000131
132 test("StringMap", function() {
133 var mapTestInput = {
134 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
135 "longValue":stringTest, stringTest:"long key"
136 };
137
138 var mapTestOutput = client.testStringMap(mapTestInput)
139
140 for (var key in mapTestOutput) {
141 equals(mapTestOutput[key], mapTestInput[key]);
142 }
143 });
144
T Jake Lucianiefabb892010-07-28 22:31:12 +0000145 test("Set", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000146 var setTestInput = new Array(1,2,3)
147 ok(client.testSet(setTestInput), setTestInput);
T Jake Lucianiefabb892010-07-28 22:31:12 +0000148 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000149
150 test("List", function() {
151 var listTestInput = new Array(1,2,3)
152 ok(client.testList(listTestInput), listTestInput);
153 });
154
T Jake Lucianiefabb892010-07-28 22:31:12 +0000155 test("Enum", function() {
156 equals(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
157 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000158
159 test("TypeDef", function() {
160 equals(client.testTypedef(69), 69);
161 });
162
163 module("deeper!");
164
165 test("MapMap", function() {
166 var mapMapTestExpectedResult = {
Roger Meierd9924982011-02-24 16:04:03 +0000167 "4":{"1":1,"2":2,"3":3,"4":4},
168 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000169 };
170
171 var mapMapTestOutput = client.testMapMap(1);
Roger Meierd9924982011-02-24 16:04:03 +0000172
173
174 for (var key in mapMapTestOutput) {
175 for (var key2 in mapMapTestOutput[key]) {
176 equals(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
177 }
178 }
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000179
180 equals(JSON.stringify(mapMapTestOutput), JSON.stringify(mapMapTestExpectedResult))
181 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000182
T Jake Lucianiefabb892010-07-28 22:31:12 +0000183
184 module("Exception");
185
186 test("Xception", function() {
187 expect(2);
188 try{
189 client.testException("Xception");
190 }catch(e){
Roger Meier37b5bf82010-10-24 21:41:24 +0000191 equals(e.errorCode, 1001);
Roger Meierd9924982011-02-24 16:04:03 +0000192 equals(e.message, "Xception");
T Jake Lucianiefabb892010-07-28 22:31:12 +0000193 }
194 });
195
196 test("no Exception", function() {
T Jake Lucianiefabb892010-07-28 22:31:12 +0000197 try{
198 client.testException("no Exception");
199 }catch(e){
Roger Meierb3b07d62011-02-22 20:55:21 +0000200 ok(false);
201 }
202 });
203
204 test("unexpected Application Exception", function() {
205 expect(1);
206 try{
207 client.testException("ApplicationException");
208 } catch(e) {
Roger Meierd9924982011-02-24 16:04:03 +0000209 ok(true); //@HACK: ignore faulty java server response for exceptions
Roger Meier08b30992011-04-06 21:30:53 +0000210 //equals(e.message, "ApplicationException");
T Jake Lucianiefabb892010-07-28 22:31:12 +0000211 }
212 });
213
214
215 module("Insanity");
216
217 test("testInsanity", function() {
Roger Meiera35944b2011-07-15 20:16:43 +0000218 var insanity = {
219 "1":{
220 "3":{
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 "2":{
237 "userMap":{ "8":8, "5":5 },
238 "xtructs":[{
239 "string_thing":"Goodbye4",
240 "byte_thing":4,
241 "i32_thing":4,
242 "i64_thing":4
243 },
244 {
245 "string_thing":"Hello2",
246 "byte_thing":2,
247 "i32_thing":2,
248 "i64_thing":2
249 }
250 ]
251 }
252 },
253 "2":{ "6":{ "userMap":null, "xtructs":null } }
254 };
255 var res = client.testInsanity("");
256 ok(res, JSON.stringify(res));
257 ok(insanity, JSON.stringify(insanity));
258 equals(JSON.stringify(res), JSON.stringify(insanity)) //TODO: read and compare maps recursively
T Jake Lucianiefabb892010-07-28 22:31:12 +0000259 });
260
Roger Meier08b30992011-04-06 21:30:53 +0000261
262 //////////////////////////////////
263 //Run same tests asynchronously
264 jQuery.ajaxSetup({ timeout: 0 });
265 $(document).ajaxError( function() { QUnit.start(); } );
266
267 module("Async Manual");
268
269 test("testI32", function() {
270 expect( 2 );
271 QUnit.stop();
272
273 var transport = new Thrift.Transport();
274 var protocol = new Thrift.Protocol(transport);
275 var client = new ThriftTest.ThriftTestClient(protocol);
276
277 var jqxhr = jQuery.ajax({
278 url: "/service",
279 data: client.send_testI32(Math.pow(-2,31)),
280 type: "POST",
281 cache: false,
282 dataType: "text",
283 success: function(res){
284 transport.setRecvBuffer( res );
285 equals(client.recv_testI32(), Math.pow(-2,31));
286 },
287 error: function() { ok(false); },
288 complete: function() {
289 ok(true);
290 QUnit.start();
291 }
292 });
293 });
294
295
296 test("testI64", function() {
297 expect( 2 );
298 QUnit.stop();
299
300 var transport = new Thrift.Transport();
301 var protocol = new Thrift.Protocol(transport);
302 var client = new ThriftTest.ThriftTestClient(protocol);
303
304 jQuery.ajax({
305 url: "/service",
306 data: client.send_testI64(Math.pow(-2,61)),
307 type: "POST",
308 cache: false,
309 dataType: "text",
310 success: function(res){
311 transport.setRecvBuffer( res );
312 equals(client.recv_testI64(), Math.pow(-2,61));
313 },
314 error: function() { ok(false); },
315 complete: function() {
316 ok(true);
317 QUnit.start();
318 }
319 });
320 });
321
322
323
324 module("Async");
325
326 test("Double", function() {
327 expect( 1 );
328
329 QUnit.stop();
330 client.testDouble(3.14159265, function(result) {
331 equals(result, 3.14159265);
332 QUnit.start();
333 });
334 });
335
336 test("Byte", function() {
337 expect( 1 );
338
339 QUnit.stop();
340 client.testByte(0x01, function(result) {
341 equals(result, 0x01);
342 QUnit.start();
343 });
344 });
345
346 test("I32", function() {
347 expect( 3 );
348
349 QUnit.stop();
350 client.testI32(Math.pow(2,30), function(result) {
351 equals(result, Math.pow(2,30));
352 QUnit.start();
353 });
354
355 QUnit.stop();
356 var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
357 equals(result, Math.pow(-2,31));
358 });
359
360 jqxhr.success(function(result) {
361 equals(result, Math.pow(-2,31));
362 QUnit.start();
363 });
364 });
365
366 test("I64", function() {
367 expect( 4 );
368
369 QUnit.stop();
370 client.testI64(Math.pow(2,60), function(result) {
371 equals(result, Math.pow(2,60));
372 QUnit.start();
373 });
374
375 QUnit.stop();
376 client.testI64(Math.pow(-2,61), function(result) {
377 equals(result, Math.pow(-2,61));
378 })
379 .error( function(e) { ok(false); } )
380 .success(function(result) {
381 equals(result, Math.pow(-2,61));
382 })
383 .complete(function() {
384 ok(true);
385 QUnit.start();
386 });
387 });
388
389 test("Xception", function() {
390 expect( 2 );
391
392 QUnit.stop();
393
394 var dfd = client.testException("Xception", function(result) {
395 ok(false);
396 QUnit.start();
397 })
398 .error(function(e){
399 equals(e.errorCode, 1001);
400 equals(e.message, "Xception");
401 QUnit.start();
402 });
403 });
404
405
T Jake Lucianiefabb892010-07-28 22:31:12 +0000406 });
407 //]]>
408 </script>
T Jake Luciani322caa22010-02-15 03:24:55 +0000409
410</head>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000411<body>
Roger Meier5aaa0212010-11-04 12:20:05 +0000412 <h1 id="qunit-header">Thrift Javascript Bindings: Unit Test (<a href="https://svn.apache.org/repos/asf/thrift/trunk/test/ThriftTest.thrift">ThriftTest.thrift</a>)</h1>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000413 <h2 id="qunit-banner"></h2>
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000414 <div id="qunit-testrunner-toolbar"></div>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000415 <h2 id="qunit-userAgent"></h2>
416 <ol id="qunit-tests"><li><!-- get valid xhtml strict--></li></ol>
417 <p>
418 <a href="http://validator.w3.org/check/referer"><img
419 src="http://www.w3.org/Icons/valid-xhtml10"
420 alt="Valid XHTML 1.0!" height="31" width="88" /></a>
421 </p>
T Jake Luciani322caa22010-02-15 03:24:55 +0000422</body>
423</html>