blob: 505991fafc0fd71800152e1fc33d649699608c02 [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
46
47 module("Base Types");
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000048
49 test("Void", function() {
50 equals(client.testVoid(), undefined);
T Jake Lucianiefabb892010-07-28 22:31:12 +000051 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000052 test("String", function() {
53 // all Languages in UTF-8
54 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ú, 粵語";
55 equals(client.testString(stringTest), stringTest);
Roger Meierc9912702010-10-15 13:16:05 +000056
57 var specialCharacters = 'quote: \" backslash:' +
58 ' forwardslash-escaped: \/ ' +
59 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
60 ' now-all-of-them-together: "\\\/\b\n\r\t' +
61 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
62 equals(client.testString(specialCharacters),specialCharacters);
T Jake Lucianiefabb892010-07-28 22:31:12 +000063 });
64 test("Double", function() {
65 equals(client.testDouble(3.14), 3.14);
66 });
67 test("Byte", function() {
68 equals(client.testByte(0x01), 0x01);
69 });
70 test("I32", function() {
71 equals(client.testI32(Math.pow(2,30)), Math.pow(2,30));
72 });
73 test("I64", function() {
74 equals(client.testI64(Math.pow(2,60)), Math.pow(2,60));
75 });
76
77
78 module("Structured Types");
79
T Jake Lucianiefabb892010-07-28 22:31:12 +000080 test("Struct", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000081 var structTestInput = new ThriftTest.Xtruct()
82 structTestInput.string_thing = 'worked'
83 structTestInput.byte_thing = 0x01
84 structTestInput.i32_thing = Math.pow(2,30)
85 structTestInput.i64_thing = Math.pow(2,60)
86
87 var structTestOutput = client.testStruct(structTestInput);
88
89 equals(structTestOutput.string_thing, structTestInput.string_thing);
90 equals(structTestOutput.byte_thing, structTestInput.byte_thing);
91 equals(structTestOutput.i32_thing, structTestInput.i32_thing);
92 equals(structTestOutput.i64_thing, structTestInput.i64_thing);
93
94 equals(JSON.stringify(structTestOutput), JSON.stringify(structTestInput))
T Jake Lucianiefabb892010-07-28 22:31:12 +000095 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000096
T Jake Lucianiefabb892010-07-28 22:31:12 +000097 test("Nest", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +000098 var xtrTestInput = new ThriftTest.Xtruct()
99 xtrTestInput.string_thing = 'worked'
100 xtrTestInput.byte_thing = 0x01
101 xtrTestInput.i32_thing = Math.pow(2,30)
102 xtrTestInput.i64_thing = Math.pow(2,60)
103
104 var nestTestInput = new ThriftTest.Xtruct2()
105 nestTestInput.byte_thing = 0x02
106 nestTestInput.struct_thing = xtrTestInput
107 nestTestInput.i32_thing = Math.pow(2,15)
108
109 var nestTestOutput = client.testNest(nestTestInput);
110
111 equals(nestTestOutput.byte_thing, nestTestInput.byte_thing);
112 equals(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
113 equals(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
114 equals(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
115 equals(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
116 equals(nestTestOutput.i32_thing, nestTestInput.i32_thing);
117
118 equals(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput))
T Jake Lucianiefabb892010-07-28 22:31:12 +0000119 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000120
T Jake Lucianiefabb892010-07-28 22:31:12 +0000121 test("Map", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000122 var mapTestInput = {7:77, 8:88, 9:99};
123
124 var mapTestOutput = client.testMap(mapTestInput)
125
126 for (var key in mapTestOutput) {
127 equals(mapTestOutput[key], mapTestInput[key]);
128 }
129
Roger Meierd9924982011-02-24 16:04:03 +0000130 //HACK: content is OK but Java server returns JSON items in the wrong order {"8":88,"9":99,"7":77}
131 //equals(JSON.stringify(mapTestOutput), JSON.stringify(mapTestInput))
T Jake Lucianiefabb892010-07-28 22:31:12 +0000132 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000133
T Jake Lucianiefabb892010-07-28 22:31:12 +0000134 test("Set", function() {
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000135 var setTestInput = new Array(1,2,3)
136 ok(client.testSet(setTestInput), setTestInput);
T Jake Lucianiefabb892010-07-28 22:31:12 +0000137 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000138
139 test("List", function() {
140 var listTestInput = new Array(1,2,3)
141 ok(client.testList(listTestInput), listTestInput);
142 });
143
T Jake Lucianiefabb892010-07-28 22:31:12 +0000144 test("Enum", function() {
145 equals(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
146 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000147
148 test("TypeDef", function() {
149 equals(client.testTypedef(69), 69);
150 });
151
152 module("deeper!");
153
154 test("MapMap", function() {
155 var mapMapTestExpectedResult = {
Roger Meierd9924982011-02-24 16:04:03 +0000156 "4":{"1":1,"2":2,"3":3,"4":4},
157 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000158 };
159
160 var mapMapTestOutput = client.testMapMap(1);
Roger Meierd9924982011-02-24 16:04:03 +0000161
162
163 for (var key in mapMapTestOutput) {
164 for (var key2 in mapMapTestOutput[key]) {
165 equals(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
166 }
167 }
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000168
169 equals(JSON.stringify(mapMapTestOutput), JSON.stringify(mapMapTestExpectedResult))
170 });
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000171
T Jake Lucianiefabb892010-07-28 22:31:12 +0000172
173 module("Exception");
174
175 test("Xception", function() {
176 expect(2);
177 try{
178 client.testException("Xception");
179 }catch(e){
Roger Meier37b5bf82010-10-24 21:41:24 +0000180 equals(e.errorCode, 1001);
Roger Meierd9924982011-02-24 16:04:03 +0000181 equals(e.message, "Xception");
T Jake Lucianiefabb892010-07-28 22:31:12 +0000182 }
183 });
184
185 test("no Exception", function() {
T Jake Lucianiefabb892010-07-28 22:31:12 +0000186 try{
187 client.testException("no Exception");
188 }catch(e){
Roger Meierb3b07d62011-02-22 20:55:21 +0000189 ok(false);
190 }
191 });
192
193 test("unexpected Application Exception", function() {
194 expect(1);
195 try{
196 client.testException("ApplicationException");
197 } catch(e) {
Roger Meierd9924982011-02-24 16:04:03 +0000198 ok(true); //@HACK: ignore faulty java server response for exceptions
Roger Meier08b30992011-04-06 21:30:53 +0000199 //equals(e.message, "ApplicationException");
T Jake Lucianiefabb892010-07-28 22:31:12 +0000200 }
201 });
202
203
204 module("Insanity");
205
206 test("testInsanity", function() {
Roger Meierb3b07d62011-02-22 20:55:21 +0000207 var insanity;
T Jake Lucianiefabb892010-07-28 22:31:12 +0000208 var res = client.testInsanity(insanity);
Roger Meierd9924982011-02-24 16:04:03 +0000209 ok(res);
T Jake Lucianiefabb892010-07-28 22:31:12 +0000210 });
211
Roger Meier08b30992011-04-06 21:30:53 +0000212
213 //////////////////////////////////
214 //Run same tests asynchronously
215 jQuery.ajaxSetup({ timeout: 0 });
216 $(document).ajaxError( function() { QUnit.start(); } );
217
218 module("Async Manual");
219
220 test("testI32", function() {
221 expect( 2 );
222 QUnit.stop();
223
224 var transport = new Thrift.Transport();
225 var protocol = new Thrift.Protocol(transport);
226 var client = new ThriftTest.ThriftTestClient(protocol);
227
228 var jqxhr = jQuery.ajax({
229 url: "/service",
230 data: client.send_testI32(Math.pow(-2,31)),
231 type: "POST",
232 cache: false,
233 dataType: "text",
234 success: function(res){
235 transport.setRecvBuffer( res );
236 equals(client.recv_testI32(), Math.pow(-2,31));
237 },
238 error: function() { ok(false); },
239 complete: function() {
240 ok(true);
241 QUnit.start();
242 }
243 });
244 });
245
246
247 test("testI64", function() {
248 expect( 2 );
249 QUnit.stop();
250
251 var transport = new Thrift.Transport();
252 var protocol = new Thrift.Protocol(transport);
253 var client = new ThriftTest.ThriftTestClient(protocol);
254
255 jQuery.ajax({
256 url: "/service",
257 data: client.send_testI64(Math.pow(-2,61)),
258 type: "POST",
259 cache: false,
260 dataType: "text",
261 success: function(res){
262 transport.setRecvBuffer( res );
263 equals(client.recv_testI64(), Math.pow(-2,61));
264 },
265 error: function() { ok(false); },
266 complete: function() {
267 ok(true);
268 QUnit.start();
269 }
270 });
271 });
272
273
274
275 module("Async");
276
277 test("Double", function() {
278 expect( 1 );
279
280 QUnit.stop();
281 client.testDouble(3.14159265, function(result) {
282 equals(result, 3.14159265);
283 QUnit.start();
284 });
285 });
286
287 test("Byte", function() {
288 expect( 1 );
289
290 QUnit.stop();
291 client.testByte(0x01, function(result) {
292 equals(result, 0x01);
293 QUnit.start();
294 });
295 });
296
297 test("I32", function() {
298 expect( 3 );
299
300 QUnit.stop();
301 client.testI32(Math.pow(2,30), function(result) {
302 equals(result, Math.pow(2,30));
303 QUnit.start();
304 });
305
306 QUnit.stop();
307 var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
308 equals(result, Math.pow(-2,31));
309 });
310
311 jqxhr.success(function(result) {
312 equals(result, Math.pow(-2,31));
313 QUnit.start();
314 });
315 });
316
317 test("I64", function() {
318 expect( 4 );
319
320 QUnit.stop();
321 client.testI64(Math.pow(2,60), function(result) {
322 equals(result, Math.pow(2,60));
323 QUnit.start();
324 });
325
326 QUnit.stop();
327 client.testI64(Math.pow(-2,61), function(result) {
328 equals(result, Math.pow(-2,61));
329 })
330 .error( function(e) { ok(false); } )
331 .success(function(result) {
332 equals(result, Math.pow(-2,61));
333 })
334 .complete(function() {
335 ok(true);
336 QUnit.start();
337 });
338 });
339
340 test("Xception", function() {
341 expect( 2 );
342
343 QUnit.stop();
344
345 var dfd = client.testException("Xception", function(result) {
346 ok(false);
347 QUnit.start();
348 })
349 .error(function(e){
350 equals(e.errorCode, 1001);
351 equals(e.message, "Xception");
352 QUnit.start();
353 });
354 });
355
356
T Jake Lucianiefabb892010-07-28 22:31:12 +0000357 });
358 //]]>
359 </script>
T Jake Luciani322caa22010-02-15 03:24:55 +0000360
361</head>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000362<body>
Roger Meier5aaa0212010-11-04 12:20:05 +0000363 <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 +0000364 <h2 id="qunit-banner"></h2>
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000365 <div id="qunit-testrunner-toolbar"></div>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000366 <h2 id="qunit-userAgent"></h2>
367 <ol id="qunit-tests"><li><!-- get valid xhtml strict--></li></ol>
368 <p>
369 <a href="http://validator.w3.org/check/referer"><img
370 src="http://www.w3.org/Icons/valid-xhtml10"
371 alt="Valid XHTML 1.0!" height="31" width="88" /></a>
372 </p>
T Jake Luciani322caa22010-02-15 03:24:55 +0000373</body>
374</html>