blob: ba0d47b2fe0aad7682214be08e2e5b757ff0033d [file] [log] [blame]
henrique2fdd9162013-08-28 14:03:34 +02001/*
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 */
19var thrift = require('thrift');
20var ttransport = require('transport');
21var assert = require('assert');
22
23var ThriftTest = require('./gen-nodejs/ThriftTest'),
24 SecondService = require('./gen-nodejs/SecondService'),
25 ttypes = require('./gen-nodejs/ThriftTest_types');
26
27var connection = thrift.createConnection('localhost', 9090, {
28 'transport': ttransport.TFramedTransport
29});
30
31var mp = new thrift.Multiplexer();
32
33client = mp.createClient("ThriftTest", ThriftTest, connection);
34secondclient = mp.createClient("SecondService", SecondService, connection);
35
36connection.on('error', function(err) {
37 assert(false, err);
38});
39
40// deepEqual doesn't work with fields using node-int64
41
42function 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
54client.testString("Test", function(err, response) {
55 assert(!err);
56 assert.equal("Test", response);
57});
58secondclient.secondtestString("Test", function(err, response) {
59 assert(!err);
60 assert.equal("Test", response);
61});
62
63
64client.testVoid(function(err, response) {
65 assert(!err);
66 assert.equal(undefined, response); //void
67});
68
69
70secondclient.secondtestString("Test", function(err, response) {
71 assert(!err);
72 assert.equal("Test", response);
73});
74
75client.testString("", function(err, response) {
76 assert(!err);
77 assert.equal("", response);
78});
79
80// all Languages in UTF-8
henrique216374e2014-01-14 15:17:04 +010081var 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
henrique2fdd9162013-08-28 14:03:34 +0200106client.testString(stringTest, function(err, response) {
107 assert(!err);
108 assert.equal(stringTest, response);
109});
110
111var 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: !@#$%&()(&%$#{}{}<><><';
116client.testString(specialCharacters, function(err, response) {
117 assert(!err);
118 assert.equal(specialCharacters, response);
119});
120
121
122client.testByte(1, function(err, response) {
123 assert(!err);
124 assert.equal(1, response);
125});
126client.testByte(0, function(err, response) {
127 assert(!err);
128 assert.equal(0, response);
129});
130client.testByte(-1, function(err, response) {
131 assert(!err);
132 assert.equal(-1, response);
133});
134client.testByte(-127, function(err, response) {
135 assert(!err);
136 assert.equal(-127, response);
137});
138
139client.testI32(-1, function(err, response) {
140 assert(!err);
141 assert.equal(-1, response);
142});
143
144client.testI64(5, function(err, response) {
145 assert(!err);
146 assert.equal(5, response);
147});
148client.testI64(-5, function(err, response) {
149 assert(!err);
150 assert.equal(-5, response);
151});
152client.testI64(-34359738368, function(err, response) {
153 assert(!err);
154 assert.equal(-34359738368, response);
155});
156
157client.testDouble(-5.2098523, function(err, response) {
158 assert(!err);
159 assert.equal(-5.2098523, response);
160});
161client.testDouble(7.012052175215044, function(err, response) {
162 assert(!err);
163 assert.equal(7.012052175215044, response);
164});
165
166
167var out = new ttypes.Xtruct({
168 string_thing: 'Zero',
169 byte_thing: 1,
170 i32_thing: -3,
171 i64_thing: 1000000
172});
173client.testStruct(out, function(err, response) {
174 assert(!err);
175 checkRecursively(out, response);
176});
177
178
179var out2 = new ttypes.Xtruct2();
180out2.byte_thing = 1;
181out2.struct_thing = out;
182out2.i32_thing = 5;
183client.testNest(out2, function(err, response) {
184 assert(!err);
185 checkRecursively(out2, response);
186});
187
188
189var mapout = {};
190for (var i = 0; i < 5; ++i) {
191 mapout[i] = i - 10;
192}
193client.testMap(mapout, function(err, response) {
194 assert(!err);
195 assert.deepEqual(mapout, response);
196});
197
198
199var mapTestInput = {
200 "a": "123",
201 "a b": "with spaces ",
202 "same": "same",
203 "0": "numeric key",
204 "longValue": stringTest,
205 stringTest: "long key"
206};
207client.testStringMap(mapTestInput, function(err, response) {
208 assert(!err);
209 assert.deepEqual(mapTestInput, response);
210});
211
212
213var setTestInput = [1, 2, 3];
214client.testSet(setTestInput, function(err, response) {
215 assert(!err);
216 assert.deepEqual(setTestInput, response);
217});
218client.testList(setTestInput, function(err, response) {
219 assert(!err);
220 assert.deepEqual(setTestInput, response);
221});
222
223client.testEnum(ttypes.Numberz.ONE, function(err, response) {
224 assert(!err);
225 assert.equal(ttypes.Numberz.ONE, response);
226});
227
228client.testTypedef(69, function(err, response) {
229 assert(!err);
230 assert.equal(69, response);
231});
232
233
234var 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};
248client.testMapMap(mapMapTest, function(err, response) {
249 assert(!err);
250 assert.deepEqual(mapMapTest, response);
251});
252
253var 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});
270var insanity = {
271 "1": {
272 "2": crazy,
273 "3": crazy
274 },
275 "2": {
276 "6": {
277 "userMap": null,
278 "xtructs": null
279 }
280 }
281};
282client.testInsanity(crazy, function(err, response) {
283 assert(!err);
284 checkRecursively(insanity, response);
285});
286
287
288client.testException('TException', function(err, response) {
289 //assert(err); //BUG?
290 assert(!response);
291});
292client.testException('Xception', function(err, response) {
293 assert(!response);
294 assert.equal(err.errorCode, 1001);
295 assert.equal('Xception', err.message);
296});
297client.testException('no Exception', function(err, response) {
298 assert(!err);
299 assert.equal(undefined, response); //void
300});
301
302
303client.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 */
315client.testI32(-1, function(err, response) {
316 assert(!err);
317 assert.equal(-1, response);
318});
319
320setTimeout(function() {
321 console.log("Server successfully tested!");
322 connection.end();
323}, 1500);
324
325// to make it also run on expresso
326exports.expressoTest = function() {};