blob: 90a532d6ff87dfa16b089e7323e06853da37a87c [file] [log] [blame]
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +03001/*
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 */
Cameron Martincaef0ed2025-01-15 11:58:39 +010019/* jshint -W100 */
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030020
21import { ThriftTest } from "./gen-js/ThriftTest_types";
22import "./gen-js/ThriftTest";
23var Int64 = require("node-int64");
24var phantom = require("phantom");
25
Cameron Martincaef0ed2025-01-15 11:58:39 +010026const int64_2_pow_60: typeof Int64 = new Int64("1000000000000000");
27const int64_minus_2_pow_60: typeof Int64 = new Int64("f000000000000000");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030028
Cameron Martincaef0ed2025-01-15 11:58:39 +010029(function () {
30 "use strict";
31
32 // Rudimentary test helper functions
33 // TODO: Return error code based on kind of errors rather than throw
34 var ok = function (t, msg) {
35 if (!t) {
36 console.log("*** FAILED ***");
37 throw new Error(msg);
38 }
39 };
40 var equal = function (a, b) {
41 if (a !== b) {
42 console.log("*** FAILED ***");
43 throw new Error();
44 }
45 };
46 var test = function (name, f) {
47 console.log("TEST : " + name);
48 f();
49 console.log("OK\n");
50 };
51
52 var parseArgs = function (args) {
53 var skips = ["--transport=http", "--protocol=json"];
54 var opts = {
55 port: "9090",
56 // protocol: 'json',
57 };
58 var keys = {};
59 for (var key in opts) {
60 keys["--" + key + "="] = key;
61 }
62 for (var i in args) {
63 var arg = args[i];
64 if (skips.indexOf(arg) != -1) {
65 continue;
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030066 }
Cameron Martincaef0ed2025-01-15 11:58:39 +010067 var hit = false;
68 for (var k in keys) {
69 if (arg.slice(0, k.length) === k) {
70 opts[keys[k]] = arg.slice(k.length);
71 hit = true;
72 break;
73 }
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030074 }
Cameron Martincaef0ed2025-01-15 11:58:39 +010075 if (!hit) {
76 throw new Error("Unknown argument: " + arg);
77 }
78 }
79 var portAsInt: number = parseInt(opts.port, 10);
80 if (!opts.port || portAsInt < 1 || portAsInt > 65535) {
81 throw new Error("Invalid port number");
82 }
83 return opts;
84 };
85
86 var execute = function () {
87 console.log("### Apache Thrift Javascript standalone test client");
88 console.log("------------------------------------------------------------");
89
90 phantom.page.injectJs("thrift.js");
91 phantom.page.injectJs("gen-js/ThriftTest_types.js");
92 phantom.page.injectJs("gen-js/ThriftTest.js");
93
94 var system = require("system");
95 var opts = parseArgs(system.args.slice(1));
96 var port = opts.port;
97 var transport = new Thrift.Transport(
98 "http://localhost:" + port + "/service",
99 );
100 var protocol = new Thrift.Protocol(transport);
101 var client = new ThriftTest.ThriftTestClient(protocol);
102
103 // TODO: Remove duplicate code with test.js.
104 // all Languages in UTF-8
105 var stringTest =
106 "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ú, 粵語";
107
108 function checkRecursively(map1, map2) {
109 if (typeof map1 !== "function" && typeof map2 !== "function") {
110 if (!map1 || typeof map1 !== "object") {
111 equal(map1, map2);
112 } else {
113 for (var key in map1) {
114 checkRecursively(map1[key], map2[key]);
115 }
116 }
117 }
118 }
119
120 test("Void", function () {
121 equal(client.testVoid(), undefined);
122 });
123 test("Binary (String)", function () {
124 var binary: string = "";
125 for (var v = 255; v >= 0; --v) {
126 binary += String.fromCharCode(v);
127 }
128 equal(client.testBinary(binary), binary);
129 });
130 test("Binary (Uint8Array)", function () {
131 var binary: string = "";
132 for (var v = 255; v >= 0; --v) {
133 binary += String.fromCharCode(v);
134 }
135 var arr = new Uint8Array(binary.length);
136 for (var i = 0; i < binary.length; ++i) {
137 arr[i] = binary[i].charCodeAt(0);
138 }
139 const hexEncodedString = Array.from(arr, function (byte) {
140 return String.fromCharCode(byte);
141 }).join("");
142 equal(client.testBinary(hexEncodedString), binary);
143 });
144 test("String", function () {
145 equal(client.testString(""), "");
146 equal(client.testString(stringTest), stringTest);
147
148 var specialCharacters =
149 'quote: \" backslash:' +
150 " forwardslash-escaped: \/ " +
151 " backspace: \b formfeed: \f newline: \n return: \r tab: " +
152 ' now-all-of-them-together: "\\\/\b\n\r\t' +
153 " now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><";
154 equal(client.testString(specialCharacters), specialCharacters);
155 });
156 test("Double", function () {
157 equal(client.testDouble(0), 0);
158 equal(client.testDouble(-1), -1);
159 equal(client.testDouble(3.14), 3.14);
160 equal(client.testDouble(Math.pow(2, 60)), Math.pow(2, 60));
161 });
162 test("Bool", function () {
163 equal(client.testBool(true), true);
164 equal(client.testBool(false), false);
165 });
166 test("I8", function () {
167 equal(client.testByte(0), 0);
168 equal(client.testByte(0x01), 0x01);
169 });
170 test("I32", function () {
171 equal(client.testI32(0), 0);
172 equal(client.testI32(Math.pow(2, 30)), Math.pow(2, 30));
173 equal(client.testI32(-Math.pow(2, 30)), -Math.pow(2, 30));
174 });
175 test("I64", function () {
176 equal(client.testI64(new Int64(0)), 0);
177 equal(client.testI64(int64_2_pow_60), Math.pow(2, 52));
178 equal(client.testI64(int64_minus_2_pow_60), -Math.pow(2, 52));
179 });
180
181 test("Struct", function () {
182 var structTestInput: ThriftTest.Xtruct = new ThriftTest.Xtruct();
183 structTestInput.string_thing = "worked";
184 structTestInput.byte_thing = 0x01;
185 structTestInput.i32_thing = Math.pow(2, 30);
186 structTestInput.i64_thing = int64_2_pow_60;
187
188 var structTestOutput: ThriftTest.Xtruct =
189 client.testStruct(structTestInput);
190
191 equal(structTestOutput.string_thing, structTestInput.string_thing);
192 equal(structTestOutput.byte_thing, structTestInput.byte_thing);
193 equal(structTestOutput.i32_thing, structTestInput.i32_thing);
194 equal(structTestOutput.i64_thing, structTestInput.i64_thing);
195
196 equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
197 });
198
199 test("Nest", function () {
200 var xtrTestInput: ThriftTest.Xtruct = new ThriftTest.Xtruct();
201 xtrTestInput.string_thing = "worked";
202 xtrTestInput.byte_thing = 0x01;
203 xtrTestInput.i32_thing = Math.pow(2, 30);
204 xtrTestInput.i64_thing = int64_2_pow_60;
205
206 var nestTestInput: ThriftTest.Xtruct2 = new ThriftTest.Xtruct2();
207 nestTestInput.byte_thing = 0x02;
208 nestTestInput.struct_thing = xtrTestInput;
209 nestTestInput.i32_thing = Math.pow(2, 15);
210
211 var nestTestOutput = client.testNest(nestTestInput);
212
213 equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
214 equal(
215 nestTestOutput.struct_thing.string_thing,
216 nestTestInput.struct_thing.string_thing,
217 );
218 equal(
219 nestTestOutput.struct_thing.byte_thing,
220 nestTestInput.struct_thing.byte_thing,
221 );
222 equal(
223 nestTestOutput.struct_thing.i32_thing,
224 nestTestInput.struct_thing.i32_thing,
225 );
226 equal(
227 nestTestOutput.struct_thing.i64_thing,
228 nestTestInput.struct_thing.i64_thing,
229 );
230 equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
231
232 equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
233 });
234
235 test("Map", function () {
236 var mapTestInput: { [k: number]: number } = { 7: 77, 8: 88, 9: 99 };
237
238 var mapTestOutput: { [k: number]: number } = client.testMap(mapTestInput);
239
240 for (var key in mapTestOutput) {
241 equal(mapTestOutput[key], mapTestInput[key]);
242 }
243 });
244
245 test("StringMap", function () {
246 var mapTestInput: { [k: string]: string } = {
247 a: "123",
248 "a b": "with spaces ",
249 same: "same",
250 "0": "numeric key",
251 longValue: stringTest,
252 stringTest: "long key",
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300253 };
Cameron Martincaef0ed2025-01-15 11:58:39 +0100254
255 var mapTestOutput: { [k: string]: string } =
256 client.testStringMap(mapTestInput);
257
258 for (var key in mapTestOutput) {
259 equal(mapTestOutput[key], mapTestInput[key]);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300260 }
Cameron Martincaef0ed2025-01-15 11:58:39 +0100261 });
262
263 test("Set", function () {
264 var setTestInput: number[] = [1, 2, 3];
265 ok(client.testSet(setTestInput), setTestInput);
266 });
267
268 test("List", function () {
269 var listTestInput: number[] = [1, 2, 3];
270 ok(client.testList(listTestInput), listTestInput);
271 });
272
273 test("Enum", function () {
274 equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
275 });
276
277 test("TypeDef", function () {
278 equal(client.testTypedef(new Int64(69)), 69);
279 });
280
281 test("MapMap", function () {
282 var mapMapTestExpectedResult: { [K: number]: { [k: number]: number } } = {
283 "4": { "1": 1, "2": 2, "3": 3, "4": 4 },
284 "-4": { "-4": -4, "-3": -3, "-2": -2, "-1": -1 },
285 };
286
287 var mapMapTestOutput = client.testMapMap(1);
288
289 for (var key in mapMapTestOutput) {
290 for (var key2 in mapMapTestOutput[key]) {
291 equal(
292 mapMapTestOutput[key][key2],
293 mapMapTestExpectedResult[key][key2],
294 );
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300295 }
296 }
Cameron Martincaef0ed2025-01-15 11:58:39 +0100297
298 checkRecursively(mapMapTestOutput, mapMapTestExpectedResult);
299 });
300
301 test("Xception", function () {
302 try {
303 client.testException("Xception");
304 ok(false, "expected an exception but there was no exception");
305 } catch (e) {
306 equal(e.errorCode, 1001);
307 equal(e.message, "Xception");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300308 }
Cameron Martincaef0ed2025-01-15 11:58:39 +0100309 });
310
311 test("no Exception", function () {
312 try {
313 client.testException("no Exception");
314 } catch (e) {
315 ok(false, "expected no exception but here was an exception");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300316 }
Cameron Martincaef0ed2025-01-15 11:58:39 +0100317 });
318
319 test("TException", function () {
320 try {
321 client.testException("TException");
322 ok(false, "expected an exception but there was no exception");
323 } catch (e) {
324 ok(ok, "succesfully got exception");
325 }
326 });
327
328 const crazy: ThriftTest.Insanity = {
329 userMap: { "5": new Int64(5), "8": new Int64(8) },
330 xtructs: [
331 {
332 string_thing: "Goodbye4",
333 byte_thing: 4,
334 i32_thing: 4,
335 i64_thing: new Int64(4),
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300336 },
337 {
Cameron Martincaef0ed2025-01-15 11:58:39 +0100338 string_thing: "Hello2",
339 byte_thing: 2,
340 i32_thing: 2,
341 i64_thing: new Int64(2),
342 },
343 ],
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300344 };
Cameron Martincaef0ed2025-01-15 11:58:39 +0100345 test("Insanity", function () {
346 const insanity: {
347 [k: number]: ThriftTest.Insanity | { [k: number]: ThriftTest.Insanity };
348 } = {
349 "1": {
350 "2": crazy,
351 "3": crazy,
352 },
353 "2": { "6": new ThriftTest.Insanity() },
354 };
355 var res = client.testInsanity(new ThriftTest.Insanity(crazy));
356 ok(res, JSON.stringify(res));
357 ok(insanity, JSON.stringify(insanity));
358
359 checkRecursively(res, insanity);
360 });
361
362 console.log("------------------------------------------------------------");
363 console.log("### All tests succeeded.");
364 return 0;
365 };
366
367 try {
368 var ret = execute();
369 phantom.exit(ret);
370 } catch (err) {
371 // Catch all and exit to avoid hang.
372 console.error(err);
373 phantom.exit(1);
374 }
375})();