blob: d96400efae719894e782ea8ee4fe83aa120d6353 [file] [log] [blame]
Roger Meiereaa61d82012-01-12 21:38:29 +00001/*
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');
Henrique Mendonça6afe0532013-06-09 22:54:51 +020020var ttransport = require('transport');
Henrique99be0272013-05-10 23:43:12 +020021var assert = require('assert');
Roger Meiereaa61d82012-01-12 21:38:29 +000022
23var ThriftTest = require('./gen-nodejs/ThriftTest'),
24 ttypes = require('./gen-nodejs/ThriftTest_types');
25
Henrique Mendonça6afe0532013-06-09 22:54:51 +020026var connection = thrift.createConnection('localhost', 9090, { 'transport': ttransport.TFramedTransport }),
27//var connection = thrift.createConnection('localhost', 9090),
Roger Meiereaa61d82012-01-12 21:38:29 +000028 client = thrift.createClient(ThriftTest, connection);
29
Roger Meiereaa61d82012-01-12 21:38:29 +000030connection.on('error', function(err) {
Henrique99be0272013-05-10 23:43:12 +020031 assert(false, err);
Roger Meiereaa61d82012-01-12 21:38:29 +000032});
33
Henrique08c34ea2013-05-11 19:29:19 +020034 // deepEqual doesn't work with fields using node-int64
Henrique4a35d4c2013-05-11 12:08:35 +020035function 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}
Henrique99be0272013-05-10 23:43:12 +020046
Roger Meiereaa61d82012-01-12 21:38:29 +000047
48client.testVoid(function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020049 assert( ! err);
Henrique08c34ea2013-05-11 19:29:19 +020050 assert.equal(undefined, response); //void
Roger Meiereaa61d82012-01-12 21:38:29 +000051});
52
Henrique4a35d4c2013-05-11 12:08:35 +020053
Roger Meiereaa61d82012-01-12 21:38:29 +000054client.testString("Test", function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020055 assert( ! err);
56 assert.equal("Test", response);
Roger Meiereaa61d82012-01-12 21:38:29 +000057});
58
Henrique4a35d4c2013-05-11 12:08:35 +020059client.testString("", function(err, response) {
60 assert( ! err);
61 assert.equal("", response);
62});
63
64// all Languages in UTF-8
65var 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ú, 粵語";
66client.testString(stringTest, function(err, response) {
67 assert( ! err);
68 assert.equal(stringTest, response);
69});
70
71var 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: !@#$%&()(&%$#{}{}<><><';
76client.testString(specialCharacters, function(err, response) {
77 assert( ! err);
78 assert.equal(specialCharacters, response);
79});
80
81
Roger Meiereaa61d82012-01-12 21:38:29 +000082client.testByte(1, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020083 assert( ! err);
84 assert.equal(1, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000085});
Henrique08c34ea2013-05-11 19:29:19 +020086client.testByte(0, function(err, response) {
87 assert( ! err);
88 assert.equal(0, response);
89});
90client.testByte(-1, function(err, response) {
91 assert( ! err);
92 assert.equal(-1, response);
93});
94client.testByte(-127, function(err, response) {
95 assert( ! err);
96 assert.equal(-127, response);
97});
Roger Meiereaa61d82012-01-12 21:38:29 +000098
99client.testI32(-1, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200100 assert( ! err);
101 assert.equal(-1, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000102});
103
104client.testI64(5, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200105 assert( ! err);
106 assert.equal(5, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000107});
Roger Meiereaa61d82012-01-12 21:38:29 +0000108client.testI64(-5, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200109 assert( ! err);
110 assert.equal(-5, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000111});
Roger Meiereaa61d82012-01-12 21:38:29 +0000112client.testI64(-34359738368, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200113 assert( ! err);
114 assert.equal(-34359738368, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000115});
116
117client.testDouble(-5.2098523, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200118 assert( ! err);
119 assert.equal(-5.2098523, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000120});
Henrique4a35d4c2013-05-11 12:08:35 +0200121client.testDouble(7.012052175215044, function(err, response) {
122 assert( ! err);
123 assert.equal(7.012052175215044, response);
124});
125
126
Roger Meiereaa61d82012-01-12 21:38:29 +0000127var out = new ttypes.Xtruct({
Henrique99be0272013-05-10 23:43:12 +0200128 string_thing: 'Zero',
129 byte_thing: 1,
130 i32_thing: -3,
Henrique4a35d4c2013-05-11 12:08:35 +0200131 i64_thing: 1000000
Roger Meiereaa61d82012-01-12 21:38:29 +0000132});
133client.testStruct(out, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200134 assert( ! err);
Henrique4a35d4c2013-05-11 12:08:35 +0200135 checkRecursively(out, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000136});
137
Henrique08c34ea2013-05-11 19:29:19 +0200138
Roger Meiereaa61d82012-01-12 21:38:29 +0000139var out2 = new ttypes.Xtruct2();
140out2.byte_thing = 1;
141out2.struct_thing = out;
142out2.i32_thing = 5;
143client.testNest(out2, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200144 assert( ! err);
Henrique4a35d4c2013-05-11 12:08:35 +0200145 checkRecursively(out2, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000146});
147
Henrique08c34ea2013-05-11 19:29:19 +0200148
Roger Meiereaa61d82012-01-12 21:38:29 +0000149var mapout = {};
150for (var i = 0; i < 5; ++i) {
151 mapout[i] = i-10;
152}
153client.testMap(mapout, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200154 assert( ! err);
155 assert.deepEqual(mapout, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000156});
Roger Meiereaa61d82012-01-12 21:38:29 +0000157
Roger Meiereaa61d82012-01-12 21:38:29 +0000158
Henrique08c34ea2013-05-11 19:29:19 +0200159var mapTestInput = {
160 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
161 "longValue":stringTest, stringTest:"long key"
162};
163client.testStringMap(mapTestInput, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200164 assert( ! err);
Henrique08c34ea2013-05-11 19:29:19 +0200165 assert.deepEqual(mapTestInput, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000166});
167
Henrique99be0272013-05-10 23:43:12 +0200168
Henrique08c34ea2013-05-11 19:29:19 +0200169var setTestInput = [1,2,3];
170client.testSet(setTestInput, function(err, response) {
171 assert( ! err);
172 assert.deepEqual(setTestInput, response);
173});
174client.testList(setTestInput, function(err, response) {
175 assert( ! err);
176 assert.deepEqual(setTestInput, response);
177});
178
179client.testEnum(ttypes.Numberz.ONE, function(err, response) {
180 assert( ! err);
181 assert.equal(ttypes.Numberz.ONE, response);
182});
183
184client.testTypedef(69, function(err, response) {
185 assert( ! err);
186 assert.equal(69, response);
187});
188
189
190var mapMapTest = {
191 "4": {"1":1, "2":2, "3":3, "4":4},
192 "-4": {"-4":-4, "-3":-3, "-2":-2, "-1":-1}
193};
194client.testMapMap(mapMapTest, function(err, response) {
195 assert( ! err);
196 assert.deepEqual(mapMapTest, response);
197});
198
199var 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});
213var insanity = {
214 "1":{ "2": crazy, "3": crazy },
215 "2":{ "6":{ "userMap":null, "xtructs":null } }
216};
217client.testInsanity(crazy, function(err, response) {
218 assert( ! err);
219 checkRecursively(insanity, response);
220});
221
222
223client.testException('TException', function(err, response) {
224 //assert(err); //BUG?
225 assert( ! response);
226});
227client.testException('Xception', function(err, response) {
228 assert( ! response);
229 assert.equal(err.errorCode, 1001);
230 assert.equal('Xception', err.message);
231});
232client.testException('no Exception', function(err, response) {
233 assert( ! err);
234 assert.equal(undefined, response); //void
235});
236
237
238client.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 */
250client.testI32(-1, function(err, response) {
251 assert( ! err);
252 assert.equal(-1, response);
253});
254
Henrique99be0272013-05-10 23:43:12 +0200255setTimeout(function() {
256 console.log("Server successfully tested!");
257 connection.end();
Henrique Mendonça6afe0532013-06-09 22:54:51 +0200258}, 1500);
Henrique99be0272013-05-10 23:43:12 +0200259
260// to make it also run on expresso
261exports.expressoTest = function() {};
262