blob: 8e19b88e0d4f038caab87735bff2c25ccc39dd85 [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');
Henrique4a35d4c2013-05-11 12:08:35 +020020//var 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
Henrique4a35d4c2013-05-11 12:08:35 +020026//var connection = thrift.createConnection('localhost', 9090, { 'transport': ttransport.TBufferedTransport }),
Roger Meiereaa61d82012-01-12 21:38:29 +000027var connection = thrift.createConnection('localhost', 9090),
28 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
Henrique4a35d4c2013-05-11 12:08:35 +020034 // deepEqual doesn't work for binary64
35function 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);
50 assert.equal(undefined, response);
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});
86
87client.testI32(-1, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020088 assert( ! err);
89 assert.equal(-1, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000090});
91
92client.testI64(5, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020093 assert( ! err);
94 assert.equal(5, response);
Roger Meiereaa61d82012-01-12 21:38:29 +000095});
96
Roger Meiereaa61d82012-01-12 21:38:29 +000097client.testI64(-5, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +020098 assert( ! err);
99 assert.equal(-5, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000100});
Roger Meiereaa61d82012-01-12 21:38:29 +0000101
102client.testI64(-34359738368, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200103 assert( ! err);
104 assert.equal(-34359738368, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000105});
106
107client.testDouble(-5.2098523, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200108 assert( ! err);
109 assert.equal(-5.2098523, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000110});
111
Henrique4a35d4c2013-05-11 12:08:35 +0200112client.testDouble(7.012052175215044, function(err, response) {
113 assert( ! err);
114 assert.equal(7.012052175215044, response);
115});
116
117
Roger Meiereaa61d82012-01-12 21:38:29 +0000118var out = new ttypes.Xtruct({
Henrique99be0272013-05-10 23:43:12 +0200119 string_thing: 'Zero',
120 byte_thing: 1,
121 i32_thing: -3,
Henrique4a35d4c2013-05-11 12:08:35 +0200122 i64_thing: 1000000
Roger Meiereaa61d82012-01-12 21:38:29 +0000123});
124client.testStruct(out, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200125 assert( ! err);
Henrique4a35d4c2013-05-11 12:08:35 +0200126 checkRecursively(out, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000127});
128
129var out2 = new ttypes.Xtruct2();
130out2.byte_thing = 1;
131out2.struct_thing = out;
132out2.i32_thing = 5;
133client.testNest(out2, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200134 assert( ! err);
Henrique4a35d4c2013-05-11 12:08:35 +0200135 checkRecursively(out2, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000136});
137
Roger Meiereaa61d82012-01-12 21:38:29 +0000138var mapout = {};
139for (var i = 0; i < 5; ++i) {
140 mapout[i] = i-10;
141}
142client.testMap(mapout, function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200143 assert( ! err);
144 assert.deepEqual(mapout, response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000145});
Roger Meiereaa61d82012-01-12 21:38:29 +0000146
147/*
148 * TODO: testSet, testList, testEnum, testTypedef, testMapMap, testInsanity
149 */
150
151
152client.testException('ApplicationException', function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200153 //assert.equal('ApplicationException', err);
154 assert( ! response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000155});
156
157client.testException('Xception', function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200158 assert.equal('Xception', err.message);
159 assert( ! response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000160});
161
162client.testException('success', function(err, response) {
Henrique99be0272013-05-10 23:43:12 +0200163 assert( ! err);
164 //assert.equal('success', response);
Roger Meiereaa61d82012-01-12 21:38:29 +0000165});
166
Henrique99be0272013-05-10 23:43:12 +0200167
168setTimeout(function() {
169 console.log("Server successfully tested!");
170 connection.end();
Roger Meiereaa61d82012-01-12 21:38:29 +0000171}, 200);
Henrique99be0272013-05-10 23:43:12 +0200172
173// to make it also run on expresso
174exports.expressoTest = function() {};
175