blob: 4c3ec4321d282e8673ecee1d846e01d6198ba722 [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
CJCombrinkdfeab8d2026-03-06 07:03:56 +010022import fs from "fs";
23import path from "path";
Cameron Martin21ed4a22024-04-22 11:08:19 +010024import assert from "assert";
25import thrift from "thrift";
26import helpers from "./helpers.js";
Roger Meier8909cbd2014-01-26 11:44:27 +010027
Cameron Martin21ed4a22024-04-22 11:08:19 +010028const ThriftTest = await import(
29 `./${helpers.genPath}/ThriftTest.${helpers.moduleExt}`
30);
31import { ThriftTestDriver, ThriftTestDriverPromise } from "./test_driver.mjs";
32const SecondService = await import(
33 `./${helpers.genPath}/SecondService.${helpers.moduleExt}`
34);
bforbisda1169d2018-10-28 11:27:38 -040035
Cameron Martin21ed4a22024-04-22 11:08:19 +010036import { program } from "commander";
Roger Meier8909cbd2014-01-26 11:44:27 +010037
38program
bforbisda1169d2018-10-28 11:27:38 -040039 .option(
40 "-p, --protocol <protocol>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010041 "Set thrift protocol (binary|compact|json) [protocol]",
bforbisda1169d2018-10-28 11:27:38 -040042 )
43 .option(
44 "-t, --transport <transport>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010045 "Set thrift transport (buffered|framed|http) [transport]",
bforbisda1169d2018-10-28 11:27:38 -040046 )
47 .option("--port <port>", "Set thrift server port number to connect", 9090)
48 .option("--host <host>", "Set thrift server host to connect", "localhost")
49 .option(
50 "--domain-socket <path>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010051 "Set thrift server unix domain socket to connect",
bforbisda1169d2018-10-28 11:27:38 -040052 )
53 .option("--ssl", "use SSL transport")
54 .option("--callback", "test with callback style functions")
55 .option(
Cameron Martinfdaca5e2025-01-07 15:25:15 +000056 "--type <type>",
bforbisda1169d2018-10-28 11:27:38 -040057 "Select server type (http|multiplex|tcp|websocket)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010058 "tcp",
bforbisda1169d2018-10-28 11:27:38 -040059 )
60 .option("--es6", "Use es6 code")
61 .option("--es5", "Use es5 code")
Cameron Martin21ed4a22024-04-22 11:08:19 +010062 .option("--esm", "Use es modules")
Roger Meier8909cbd2014-01-26 11:44:27 +010063 .parse(process.argv);
64
Cameron Martinfdaca5e2025-01-07 15:25:15 +000065const opts = program.opts();
66const host = opts.host;
67const port = opts.port;
68const domainSocket = opts.domainSocket;
69const ssl = opts.ssl;
70let type = opts.type;
henrique31236232014-02-23 20:16:44 +010071
James E. King, III375bfee2017-10-26 00:09:34 -040072/* for compatibility with cross test invocation for http transport testing */
Cameron Martinfdaca5e2025-01-07 15:25:15 +000073if (opts.transport === "http") {
74 opts.transport = "buffered";
bforbisda1169d2018-10-28 11:27:38 -040075 type = "http";
James E. King, III375bfee2017-10-26 00:09:34 -040076}
77
Cameron Martinfdaca5e2025-01-07 15:25:15 +000078if (opts.transport === "websocket") {
79 opts.transport = "buffered";
penenin1ab096c2020-05-18 12:27:31 -070080 type = "websocket";
81}
82
bforbisda1169d2018-10-28 11:27:38 -040083const options = {
Cameron Martinfdaca5e2025-01-07 15:25:15 +000084 transport: helpers.transports[opts.transport],
85 protocol: helpers.protocols[opts.protocol],
Roger Meier57b354b2014-02-22 01:01:58 +010086};
87
bforbisda1169d2018-10-28 11:27:38 -040088if (type === "http" || type === "websocket") {
89 options.path = "/test";
Roger Meier57b354b2014-02-22 01:01:58 +010090}
Roger Meier8909cbd2014-01-26 11:44:27 +010091
bforbisda1169d2018-10-28 11:27:38 -040092if (type === "http") {
93 options.headers = { Connection: "close" };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080094}
95
96if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -040097 if (type === "tcp" || type === "multiplex") {
CJCombrinkdfeab8d2026-03-06 07:03:56 +010098 options.secureProtocol = "TLS_method";
99 options.secureOptions = 0;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800100 options.rejectUnauthorized = false;
CJCombrinkdfeab8d2026-03-06 07:03:56 +0100101 options.cert = fs.readFileSync(
102 path.resolve(import.meta.dirname, "../../../test/keys/client.crt"),
103 );
104 options.key = fs.readFileSync(
105 path.resolve(import.meta.dirname, "../../../test/keys/client.key"),
106 );
bforbisda1169d2018-10-28 11:27:38 -0400107 } else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800108 options.nodeOptions = { rejectUnauthorized: false };
109 options.https = true;
bforbisda1169d2018-10-28 11:27:38 -0400110 } else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800111 options.wsOptions = { rejectUnauthorized: false };
112 options.secure = true;
113 }
114}
115
bforbisda1169d2018-10-28 11:27:38 -0400116let connection;
117let client;
Cameron Martinfdaca5e2025-01-07 15:25:15 +0000118const testDriver = opts.callback ? ThriftTestDriver : ThriftTestDriverPromise;
119if (helpers.ecmaMode === "es6" && opts.callback) {
bforbisda1169d2018-10-28 11:27:38 -0400120 console.log("ES6 does not support callback style");
121 process.exit(0);
122}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800123
bforbisda1169d2018-10-28 11:27:38 -0400124if (type === "tcp" || type === "multiplex") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800125 if (domainSocket) {
126 connection = thrift.createUDSConnection(domainSocket, options);
127 } else {
bforbisda1169d2018-10-28 11:27:38 -0400128 connection = ssl
129 ? thrift.createSSLConnection(host, port, options)
130 : thrift.createConnection(host, port, options);
Daniel Shihe41de0f2018-03-21 08:28:38 +0800131 }
bforbisda1169d2018-10-28 11:27:38 -0400132} else if (type === "http") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800133 if (domainSocket) {
134 connection = thrift.createHttpUDSConnection(domainSocket, options);
135 } else {
136 connection = thrift.createHttpConnection(host, port, options);
137 }
bforbisda1169d2018-10-28 11:27:38 -0400138} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800139 connection = thrift.createWSConnection(host, port, options);
140 connection.open();
141}
Roger Meier8909cbd2014-01-26 11:44:27 +0100142
Cameron Martincaef0ed2025-01-15 11:58:39 +0100143connection.on("error", function (err) {
bforbisda1169d2018-10-28 11:27:38 -0400144 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +0100145});
146
bforbisda1169d2018-10-28 11:27:38 -0400147if (type === "tcp") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800148 client = thrift.createClient(ThriftTest, connection);
149 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400150} else if (type === "multiplex") {
151 const mp = new thrift.Multiplexer();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800152 client = mp.createClient("ThriftTest", ThriftTest, connection);
bforbisda1169d2018-10-28 11:27:38 -0400153 const secondclient = mp.createClient(
154 "SecondService",
155 SecondService,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100156 connection,
bforbisda1169d2018-10-28 11:27:38 -0400157 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800158
Cameron Martincaef0ed2025-01-15 11:58:39 +0100159 connection.on("connect", function () {
160 secondclient.secondtestString("Test", function (err, response) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800161 assert(!err);
bforbisda1169d2018-10-28 11:27:38 -0400162 assert.equal('testString("Test")', response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800163 });
164
165 runTests();
166 });
bforbisda1169d2018-10-28 11:27:38 -0400167} else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800168 client = thrift.createHttpClient(ThriftTest, connection);
169 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400170} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800171 client = thrift.createWSClient(ThriftTest, connection);
172 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800173}
Roger Meier8909cbd2014-01-26 11:44:27 +0100174
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800175function runTests() {
Cameron Martincaef0ed2025-01-15 11:58:39 +0100176 testDriver(client, function (status) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800177 console.log(status);
bforbisda1169d2018-10-28 11:27:38 -0400178 if (type !== "http" && type !== "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800179 connection.end();
180 }
bforbisda1169d2018-10-28 11:27:38 -0400181 if (type !== "multiplex") {
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700182 process.exit(0);
183 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800184 });
185}
186
Cameron Martin21ed4a22024-04-22 11:08:19 +0100187export const expressoTest = function () {};