blob: bd124afa72b909fd74f4a1e932fb4bbb10775fd4 [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 Meierb3b07d62011-02-22 20:55:21 +0000218 var insanity;
T Jake Lucianiefabb892010-07-28 22:31:12 +0000219 var res = client.testInsanity(insanity);
Roger Meierd9924982011-02-24 16:04:03 +0000220 ok(res);
T Jake Lucianiefabb892010-07-28 22:31:12 +0000221 });
222
Roger Meier08b30992011-04-06 21:30:53 +0000223
224 //////////////////////////////////
225 //Run same tests asynchronously
226 jQuery.ajaxSetup({ timeout: 0 });
227 $(document).ajaxError( function() { QUnit.start(); } );
228
229 module("Async Manual");
230
231 test("testI32", function() {
232 expect( 2 );
233 QUnit.stop();
234
235 var transport = new Thrift.Transport();
236 var protocol = new Thrift.Protocol(transport);
237 var client = new ThriftTest.ThriftTestClient(protocol);
238
239 var jqxhr = jQuery.ajax({
240 url: "/service",
241 data: client.send_testI32(Math.pow(-2,31)),
242 type: "POST",
243 cache: false,
244 dataType: "text",
245 success: function(res){
246 transport.setRecvBuffer( res );
247 equals(client.recv_testI32(), Math.pow(-2,31));
248 },
249 error: function() { ok(false); },
250 complete: function() {
251 ok(true);
252 QUnit.start();
253 }
254 });
255 });
256
257
258 test("testI64", function() {
259 expect( 2 );
260 QUnit.stop();
261
262 var transport = new Thrift.Transport();
263 var protocol = new Thrift.Protocol(transport);
264 var client = new ThriftTest.ThriftTestClient(protocol);
265
266 jQuery.ajax({
267 url: "/service",
268 data: client.send_testI64(Math.pow(-2,61)),
269 type: "POST",
270 cache: false,
271 dataType: "text",
272 success: function(res){
273 transport.setRecvBuffer( res );
274 equals(client.recv_testI64(), Math.pow(-2,61));
275 },
276 error: function() { ok(false); },
277 complete: function() {
278 ok(true);
279 QUnit.start();
280 }
281 });
282 });
283
284
285
286 module("Async");
287
288 test("Double", function() {
289 expect( 1 );
290
291 QUnit.stop();
292 client.testDouble(3.14159265, function(result) {
293 equals(result, 3.14159265);
294 QUnit.start();
295 });
296 });
297
298 test("Byte", function() {
299 expect( 1 );
300
301 QUnit.stop();
302 client.testByte(0x01, function(result) {
303 equals(result, 0x01);
304 QUnit.start();
305 });
306 });
307
308 test("I32", function() {
309 expect( 3 );
310
311 QUnit.stop();
312 client.testI32(Math.pow(2,30), function(result) {
313 equals(result, Math.pow(2,30));
314 QUnit.start();
315 });
316
317 QUnit.stop();
318 var jqxhr = client.testI32(Math.pow(-2,31), function(result) {
319 equals(result, Math.pow(-2,31));
320 });
321
322 jqxhr.success(function(result) {
323 equals(result, Math.pow(-2,31));
324 QUnit.start();
325 });
326 });
327
328 test("I64", function() {
329 expect( 4 );
330
331 QUnit.stop();
332 client.testI64(Math.pow(2,60), function(result) {
333 equals(result, Math.pow(2,60));
334 QUnit.start();
335 });
336
337 QUnit.stop();
338 client.testI64(Math.pow(-2,61), function(result) {
339 equals(result, Math.pow(-2,61));
340 })
341 .error( function(e) { ok(false); } )
342 .success(function(result) {
343 equals(result, Math.pow(-2,61));
344 })
345 .complete(function() {
346 ok(true);
347 QUnit.start();
348 });
349 });
350
351 test("Xception", function() {
352 expect( 2 );
353
354 QUnit.stop();
355
356 var dfd = client.testException("Xception", function(result) {
357 ok(false);
358 QUnit.start();
359 })
360 .error(function(e){
361 equals(e.errorCode, 1001);
362 equals(e.message, "Xception");
363 QUnit.start();
364 });
365 });
366
367
T Jake Lucianiefabb892010-07-28 22:31:12 +0000368 });
369 //]]>
370 </script>
T Jake Luciani322caa22010-02-15 03:24:55 +0000371
372</head>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000373<body>
Roger Meier5aaa0212010-11-04 12:20:05 +0000374 <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 +0000375 <h2 id="qunit-banner"></h2>
Bryan Duxbury3ff373e2010-08-15 23:19:06 +0000376 <div id="qunit-testrunner-toolbar"></div>
T Jake Lucianiefabb892010-07-28 22:31:12 +0000377 <h2 id="qunit-userAgent"></h2>
378 <ol id="qunit-tests"><li><!-- get valid xhtml strict--></li></ol>
379 <p>
380 <a href="http://validator.w3.org/check/referer"><img
381 src="http://www.w3.org/Icons/valid-xhtml10"
382 alt="Valid XHTML 1.0!" height="31" width="88" /></a>
383 </p>
T Jake Luciani322caa22010-02-15 03:24:55 +0000384</body>
385</html>