| henrique | 2fdd916 | 2013-08-28 14:03:34 +0200 | [diff] [blame] | 1 | /* | 
 | 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 | var thrift = require('thrift'); | 
| Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 20 | var ThriftTransports = require('thrift/transport'); | 
 | 21 | var ThriftProtocols = require('thrift/protocol'); | 
| henrique | 2fdd916 | 2013-08-28 14:03:34 +0200 | [diff] [blame] | 22 | var assert = require('assert'); | 
 | 23 |  | 
 | 24 | var ThriftTest = require('./gen-nodejs/ThriftTest'), | 
 | 25 |     SecondService = require('./gen-nodejs/SecondService'), | 
 | 26 |     ttypes = require('./gen-nodejs/ThriftTest_types'); | 
 | 27 |  | 
| Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 28 | var program = require('commander'); | 
 | 29 |  | 
 | 30 | program | 
 | 31 |   .option('-p, --protocol <protocol>', 'Set thift protocol (binary|json) [protocol]') | 
 | 32 |   .option('-t, --transport <transport>', 'Set thift transport (buffered|framed) [transport]') | 
| Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 33 |   .option('--ssl', 'use ssl transport') | 
| Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 34 |   .parse(process.argv); | 
 | 35 |  | 
 | 36 | var protocol = undefined; | 
 | 37 | var transport =  undefined; | 
 | 38 |  | 
 | 39 | if (program.protocol === "binary") { | 
 | 40 |   protocol = ThriftProtocols.TBinaryProtocol; | 
 | 41 | } else if (program.protocol === "json") { | 
 | 42 |   protocol = ThriftProtocols.TJSONProtocol; | 
 | 43 | } else { | 
 | 44 |   //default | 
 | 45 |   protocol = ThriftProtocols.TBinaryProtocol; | 
 | 46 | } | 
 | 47 |  | 
 | 48 | if (program.transport === "framed") { | 
 | 49 |   transport = ThriftTransports.TFramedTransport; | 
 | 50 | } else if (program.transport === "buffered") { | 
 | 51 |   transport = ThriftTransports.TBufferedTransport; | 
 | 52 | } else { | 
 | 53 |   //default | 
 | 54 |   transport = ThriftTransports.TBufferedTransport; | 
 | 55 | } | 
 | 56 |  | 
| Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 57 | var options = { | 
| Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 58 |   transport: transport, | 
 | 59 |   protocol: protocol | 
| Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 60 | }; | 
 | 61 |  | 
 | 62 | var connection = undefined; | 
 | 63 | if (program.ssl) { | 
 | 64 |   options.rejectUnauthorized = false | 
 | 65 |   connection = thrift.createSSLConnection('localhost', 9090, options); | 
 | 66 | } else { | 
 | 67 |   connection = thrift.createConnection('localhost', 9090, options); | 
 | 68 | } | 
| henrique | 2fdd916 | 2013-08-28 14:03:34 +0200 | [diff] [blame] | 69 |  | 
 | 70 | var mp = new thrift.Multiplexer(); | 
 | 71 |  | 
 | 72 | client = mp.createClient("ThriftTest", ThriftTest, connection); | 
 | 73 | secondclient = mp.createClient("SecondService", SecondService, connection); | 
 | 74 |  | 
 | 75 | connection.on('error', function(err) { | 
 | 76 |     assert(false, err); | 
 | 77 | }); | 
 | 78 |  | 
 | 79 | // deepEqual doesn't work with fields using node-int64 | 
 | 80 |  | 
 | 81 | function checkRecursively(map1, map2) { | 
 | 82 |     if (typeof map1 !== 'function' && typeof map2 !== 'function') { | 
 | 83 |         if (!map1 || typeof map1 !== 'object') { | 
 | 84 |             assert.equal(map1, map2); | 
 | 85 |         } else { | 
 | 86 |             for (var key in map1) { | 
 | 87 |                 checkRecursively(map1[key], map2[key]); | 
 | 88 |             } | 
 | 89 |         } | 
 | 90 |     } | 
 | 91 | } | 
 | 92 |  | 
 | 93 | client.testString("Test", function(err, response) { | 
 | 94 |     assert(!err); | 
 | 95 |     assert.equal("Test", response); | 
 | 96 | }); | 
 | 97 | secondclient.secondtestString("Test", function(err, response) { | 
 | 98 |     assert(!err); | 
 | 99 |     assert.equal("Test", response); | 
 | 100 | }); | 
 | 101 |  | 
 | 102 |  | 
 | 103 | client.testVoid(function(err, response) { | 
 | 104 |     assert(!err); | 
 | 105 |     assert.equal(undefined, response); //void | 
 | 106 | }); | 
 | 107 |  | 
 | 108 |  | 
 | 109 | secondclient.secondtestString("Test", function(err, response) { | 
 | 110 |     assert(!err); | 
 | 111 |     assert.equal("Test", response); | 
 | 112 | }); | 
 | 113 |  | 
 | 114 | client.testString("", function(err, response) { | 
 | 115 |     assert(!err); | 
 | 116 |     assert.equal("", response); | 
 | 117 | }); | 
 | 118 |  | 
 | 119 | // all Languages in UTF-8 | 
| henrique | 216374e | 2014-01-14 15:17:04 +0100 | [diff] [blame] | 120 | var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " + | 
 | 121 |     "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " + | 
 | 122 |     "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, " + | 
 | 123 |     "Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, Cebuano, ᏣᎳᎩ, " + | 
 | 124 |     "Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, Dansk, Zazaki, " + | 
 | 125 |     "ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, Esperanto, " + | 
 | 126 |     "Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, Français, " + | 
 | 127 |     "Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, Avañe'ẽ, " + | 
 | 128 |     "ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, Kreyòl ayisyen, " + | 
 | 129 |     "Magyar, Հայերեն, Interlingua, Bahasa Indonesia, Ilokano, Ido, " + | 
 | 130 |     "Íslenska, Italiano, 日本語, Lojban, Basa Jawa, ქართული, Kongo, " + | 
 | 131 |     "Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, Ripoarisch, Kurdî, Коми, " + | 
 | 132 |     "Kernewek, Кыргызча, Latina, Ladino, Lëtzebuergesch, Limburgs, " + | 
 | 133 |     "Lingála, ລາວ, Lietuvių, Latviešu, Basa Banyumasan, Malagasy, " + | 
 | 134 |     "Македонски, മലയാളം, मराठी, Bahasa Melayu, مازِرونی, Nnapulitano, " + | 
 | 135 |     "Nedersaksisch, नेपाल भाषा, Nederlands, Norsk (nynorsk), Norsk (" + | 
 | 136 |     "bokmål), Nouormand, Diné bizaad, Occitan, Иронау, Papiamentu, " + | 
 | 137 |     "Deitsch, Norfuk / Pitkern, Polski, پنجابی, پښتو, Português, Runa " + | 
 | 138 |     "Simi, Rumantsch, Romani, Română, Русский, Саха тыла, Sardu, " + | 
 | 139 |     "Sicilianu, Scots, Sámegiella, Simple English, Slovenčina, " + | 
 | 140 |     "Slovenščina, Српски / Srpski, Seeltersk, Svenska, Kiswahili, தமிழ், " + | 
 | 141 |     "తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, Türkçe, Татарча/Tatarça, " + | 
 | 142 |     "Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, " + | 
 | 143 |     "ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語"; | 
 | 144 |  | 
| henrique | 2fdd916 | 2013-08-28 14:03:34 +0200 | [diff] [blame] | 145 | client.testString(stringTest, function(err, response) { | 
 | 146 |     assert(!err); | 
 | 147 |     assert.equal(stringTest, response); | 
 | 148 | }); | 
 | 149 |  | 
 | 150 | var specialCharacters = 'quote: \" backslash:' + | 
 | 151 |     ' forwardslash-escaped: \/ ' + | 
 | 152 |     ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + | 
 | 153 |     ' now-all-of-them-together: "\\\/\b\n\r\t' + | 
 | 154 |     ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; | 
 | 155 | client.testString(specialCharacters, function(err, response) { | 
 | 156 |     assert(!err); | 
 | 157 |     assert.equal(specialCharacters, response); | 
 | 158 | }); | 
 | 159 |  | 
 | 160 |  | 
 | 161 | client.testByte(1, function(err, response) { | 
 | 162 |     assert(!err); | 
 | 163 |     assert.equal(1, response); | 
 | 164 | }); | 
 | 165 | client.testByte(0, function(err, response) { | 
 | 166 |     assert(!err); | 
 | 167 |     assert.equal(0, response); | 
 | 168 | }); | 
 | 169 | client.testByte(-1, function(err, response) { | 
 | 170 |     assert(!err); | 
 | 171 |     assert.equal(-1, response); | 
 | 172 | }); | 
 | 173 | client.testByte(-127, function(err, response) { | 
 | 174 |     assert(!err); | 
 | 175 |     assert.equal(-127, response); | 
 | 176 | }); | 
 | 177 |  | 
 | 178 | client.testI32(-1, function(err, response) { | 
 | 179 |     assert(!err); | 
 | 180 |     assert.equal(-1, response); | 
 | 181 | }); | 
 | 182 |  | 
 | 183 | client.testI64(5, function(err, response) { | 
 | 184 |     assert(!err); | 
 | 185 |     assert.equal(5, response); | 
 | 186 | }); | 
 | 187 | client.testI64(-5, function(err, response) { | 
 | 188 |     assert(!err); | 
 | 189 |     assert.equal(-5, response); | 
 | 190 | }); | 
 | 191 | client.testI64(-34359738368, function(err, response) { | 
 | 192 |     assert(!err); | 
 | 193 |     assert.equal(-34359738368, response); | 
 | 194 | }); | 
 | 195 |  | 
 | 196 | client.testDouble(-5.2098523, function(err, response) { | 
 | 197 |     assert(!err); | 
 | 198 |     assert.equal(-5.2098523, response); | 
 | 199 | }); | 
 | 200 | client.testDouble(7.012052175215044, function(err, response) { | 
 | 201 |     assert(!err); | 
 | 202 |     assert.equal(7.012052175215044, response); | 
 | 203 | }); | 
 | 204 |  | 
 | 205 |  | 
 | 206 | var out = new ttypes.Xtruct({ | 
 | 207 |     string_thing: 'Zero', | 
 | 208 |     byte_thing: 1, | 
 | 209 |     i32_thing: -3, | 
 | 210 |     i64_thing: 1000000 | 
 | 211 | }); | 
 | 212 | client.testStruct(out, function(err, response) { | 
 | 213 |     assert(!err); | 
 | 214 |     checkRecursively(out, response); | 
 | 215 | }); | 
 | 216 |  | 
 | 217 |  | 
 | 218 | var out2 = new ttypes.Xtruct2(); | 
 | 219 | out2.byte_thing = 1; | 
 | 220 | out2.struct_thing = out; | 
 | 221 | out2.i32_thing = 5; | 
 | 222 | client.testNest(out2, function(err, response) { | 
 | 223 |     assert(!err); | 
 | 224 |     checkRecursively(out2, response); | 
 | 225 | }); | 
 | 226 |  | 
 | 227 |  | 
 | 228 | var mapout = {}; | 
 | 229 | for (var i = 0; i < 5; ++i) { | 
 | 230 |     mapout[i] = i - 10; | 
 | 231 | } | 
 | 232 | client.testMap(mapout, function(err, response) { | 
 | 233 |     assert(!err); | 
 | 234 |     assert.deepEqual(mapout, response); | 
 | 235 | }); | 
 | 236 |  | 
 | 237 |  | 
 | 238 | var mapTestInput = { | 
 | 239 |     "a": "123", | 
 | 240 |     "a b": "with spaces ", | 
 | 241 |     "same": "same", | 
 | 242 |     "0": "numeric key", | 
 | 243 |     "longValue": stringTest, | 
 | 244 |     stringTest: "long key" | 
 | 245 | }; | 
 | 246 | client.testStringMap(mapTestInput, function(err, response) { | 
 | 247 |     assert(!err); | 
 | 248 |     assert.deepEqual(mapTestInput, response); | 
 | 249 | }); | 
 | 250 |  | 
 | 251 |  | 
 | 252 | var setTestInput = [1, 2, 3]; | 
 | 253 | client.testSet(setTestInput, function(err, response) { | 
 | 254 |     assert(!err); | 
 | 255 |     assert.deepEqual(setTestInput, response); | 
 | 256 | }); | 
 | 257 | client.testList(setTestInput, function(err, response) { | 
 | 258 |     assert(!err); | 
 | 259 |     assert.deepEqual(setTestInput, response); | 
 | 260 | }); | 
 | 261 |  | 
 | 262 | client.testEnum(ttypes.Numberz.ONE, function(err, response) { | 
 | 263 |     assert(!err); | 
 | 264 |     assert.equal(ttypes.Numberz.ONE, response); | 
 | 265 | }); | 
 | 266 |  | 
 | 267 | client.testTypedef(69, function(err, response) { | 
 | 268 |     assert(!err); | 
 | 269 |     assert.equal(69, response); | 
 | 270 | }); | 
 | 271 |  | 
 | 272 |  | 
 | 273 | var mapMapTest = { | 
 | 274 |     "4": { | 
 | 275 |         "1": 1, | 
 | 276 |         "2": 2, | 
 | 277 |         "3": 3, | 
 | 278 |         "4": 4 | 
 | 279 |     }, | 
 | 280 |     "-4": { | 
 | 281 |         "-4": -4, | 
 | 282 |         "-3": -3, | 
 | 283 |         "-2": -2, | 
 | 284 |         "-1": -1 | 
 | 285 |     } | 
 | 286 | }; | 
 | 287 | client.testMapMap(mapMapTest, function(err, response) { | 
 | 288 |     assert(!err); | 
 | 289 |     assert.deepEqual(mapMapTest, response); | 
 | 290 | }); | 
 | 291 |  | 
 | 292 | var crazy = new ttypes.Insanity({ | 
 | 293 |     "userMap": { | 
 | 294 |         "5": 5, | 
 | 295 |         "8": 8 | 
 | 296 |     }, | 
 | 297 |     "xtructs": [new ttypes.Xtruct({ | 
 | 298 |             "string_thing": "Goodbye4", | 
 | 299 |             "byte_thing": 4, | 
 | 300 |             "i32_thing": 4, | 
 | 301 |             "i64_thing": 4 | 
 | 302 |         }), new ttypes.Xtruct({ | 
 | 303 |             "string_thing": "Hello2", | 
 | 304 |             "byte_thing": 2, | 
 | 305 |             "i32_thing": 2, | 
 | 306 |             "i64_thing": 2 | 
 | 307 |         })] | 
 | 308 | }); | 
 | 309 | var insanity = { | 
 | 310 |     "1": { | 
 | 311 |         "2": crazy, | 
 | 312 |         "3": crazy | 
 | 313 |     }, | 
 | 314 |     "2": { | 
 | 315 |         "6": { | 
 | 316 |             "userMap": null, | 
 | 317 |             "xtructs": null | 
 | 318 |         } | 
 | 319 |     } | 
 | 320 | }; | 
 | 321 | client.testInsanity(crazy, function(err, response) { | 
 | 322 |     assert(!err); | 
 | 323 |     checkRecursively(insanity, response); | 
 | 324 | }); | 
 | 325 |  | 
 | 326 |  | 
 | 327 | client.testException('TException', function(err, response) { | 
 | 328 |     //assert(err); //BUG? | 
 | 329 |     assert(!response); | 
 | 330 | }); | 
 | 331 | client.testException('Xception', function(err, response) { | 
 | 332 |     assert(!response); | 
 | 333 |     assert.equal(err.errorCode, 1001); | 
 | 334 |     assert.equal('Xception', err.message); | 
 | 335 | }); | 
 | 336 | client.testException('no Exception', function(err, response) { | 
 | 337 |     assert(!err); | 
 | 338 |     assert.equal(undefined, response); //void | 
 | 339 | }); | 
 | 340 |  | 
 | 341 |  | 
 | 342 | client.testOneway(1, function(err, response) { | 
 | 343 |     assert(!response); //should not answer | 
 | 344 | }); | 
 | 345 |  | 
 | 346 | /** | 
 | 347 |  * redo a simple test after the oneway to make sure we aren't "off by one" -- | 
 | 348 |  * if the server treated oneway void like normal void, this next test will | 
 | 349 |  * fail since it will get the void confirmation rather than the correct | 
 | 350 |  * result. In this circumstance, the client will throw the exception: | 
 | 351 |  * | 
 | 352 |  *   TApplicationException: Wrong method namea | 
 | 353 |  */ | 
 | 354 | client.testI32(-1, function(err, response) { | 
 | 355 |     assert(!err); | 
 | 356 |     assert.equal(-1, response); | 
 | 357 | }); | 
 | 358 |  | 
 | 359 | setTimeout(function() { | 
 | 360 |     console.log("Server successfully tested!"); | 
 | 361 |     connection.end(); | 
 | 362 | }, 1500); | 
 | 363 |  | 
 | 364 | // to make it also run on expresso | 
 | 365 | exports.expressoTest = function() {}; |