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