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