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 |
| 81 | 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ú, 粵語"; |
| 82 | client.testString(stringTest, function(err, response) { |
| 83 | assert(!err); |
| 84 | assert.equal(stringTest, response); |
| 85 | }); |
| 86 | |
| 87 | var specialCharacters = 'quote: \" backslash:' + |
| 88 | ' forwardslash-escaped: \/ ' + |
| 89 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + |
| 90 | ' now-all-of-them-together: "\\\/\b\n\r\t' + |
| 91 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; |
| 92 | client.testString(specialCharacters, function(err, response) { |
| 93 | assert(!err); |
| 94 | assert.equal(specialCharacters, response); |
| 95 | }); |
| 96 | |
| 97 | |
| 98 | client.testByte(1, function(err, response) { |
| 99 | assert(!err); |
| 100 | assert.equal(1, response); |
| 101 | }); |
| 102 | client.testByte(0, function(err, response) { |
| 103 | assert(!err); |
| 104 | assert.equal(0, response); |
| 105 | }); |
| 106 | client.testByte(-1, function(err, response) { |
| 107 | assert(!err); |
| 108 | assert.equal(-1, response); |
| 109 | }); |
| 110 | client.testByte(-127, function(err, response) { |
| 111 | assert(!err); |
| 112 | assert.equal(-127, response); |
| 113 | }); |
| 114 | |
| 115 | client.testI32(-1, function(err, response) { |
| 116 | assert(!err); |
| 117 | assert.equal(-1, response); |
| 118 | }); |
| 119 | |
| 120 | client.testI64(5, function(err, response) { |
| 121 | assert(!err); |
| 122 | assert.equal(5, response); |
| 123 | }); |
| 124 | client.testI64(-5, function(err, response) { |
| 125 | assert(!err); |
| 126 | assert.equal(-5, response); |
| 127 | }); |
| 128 | client.testI64(-34359738368, function(err, response) { |
| 129 | assert(!err); |
| 130 | assert.equal(-34359738368, response); |
| 131 | }); |
| 132 | |
| 133 | client.testDouble(-5.2098523, function(err, response) { |
| 134 | assert(!err); |
| 135 | assert.equal(-5.2098523, response); |
| 136 | }); |
| 137 | client.testDouble(7.012052175215044, function(err, response) { |
| 138 | assert(!err); |
| 139 | assert.equal(7.012052175215044, response); |
| 140 | }); |
| 141 | |
| 142 | |
| 143 | var out = new ttypes.Xtruct({ |
| 144 | string_thing: 'Zero', |
| 145 | byte_thing: 1, |
| 146 | i32_thing: -3, |
| 147 | i64_thing: 1000000 |
| 148 | }); |
| 149 | client.testStruct(out, function(err, response) { |
| 150 | assert(!err); |
| 151 | checkRecursively(out, response); |
| 152 | }); |
| 153 | |
| 154 | |
| 155 | var out2 = new ttypes.Xtruct2(); |
| 156 | out2.byte_thing = 1; |
| 157 | out2.struct_thing = out; |
| 158 | out2.i32_thing = 5; |
| 159 | client.testNest(out2, function(err, response) { |
| 160 | assert(!err); |
| 161 | checkRecursively(out2, response); |
| 162 | }); |
| 163 | |
| 164 | |
| 165 | var mapout = {}; |
| 166 | for (var i = 0; i < 5; ++i) { |
| 167 | mapout[i] = i - 10; |
| 168 | } |
| 169 | client.testMap(mapout, function(err, response) { |
| 170 | assert(!err); |
| 171 | assert.deepEqual(mapout, response); |
| 172 | }); |
| 173 | |
| 174 | |
| 175 | var mapTestInput = { |
| 176 | "a": "123", |
| 177 | "a b": "with spaces ", |
| 178 | "same": "same", |
| 179 | "0": "numeric key", |
| 180 | "longValue": stringTest, |
| 181 | stringTest: "long key" |
| 182 | }; |
| 183 | client.testStringMap(mapTestInput, function(err, response) { |
| 184 | assert(!err); |
| 185 | assert.deepEqual(mapTestInput, response); |
| 186 | }); |
| 187 | |
| 188 | |
| 189 | var setTestInput = [1, 2, 3]; |
| 190 | client.testSet(setTestInput, function(err, response) { |
| 191 | assert(!err); |
| 192 | assert.deepEqual(setTestInput, response); |
| 193 | }); |
| 194 | client.testList(setTestInput, function(err, response) { |
| 195 | assert(!err); |
| 196 | assert.deepEqual(setTestInput, response); |
| 197 | }); |
| 198 | |
| 199 | client.testEnum(ttypes.Numberz.ONE, function(err, response) { |
| 200 | assert(!err); |
| 201 | assert.equal(ttypes.Numberz.ONE, response); |
| 202 | }); |
| 203 | |
| 204 | client.testTypedef(69, function(err, response) { |
| 205 | assert(!err); |
| 206 | assert.equal(69, response); |
| 207 | }); |
| 208 | |
| 209 | |
| 210 | var mapMapTest = { |
| 211 | "4": { |
| 212 | "1": 1, |
| 213 | "2": 2, |
| 214 | "3": 3, |
| 215 | "4": 4 |
| 216 | }, |
| 217 | "-4": { |
| 218 | "-4": -4, |
| 219 | "-3": -3, |
| 220 | "-2": -2, |
| 221 | "-1": -1 |
| 222 | } |
| 223 | }; |
| 224 | client.testMapMap(mapMapTest, function(err, response) { |
| 225 | assert(!err); |
| 226 | assert.deepEqual(mapMapTest, response); |
| 227 | }); |
| 228 | |
| 229 | var crazy = new ttypes.Insanity({ |
| 230 | "userMap": { |
| 231 | "5": 5, |
| 232 | "8": 8 |
| 233 | }, |
| 234 | "xtructs": [new ttypes.Xtruct({ |
| 235 | "string_thing": "Goodbye4", |
| 236 | "byte_thing": 4, |
| 237 | "i32_thing": 4, |
| 238 | "i64_thing": 4 |
| 239 | }), new ttypes.Xtruct({ |
| 240 | "string_thing": "Hello2", |
| 241 | "byte_thing": 2, |
| 242 | "i32_thing": 2, |
| 243 | "i64_thing": 2 |
| 244 | })] |
| 245 | }); |
| 246 | var insanity = { |
| 247 | "1": { |
| 248 | "2": crazy, |
| 249 | "3": crazy |
| 250 | }, |
| 251 | "2": { |
| 252 | "6": { |
| 253 | "userMap": null, |
| 254 | "xtructs": null |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | client.testInsanity(crazy, function(err, response) { |
| 259 | assert(!err); |
| 260 | checkRecursively(insanity, response); |
| 261 | }); |
| 262 | |
| 263 | |
| 264 | client.testException('TException', function(err, response) { |
| 265 | //assert(err); //BUG? |
| 266 | assert(!response); |
| 267 | }); |
| 268 | client.testException('Xception', function(err, response) { |
| 269 | assert(!response); |
| 270 | assert.equal(err.errorCode, 1001); |
| 271 | assert.equal('Xception', err.message); |
| 272 | }); |
| 273 | client.testException('no Exception', function(err, response) { |
| 274 | assert(!err); |
| 275 | assert.equal(undefined, response); //void |
| 276 | }); |
| 277 | |
| 278 | |
| 279 | client.testOneway(1, function(err, response) { |
| 280 | assert(!response); //should not answer |
| 281 | }); |
| 282 | |
| 283 | /** |
| 284 | * redo a simple test after the oneway to make sure we aren't "off by one" -- |
| 285 | * if the server treated oneway void like normal void, this next test will |
| 286 | * fail since it will get the void confirmation rather than the correct |
| 287 | * result. In this circumstance, the client will throw the exception: |
| 288 | * |
| 289 | * TApplicationException: Wrong method namea |
| 290 | */ |
| 291 | client.testI32(-1, function(err, response) { |
| 292 | assert(!err); |
| 293 | assert.equal(-1, response); |
| 294 | }); |
| 295 | |
| 296 | setTimeout(function() { |
| 297 | console.log("Server successfully tested!"); |
| 298 | connection.end(); |
| 299 | }, 1500); |
| 300 | |
| 301 | // to make it also run on expresso |
| 302 | exports.expressoTest = function() {}; |