blob: 617039b24132243991be157bbb12f2aa1eecb700 [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
bforbisda1169d2018-10-28 11:27:38 -040022const assert = require("assert");
23const thrift = require("thrift");
24const helpers = require("./helpers");
Roger Meier8909cbd2014-01-26 11:44:27 +010025
bforbisda1169d2018-10-28 11:27:38 -040026const ThriftTest = require(`./${helpers.genPath}/ThriftTest`);
27const ThriftTestDriver = require("./test_driver").ThriftTestDriver;
Cameron Martincaef0ed2025-01-15 11:58:39 +010028const ThriftTestDriverPromise =
29 require("./test_driver").ThriftTestDriverPromise;
bforbisda1169d2018-10-28 11:27:38 -040030const SecondService = require(`./${helpers.genPath}/SecondService`);
31
Cameron Martinfdaca5e2025-01-07 15:25:15 +000032const { program } = require("commander");
Roger Meier8909cbd2014-01-26 11:44:27 +010033
34program
bforbisda1169d2018-10-28 11:27:38 -040035 .option(
36 "-p, --protocol <protocol>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010037 "Set thrift protocol (binary|compact|json) [protocol]",
bforbisda1169d2018-10-28 11:27:38 -040038 )
39 .option(
40 "-t, --transport <transport>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010041 "Set thrift transport (buffered|framed|http) [transport]",
bforbisda1169d2018-10-28 11:27:38 -040042 )
43 .option("--port <port>", "Set thrift server port number to connect", 9090)
44 .option("--host <host>", "Set thrift server host to connect", "localhost")
45 .option(
46 "--domain-socket <path>",
Cameron Martincaef0ed2025-01-15 11:58:39 +010047 "Set thrift server unix domain socket to connect",
bforbisda1169d2018-10-28 11:27:38 -040048 )
49 .option("--ssl", "use SSL transport")
50 .option("--callback", "test with callback style functions")
51 .option(
Cameron Martinfdaca5e2025-01-07 15:25:15 +000052 "--type <type>",
bforbisda1169d2018-10-28 11:27:38 -040053 "Select server type (http|multiplex|tcp|websocket)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010054 "tcp",
bforbisda1169d2018-10-28 11:27:38 -040055 )
56 .option("--es6", "Use es6 code")
57 .option("--es5", "Use es5 code")
Roger Meier8909cbd2014-01-26 11:44:27 +010058 .parse(process.argv);
59
Cameron Martinfdaca5e2025-01-07 15:25:15 +000060const opts = program.opts();
61const host = opts.host;
62const port = opts.port;
63const domainSocket = opts.domainSocket;
64const ssl = opts.ssl;
65let type = opts.type;
henrique31236232014-02-23 20:16:44 +010066
James E. King, III375bfee2017-10-26 00:09:34 -040067/* for compatibility with cross test invocation for http transport testing */
Cameron Martinfdaca5e2025-01-07 15:25:15 +000068if (opts.transport === "http") {
69 opts.transport = "buffered";
bforbisda1169d2018-10-28 11:27:38 -040070 type = "http";
James E. King, III375bfee2017-10-26 00:09:34 -040071}
72
Cameron Martinfdaca5e2025-01-07 15:25:15 +000073if (opts.transport === "websocket") {
74 opts.transport = "buffered";
penenin1ab096c2020-05-18 12:27:31 -070075 type = "websocket";
76}
77
bforbisda1169d2018-10-28 11:27:38 -040078const options = {
Cameron Martinfdaca5e2025-01-07 15:25:15 +000079 transport: helpers.transports[opts.transport],
80 protocol: helpers.protocols[opts.protocol],
Roger Meier57b354b2014-02-22 01:01:58 +010081};
82
bforbisda1169d2018-10-28 11:27:38 -040083if (type === "http" || type === "websocket") {
84 options.path = "/test";
Roger Meier57b354b2014-02-22 01:01:58 +010085}
Roger Meier8909cbd2014-01-26 11:44:27 +010086
bforbisda1169d2018-10-28 11:27:38 -040087if (type === "http") {
88 options.headers = { Connection: "close" };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080089}
90
91if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -040092 if (type === "tcp" || type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080093 options.rejectUnauthorized = false;
bforbisda1169d2018-10-28 11:27:38 -040094 } else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080095 options.nodeOptions = { rejectUnauthorized: false };
96 options.https = true;
bforbisda1169d2018-10-28 11:27:38 -040097 } else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080098 options.wsOptions = { rejectUnauthorized: false };
99 options.secure = true;
100 }
101}
102
bforbisda1169d2018-10-28 11:27:38 -0400103let connection;
104let client;
Cameron Martinfdaca5e2025-01-07 15:25:15 +0000105const testDriver = opts.callback ? ThriftTestDriver : ThriftTestDriverPromise;
106if (helpers.ecmaMode === "es6" && opts.callback) {
bforbisda1169d2018-10-28 11:27:38 -0400107 console.log("ES6 does not support callback style");
108 process.exit(0);
109}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800110
bforbisda1169d2018-10-28 11:27:38 -0400111if (type === "tcp" || type === "multiplex") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800112 if (domainSocket) {
113 connection = thrift.createUDSConnection(domainSocket, options);
114 } else {
bforbisda1169d2018-10-28 11:27:38 -0400115 connection = ssl
116 ? thrift.createSSLConnection(host, port, options)
117 : thrift.createConnection(host, port, options);
Daniel Shihe41de0f2018-03-21 08:28:38 +0800118 }
bforbisda1169d2018-10-28 11:27:38 -0400119} else if (type === "http") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800120 if (domainSocket) {
121 connection = thrift.createHttpUDSConnection(domainSocket, options);
122 } else {
123 connection = thrift.createHttpConnection(host, port, options);
124 }
bforbisda1169d2018-10-28 11:27:38 -0400125} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800126 connection = thrift.createWSConnection(host, port, options);
127 connection.open();
128}
Roger Meier8909cbd2014-01-26 11:44:27 +0100129
Cameron Martincaef0ed2025-01-15 11:58:39 +0100130connection.on("error", function (err) {
bforbisda1169d2018-10-28 11:27:38 -0400131 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +0100132});
133
bforbisda1169d2018-10-28 11:27:38 -0400134if (type === "tcp") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800135 client = thrift.createClient(ThriftTest, connection);
136 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400137} else if (type === "multiplex") {
138 const mp = new thrift.Multiplexer();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800139 client = mp.createClient("ThriftTest", ThriftTest, connection);
bforbisda1169d2018-10-28 11:27:38 -0400140 const secondclient = mp.createClient(
141 "SecondService",
142 SecondService,
Cameron Martincaef0ed2025-01-15 11:58:39 +0100143 connection,
bforbisda1169d2018-10-28 11:27:38 -0400144 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800145
Cameron Martincaef0ed2025-01-15 11:58:39 +0100146 connection.on("connect", function () {
147 secondclient.secondtestString("Test", function (err, response) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800148 assert(!err);
bforbisda1169d2018-10-28 11:27:38 -0400149 assert.equal('testString("Test")', response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800150 });
151
152 runTests();
153 });
bforbisda1169d2018-10-28 11:27:38 -0400154} else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800155 client = thrift.createHttpClient(ThriftTest, connection);
156 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400157} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800158 client = thrift.createWSClient(ThriftTest, connection);
159 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800160}
Roger Meier8909cbd2014-01-26 11:44:27 +0100161
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800162function runTests() {
Cameron Martincaef0ed2025-01-15 11:58:39 +0100163 testDriver(client, function (status) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800164 console.log(status);
bforbisda1169d2018-10-28 11:27:38 -0400165 if (type !== "http" && type !== "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800166 connection.end();
167 }
bforbisda1169d2018-10-28 11:27:38 -0400168 if (type !== "multiplex") {
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700169 process.exit(0);
170 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800171 });
172}
173
Cameron Martincaef0ed2025-01-15 11:58:39 +0100174exports.expressoTest = function () {};