blob: 6200dc6c4288fa0829070bbfd1567d491a588725 [file] [log] [blame]
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -08001#!/usr/bin/env node
Roger Meier32f39822014-06-18 22:43:17 +02002
Roger Meier8909cbd2014-01-26 11:44:27 +01003/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
Cameron Martin21ed4a22024-04-22 11:08:19 +010022import assert from "assert";
23import thrift from "thrift";
24import helpers from "./helpers.js";
Roger Meier8909cbd2014-01-26 11:44:27 +010025
Cameron Martin21ed4a22024-04-22 11:08:19 +010026const ThriftTest = await import(
27 `./${helpers.genPath}/ThriftTest.${helpers.moduleExt}`
28);
29import { ThriftTestDriver, ThriftTestDriverPromise } from "./test_driver.mjs";
30const SecondService = await import(
31 `./${helpers.genPath}/SecondService.${helpers.moduleExt}`
32);
bforbisda1169d2018-10-28 11:27:38 -040033
Cameron Martin21ed4a22024-04-22 11:08:19 +010034import { program } from "commander";
Roger Meier8909cbd2014-01-26 11:44:27 +010035
36program
bforbisda1169d2018-10-28 11:27:38 -040037 .option(
38 "-p, --protocol <protocol>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010039 "Set thrift protocol (binary|compact|json) [protocol]",
bforbisda1169d2018-10-28 11:27:38 -040040 )
41 .option(
42 "-t, --transport <transport>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010043 "Set thrift transport (buffered|framed|http) [transport]",
bforbisda1169d2018-10-28 11:27:38 -040044 )
45 .option("--port <port>", "Set thrift server port number to connect", 9090)
46 .option("--host <host>", "Set thrift server host to connect", "localhost")
47 .option(
48 "--domain-socket <path>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010049 "Set thrift server unix domain socket to connect",
bforbisda1169d2018-10-28 11:27:38 -040050 )
51 .option("--ssl", "use SSL transport")
52 .option("--callback", "test with callback style functions")
53 .option(
Cameron Martinfdaca5e2025-01-07 15:25:15 +000054 "--type <type>",
bforbisda1169d2018-10-28 11:27:38 -040055 "Select server type (http|multiplex|tcp|websocket)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010056 "tcp",
bforbisda1169d2018-10-28 11:27:38 -040057 )
58 .option("--es6", "Use es6 code")
59 .option("--es5", "Use es5 code")
Cameron Martin21ed4a22024-04-22 11:08:19 +010060 .option("--esm", "Use es modules")
Roger Meier8909cbd2014-01-26 11:44:27 +010061 .parse(process.argv);
62
Cameron Martinfdaca5e2025-01-07 15:25:15 +000063const opts = program.opts();
64const host = opts.host;
65const port = opts.port;
66const domainSocket = opts.domainSocket;
67const ssl = opts.ssl;
68let type = opts.type;
henrique31236232014-02-23 20:16:44 +010069
James E. King, III375bfee2017-10-26 00:09:34 -040070/* for compatibility with cross test invocation for http transport testing */
Cameron Martinfdaca5e2025-01-07 15:25:15 +000071if (opts.transport === "http") {
72 opts.transport = "buffered";
bforbisda1169d2018-10-28 11:27:38 -040073 type = "http";
James E. King, III375bfee2017-10-26 00:09:34 -040074}
75
Cameron Martinfdaca5e2025-01-07 15:25:15 +000076if (opts.transport === "websocket") {
77 opts.transport = "buffered";
penenin1ab096c2020-05-18 12:27:31 -070078 type = "websocket";
79}
80
bforbisda1169d2018-10-28 11:27:38 -040081const options = {
Cameron Martinfdaca5e2025-01-07 15:25:15 +000082 transport: helpers.transports[opts.transport],
83 protocol: helpers.protocols[opts.protocol],
Roger Meier57b354b2014-02-22 01:01:58 +010084};
85
bforbisda1169d2018-10-28 11:27:38 -040086if (type === "http" || type === "websocket") {
87 options.path = "/test";
Roger Meier57b354b2014-02-22 01:01:58 +010088}
Roger Meier8909cbd2014-01-26 11:44:27 +010089
bforbisda1169d2018-10-28 11:27:38 -040090if (type === "http") {
91 options.headers = { Connection: "close" };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080092}
93
94if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -040095 if (type === "tcp" || type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080096 options.rejectUnauthorized = false;
bforbisda1169d2018-10-28 11:27:38 -040097 } else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080098 options.nodeOptions = { rejectUnauthorized: false };
99 options.https = true;
bforbisda1169d2018-10-28 11:27:38 -0400100 } else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800101 options.wsOptions = { rejectUnauthorized: false };
102 options.secure = true;
103 }
104}
105
bforbisda1169d2018-10-28 11:27:38 -0400106let connection;
107let client;
Cameron Martinfdaca5e2025-01-07 15:25:15 +0000108const testDriver = opts.callback ? ThriftTestDriver : ThriftTestDriverPromise;
109if (helpers.ecmaMode === "es6" && opts.callback) {
bforbisda1169d2018-10-28 11:27:38 -0400110 console.log("ES6 does not support callback style");
111 process.exit(0);
112}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800113
bforbisda1169d2018-10-28 11:27:38 -0400114if (type === "tcp" || type === "multiplex") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800115 if (domainSocket) {
116 connection = thrift.createUDSConnection(domainSocket, options);
117 } else {
bforbisda1169d2018-10-28 11:27:38 -0400118 connection = ssl
119 ? thrift.createSSLConnection(host, port, options)
120 : thrift.createConnection(host, port, options);
Daniel Shihe41de0f2018-03-21 08:28:38 +0800121 }
bforbisda1169d2018-10-28 11:27:38 -0400122} else if (type === "http") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800123 if (domainSocket) {
124 connection = thrift.createHttpUDSConnection(domainSocket, options);
125 } else {
126 connection = thrift.createHttpConnection(host, port, options);
127 }
bforbisda1169d2018-10-28 11:27:38 -0400128} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800129 connection = thrift.createWSConnection(host, port, options);
130 connection.open();
131}
Roger Meier8909cbd2014-01-26 11:44:27 +0100132
Cameron Martincaef0ed2025-01-15 11:58:39 +0100133connection.on("error", function (err) {
bforbisda1169d2018-10-28 11:27:38 -0400134 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +0100135});
136
bforbisda1169d2018-10-28 11:27:38 -0400137if (type === "tcp") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800138 client = thrift.createClient(ThriftTest, connection);
139 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400140} else if (type === "multiplex") {
141 const mp = new thrift.Multiplexer();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800142 client = mp.createClient("ThriftTest", ThriftTest, connection);
bforbisda1169d2018-10-28 11:27:38 -0400143 const secondclient = mp.createClient(
144 "SecondService",
145 SecondService,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100146 connection,
bforbisda1169d2018-10-28 11:27:38 -0400147 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800148
Cameron Martincaef0ed2025-01-15 11:58:39 +0100149 connection.on("connect", function () {
150 secondclient.secondtestString("Test", function (err, response) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800151 assert(!err);
bforbisda1169d2018-10-28 11:27:38 -0400152 assert.equal('testString("Test")', response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800153 });
154
155 runTests();
156 });
bforbisda1169d2018-10-28 11:27:38 -0400157} else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800158 client = thrift.createHttpClient(ThriftTest, connection);
159 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400160} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800161 client = thrift.createWSClient(ThriftTest, connection);
162 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800163}
Roger Meier8909cbd2014-01-26 11:44:27 +0100164
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800165function runTests() {
Cameron Martincaef0ed2025-01-15 11:58:39 +0100166 testDriver(client, function (status) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800167 console.log(status);
bforbisda1169d2018-10-28 11:27:38 -0400168 if (type !== "http" && type !== "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800169 connection.end();
170 }
bforbisda1169d2018-10-28 11:27:38 -0400171 if (type !== "multiplex") {
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700172 process.exit(0);
173 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800174 });
175}
176
Cameron Martin21ed4a22024-04-22 11:08:19 +0100177export const expressoTest = function () {};