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