Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [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'); |
Henrique Mendonça | 6afe053 | 2013-06-09 22:54:51 +0200 | [diff] [blame] | 20 | var ttransport = require('transport'); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 21 | var assert = require('assert'); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 22 | |
| 23 | var ThriftTest = require('./gen-nodejs/ThriftTest'), |
| 24 | ttypes = require('./gen-nodejs/ThriftTest_types'); |
| 25 | |
Henrique Mendonça | 6afe053 | 2013-06-09 22:54:51 +0200 | [diff] [blame] | 26 | var connection = thrift.createConnection('localhost', 9090, { 'transport': ttransport.TFramedTransport }), |
| 27 | //var connection = thrift.createConnection('localhost', 9090), |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 28 | client = thrift.createClient(ThriftTest, connection); |
| 29 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 30 | connection.on('error', function(err) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 31 | assert(false, err); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 32 | }); |
| 33 | |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 34 | // deepEqual doesn't work with fields using node-int64 |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 35 | function checkRecursively(map1, map2) { |
| 36 | if (typeof map1 !== 'function' && typeof map2 !== 'function') { |
| 37 | if (!map1 || typeof map1 !== 'object') { |
| 38 | assert.equal(map1, map2); |
| 39 | } else { |
| 40 | for (var key in map1) { |
| 41 | checkRecursively(map1[key], map2[key]); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 46 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 47 | |
| 48 | client.testVoid(function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 49 | assert( ! err); |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 50 | assert.equal(undefined, response); //void |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 51 | }); |
| 52 | |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 53 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 54 | client.testString("Test", function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 55 | assert( ! err); |
| 56 | assert.equal("Test", response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 57 | }); |
| 58 | |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 59 | client.testString("", function(err, response) { |
| 60 | assert( ! err); |
| 61 | assert.equal("", response); |
| 62 | }); |
| 63 | |
| 64 | // all Languages in UTF-8 |
| 65 | 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ú, 粵語"; |
| 66 | client.testString(stringTest, function(err, response) { |
| 67 | assert( ! err); |
| 68 | assert.equal(stringTest, response); |
| 69 | }); |
| 70 | |
| 71 | var specialCharacters = 'quote: \" backslash:' + |
| 72 | ' forwardslash-escaped: \/ ' + |
| 73 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + |
| 74 | ' now-all-of-them-together: "\\\/\b\n\r\t' + |
| 75 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; |
| 76 | client.testString(specialCharacters, function(err, response) { |
| 77 | assert( ! err); |
| 78 | assert.equal(specialCharacters, response); |
| 79 | }); |
| 80 | |
| 81 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 82 | client.testByte(1, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 83 | assert( ! err); |
| 84 | assert.equal(1, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 85 | }); |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 86 | client.testByte(0, function(err, response) { |
| 87 | assert( ! err); |
| 88 | assert.equal(0, response); |
| 89 | }); |
| 90 | client.testByte(-1, function(err, response) { |
| 91 | assert( ! err); |
| 92 | assert.equal(-1, response); |
| 93 | }); |
| 94 | client.testByte(-127, function(err, response) { |
| 95 | assert( ! err); |
| 96 | assert.equal(-127, response); |
| 97 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 98 | |
| 99 | client.testI32(-1, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 100 | assert( ! err); |
| 101 | assert.equal(-1, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 102 | }); |
| 103 | |
| 104 | client.testI64(5, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 105 | assert( ! err); |
| 106 | assert.equal(5, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 107 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 108 | client.testI64(-5, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 109 | assert( ! err); |
| 110 | assert.equal(-5, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 111 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 112 | client.testI64(-34359738368, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 113 | assert( ! err); |
| 114 | assert.equal(-34359738368, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 115 | }); |
| 116 | |
| 117 | client.testDouble(-5.2098523, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 118 | assert( ! err); |
| 119 | assert.equal(-5.2098523, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 120 | }); |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 121 | client.testDouble(7.012052175215044, function(err, response) { |
| 122 | assert( ! err); |
| 123 | assert.equal(7.012052175215044, response); |
| 124 | }); |
| 125 | |
| 126 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 127 | var out = new ttypes.Xtruct({ |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 128 | string_thing: 'Zero', |
| 129 | byte_thing: 1, |
| 130 | i32_thing: -3, |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 131 | i64_thing: 1000000 |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 132 | }); |
| 133 | client.testStruct(out, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 134 | assert( ! err); |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 135 | checkRecursively(out, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 136 | }); |
| 137 | |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 138 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 139 | var out2 = new ttypes.Xtruct2(); |
| 140 | out2.byte_thing = 1; |
| 141 | out2.struct_thing = out; |
| 142 | out2.i32_thing = 5; |
| 143 | client.testNest(out2, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 144 | assert( ! err); |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame] | 145 | checkRecursively(out2, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 146 | }); |
| 147 | |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 148 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 149 | var mapout = {}; |
| 150 | for (var i = 0; i < 5; ++i) { |
| 151 | mapout[i] = i-10; |
| 152 | } |
| 153 | client.testMap(mapout, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 154 | assert( ! err); |
| 155 | assert.deepEqual(mapout, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 156 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 157 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 158 | |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 159 | var mapTestInput = { |
| 160 | "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key", |
| 161 | "longValue":stringTest, stringTest:"long key" |
| 162 | }; |
| 163 | client.testStringMap(mapTestInput, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 164 | assert( ! err); |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 165 | assert.deepEqual(mapTestInput, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 166 | }); |
| 167 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 168 | |
Henrique | 08c34ea | 2013-05-11 19:29:19 +0200 | [diff] [blame] | 169 | var setTestInput = [1,2,3]; |
| 170 | client.testSet(setTestInput, function(err, response) { |
| 171 | assert( ! err); |
| 172 | assert.deepEqual(setTestInput, response); |
| 173 | }); |
| 174 | client.testList(setTestInput, function(err, response) { |
| 175 | assert( ! err); |
| 176 | assert.deepEqual(setTestInput, response); |
| 177 | }); |
| 178 | |
| 179 | client.testEnum(ttypes.Numberz.ONE, function(err, response) { |
| 180 | assert( ! err); |
| 181 | assert.equal(ttypes.Numberz.ONE, response); |
| 182 | }); |
| 183 | |
| 184 | client.testTypedef(69, function(err, response) { |
| 185 | assert( ! err); |
| 186 | assert.equal(69, response); |
| 187 | }); |
| 188 | |
| 189 | |
| 190 | var mapMapTest = { |
| 191 | "4": {"1":1, "2":2, "3":3, "4":4}, |
| 192 | "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1} |
| 193 | }; |
| 194 | client.testMapMap(mapMapTest, function(err, response) { |
| 195 | assert( ! err); |
| 196 | assert.deepEqual(mapMapTest, response); |
| 197 | }); |
| 198 | |
| 199 | var crazy = new ttypes.Insanity({ |
| 200 | "userMap":{ "5":5, "8":8 }, |
| 201 | "xtructs":[new ttypes.Xtruct({ |
| 202 | "string_thing":"Goodbye4", |
| 203 | "byte_thing":4, |
| 204 | "i32_thing":4, |
| 205 | "i64_thing":4 |
| 206 | }), new ttypes.Xtruct({ |
| 207 | "string_thing":"Hello2", |
| 208 | "byte_thing":2, |
| 209 | "i32_thing":2, |
| 210 | "i64_thing":2 |
| 211 | })] |
| 212 | }); |
| 213 | var insanity = { |
| 214 | "1":{ "2": crazy, "3": crazy }, |
| 215 | "2":{ "6":{ "userMap":null, "xtructs":null } } |
| 216 | }; |
| 217 | client.testInsanity(crazy, function(err, response) { |
| 218 | assert( ! err); |
| 219 | checkRecursively(insanity, response); |
| 220 | }); |
| 221 | |
| 222 | |
| 223 | client.testException('TException', function(err, response) { |
| 224 | //assert(err); //BUG? |
| 225 | assert( ! response); |
| 226 | }); |
| 227 | client.testException('Xception', function(err, response) { |
| 228 | assert( ! response); |
| 229 | assert.equal(err.errorCode, 1001); |
| 230 | assert.equal('Xception', err.message); |
| 231 | }); |
| 232 | client.testException('no Exception', function(err, response) { |
| 233 | assert( ! err); |
| 234 | assert.equal(undefined, response); //void |
| 235 | }); |
| 236 | |
| 237 | |
| 238 | client.testOneway(1, function(err, response) { |
| 239 | assert(false); //should not answer |
| 240 | }); |
| 241 | |
| 242 | /** |
| 243 | * redo a simple test after the oneway to make sure we aren't "off by one" -- |
| 244 | * if the server treated oneway void like normal void, this next test will |
| 245 | * fail since it will get the void confirmation rather than the correct |
| 246 | * result. In this circumstance, the client will throw the exception: |
| 247 | * |
| 248 | * TApplicationException: Wrong method namea |
| 249 | */ |
| 250 | client.testI32(-1, function(err, response) { |
| 251 | assert( ! err); |
| 252 | assert.equal(-1, response); |
| 253 | }); |
| 254 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 255 | setTimeout(function() { |
| 256 | console.log("Server successfully tested!"); |
| 257 | connection.end(); |
Henrique Mendonça | 6afe053 | 2013-06-09 22:54:51 +0200 | [diff] [blame] | 258 | }, 1500); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 259 | |
| 260 | // to make it also run on expresso |
| 261 | exports.expressoTest = function() {}; |
| 262 | |