blob: 31ea06e2f0d8d28eb313c098572edd29e229c238 [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;
28const ThriftTestDriverPromise = require("./test_driver")
29 .ThriftTestDriverPromise;
30const SecondService = require(`./${helpers.genPath}/SecondService`);
31
32const program = require("commander");
Roger Meier8909cbd2014-01-26 11:44:27 +010033
34program
bforbisda1169d2018-10-28 11:27:38 -040035 .option(
36 "-p, --protocol <protocol>",
37 "Set thrift protocol (binary|compact|json) [protocol]"
38 )
39 .option(
40 "-t, --transport <transport>",
41 "Set thrift transport (buffered|framed|http) [transport]"
42 )
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>",
47 "Set thrift server unix domain socket to connect"
48 )
49 .option("--ssl", "use SSL transport")
50 .option("--callback", "test with callback style functions")
51 .option(
52 "-t, --type <type>",
53 "Select server type (http|multiplex|tcp|websocket)",
54 "tcp"
55 )
56 .option("--es6", "Use es6 code")
57 .option("--es5", "Use es5 code")
Roger Meier8909cbd2014-01-26 11:44:27 +010058 .parse(process.argv);
59
bforbisda1169d2018-10-28 11:27:38 -040060const host = program.host;
61const port = program.port;
62const domainSocket = program.domainSocket;
63const ssl = program.ssl;
64let type = program.type;
henrique31236232014-02-23 20:16:44 +010065
James E. King, III375bfee2017-10-26 00:09:34 -040066/* for compatibility with cross test invocation for http transport testing */
bforbisda1169d2018-10-28 11:27:38 -040067if (program.transport === "http") {
68 program.transport = "buffered";
69 type = "http";
James E. King, III375bfee2017-10-26 00:09:34 -040070}
71
penenin1ab096c2020-05-18 12:27:31 -070072if (program.transport === "websocket") {
73 program.transport = "buffered";
74 type = "websocket";
75}
76
bforbisda1169d2018-10-28 11:27:38 -040077const options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080078 transport: helpers.transports[program.transport],
79 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010080};
81
bforbisda1169d2018-10-28 11:27:38 -040082if (type === "http" || type === "websocket") {
83 options.path = "/test";
Roger Meier57b354b2014-02-22 01:01:58 +010084}
Roger Meier8909cbd2014-01-26 11:44:27 +010085
bforbisda1169d2018-10-28 11:27:38 -040086if (type === "http") {
87 options.headers = { Connection: "close" };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080088}
89
90if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -040091 if (type === "tcp" || type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080092 options.rejectUnauthorized = false;
bforbisda1169d2018-10-28 11:27:38 -040093 } else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080094 options.nodeOptions = { rejectUnauthorized: false };
95 options.https = true;
bforbisda1169d2018-10-28 11:27:38 -040096 } else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080097 options.wsOptions = { rejectUnauthorized: false };
98 options.secure = true;
99 }
100}
101
bforbisda1169d2018-10-28 11:27:38 -0400102let connection;
103let client;
104const testDriver = program.callback
105 ? ThriftTestDriver
106 : ThriftTestDriverPromise;
107if (helpers.ecmaMode === "es6" && program.callback) {
108 console.log("ES6 does not support callback style");
109 process.exit(0);
110}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800111
bforbisda1169d2018-10-28 11:27:38 -0400112if (type === "tcp" || type === "multiplex") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800113 if (domainSocket) {
114 connection = thrift.createUDSConnection(domainSocket, options);
115 } else {
bforbisda1169d2018-10-28 11:27:38 -0400116 connection = ssl
117 ? thrift.createSSLConnection(host, port, options)
118 : thrift.createConnection(host, port, options);
Daniel Shihe41de0f2018-03-21 08:28:38 +0800119 }
bforbisda1169d2018-10-28 11:27:38 -0400120} else if (type === "http") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800121 if (domainSocket) {
122 connection = thrift.createHttpUDSConnection(domainSocket, options);
123 } else {
124 connection = thrift.createHttpConnection(host, port, options);
125 }
bforbisda1169d2018-10-28 11:27:38 -0400126} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800127 connection = thrift.createWSConnection(host, port, options);
128 connection.open();
129}
Roger Meier8909cbd2014-01-26 11:44:27 +0100130
bforbisda1169d2018-10-28 11:27:38 -0400131connection.on("error", function(err) {
132 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +0100133});
134
bforbisda1169d2018-10-28 11:27:38 -0400135if (type === "tcp") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800136 client = thrift.createClient(ThriftTest, connection);
137 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400138} else if (type === "multiplex") {
139 const mp = new thrift.Multiplexer();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800140 client = mp.createClient("ThriftTest", ThriftTest, connection);
bforbisda1169d2018-10-28 11:27:38 -0400141 const secondclient = mp.createClient(
142 "SecondService",
143 SecondService,
144 connection
145 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800146
bforbisda1169d2018-10-28 11:27:38 -0400147 connection.on("connect", function() {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800148 secondclient.secondtestString("Test", function(err, response) {
149 assert(!err);
bforbisda1169d2018-10-28 11:27:38 -0400150 assert.equal('testString("Test")', response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800151 });
152
153 runTests();
154 });
bforbisda1169d2018-10-28 11:27:38 -0400155} else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800156 client = thrift.createHttpClient(ThriftTest, connection);
157 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400158} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800159 client = thrift.createWSClient(ThriftTest, connection);
160 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800161}
Roger Meier8909cbd2014-01-26 11:44:27 +0100162
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800163function runTests() {
bforbisda1169d2018-10-28 11:27:38 -0400164 testDriver(client, function(status) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800165 console.log(status);
bforbisda1169d2018-10-28 11:27:38 -0400166 if (type !== "http" && type !== "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800167 connection.end();
168 }
bforbisda1169d2018-10-28 11:27:38 -0400169 if (type !== "multiplex") {
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700170 process.exit(0);
171 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800172 });
173}
174
Roger Meier8909cbd2014-01-26 11:44:27 +0100175exports.expressoTest = function() {};