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 | 4a35d4c | 2013-05-11 12:08:35 +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 | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame^] | 26 | //var connection = thrift.createConnection('localhost', 9090, { 'transport': ttransport.TBufferedTransport }), |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 27 | var connection = thrift.createConnection('localhost', 9090), |
| 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 | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame^] | 34 | // deepEqual doesn't work for binary64 |
| 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); |
| 50 | assert.equal(undefined, response); |
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 | }); |
| 86 | |
| 87 | client.testI32(-1, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 88 | assert( ! err); |
| 89 | assert.equal(-1, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 90 | }); |
| 91 | |
| 92 | client.testI64(5, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 93 | assert( ! err); |
| 94 | assert.equal(5, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 95 | }); |
| 96 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 97 | client.testI64(-5, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 98 | assert( ! err); |
| 99 | assert.equal(-5, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 100 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 101 | |
| 102 | client.testI64(-34359738368, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 103 | assert( ! err); |
| 104 | assert.equal(-34359738368, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 105 | }); |
| 106 | |
| 107 | client.testDouble(-5.2098523, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 108 | assert( ! err); |
| 109 | assert.equal(-5.2098523, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 110 | }); |
| 111 | |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame^] | 112 | client.testDouble(7.012052175215044, function(err, response) { |
| 113 | assert( ! err); |
| 114 | assert.equal(7.012052175215044, response); |
| 115 | }); |
| 116 | |
| 117 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 118 | var out = new ttypes.Xtruct({ |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 119 | string_thing: 'Zero', |
| 120 | byte_thing: 1, |
| 121 | i32_thing: -3, |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame^] | 122 | i64_thing: 1000000 |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 123 | }); |
| 124 | client.testStruct(out, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 125 | assert( ! err); |
Henrique | 4a35d4c | 2013-05-11 12:08:35 +0200 | [diff] [blame^] | 126 | checkRecursively(out, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 127 | }); |
| 128 | |
| 129 | var out2 = new ttypes.Xtruct2(); |
| 130 | out2.byte_thing = 1; |
| 131 | out2.struct_thing = out; |
| 132 | out2.i32_thing = 5; |
| 133 | client.testNest(out2, 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(out2, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 136 | }); |
| 137 | |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 138 | var mapout = {}; |
| 139 | for (var i = 0; i < 5; ++i) { |
| 140 | mapout[i] = i-10; |
| 141 | } |
| 142 | client.testMap(mapout, function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 143 | assert( ! err); |
| 144 | assert.deepEqual(mapout, response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 145 | }); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * TODO: testSet, testList, testEnum, testTypedef, testMapMap, testInsanity |
| 149 | */ |
| 150 | |
| 151 | |
| 152 | client.testException('ApplicationException', function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 153 | //assert.equal('ApplicationException', err); |
| 154 | assert( ! response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 155 | }); |
| 156 | |
| 157 | client.testException('Xception', function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 158 | assert.equal('Xception', err.message); |
| 159 | assert( ! response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 160 | }); |
| 161 | |
| 162 | client.testException('success', function(err, response) { |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 163 | assert( ! err); |
| 164 | //assert.equal('success', response); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 165 | }); |
| 166 | |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 167 | |
| 168 | setTimeout(function() { |
| 169 | console.log("Server successfully tested!"); |
| 170 | connection.end(); |
Roger Meier | eaa61d8 | 2012-01-12 21:38:29 +0000 | [diff] [blame] | 171 | }, 200); |
Henrique | 99be027 | 2013-05-10 23:43:12 +0200 | [diff] [blame] | 172 | |
| 173 | // to make it also run on expresso |
| 174 | exports.expressoTest = function() {}; |
| 175 | |