blob: 49e3a5ec935e1c40c172e6dcf5e5344253ed67b6 [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
bforbisda1169d2018-10-28 11:27:38 -040072const options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080073 transport: helpers.transports[program.transport],
74 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010075};
76
bforbisda1169d2018-10-28 11:27:38 -040077if (type === "http" || type === "websocket") {
78 options.path = "/test";
Roger Meier57b354b2014-02-22 01:01:58 +010079}
Roger Meier8909cbd2014-01-26 11:44:27 +010080
bforbisda1169d2018-10-28 11:27:38 -040081if (type === "http") {
82 options.headers = { Connection: "close" };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080083}
84
85if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -040086 if (type === "tcp" || type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080087 options.rejectUnauthorized = false;
bforbisda1169d2018-10-28 11:27:38 -040088 } else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080089 options.nodeOptions = { rejectUnauthorized: false };
90 options.https = true;
bforbisda1169d2018-10-28 11:27:38 -040091 } else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080092 options.wsOptions = { rejectUnauthorized: false };
93 options.secure = true;
94 }
95}
96
bforbisda1169d2018-10-28 11:27:38 -040097let connection;
98let client;
99const testDriver = program.callback
100 ? ThriftTestDriver
101 : ThriftTestDriverPromise;
102if (helpers.ecmaMode === "es6" && program.callback) {
103 console.log("ES6 does not support callback style");
104 process.exit(0);
105}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800106
bforbisda1169d2018-10-28 11:27:38 -0400107if (type === "tcp" || type === "multiplex") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800108 if (domainSocket) {
109 connection = thrift.createUDSConnection(domainSocket, options);
110 } else {
bforbisda1169d2018-10-28 11:27:38 -0400111 connection = ssl
112 ? thrift.createSSLConnection(host, port, options)
113 : thrift.createConnection(host, port, options);
Daniel Shihe41de0f2018-03-21 08:28:38 +0800114 }
bforbisda1169d2018-10-28 11:27:38 -0400115} else if (type === "http") {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800116 if (domainSocket) {
117 connection = thrift.createHttpUDSConnection(domainSocket, options);
118 } else {
119 connection = thrift.createHttpConnection(host, port, options);
120 }
bforbisda1169d2018-10-28 11:27:38 -0400121} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800122 connection = thrift.createWSConnection(host, port, options);
123 connection.open();
124}
Roger Meier8909cbd2014-01-26 11:44:27 +0100125
bforbisda1169d2018-10-28 11:27:38 -0400126connection.on("error", function(err) {
127 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +0100128});
129
bforbisda1169d2018-10-28 11:27:38 -0400130if (type === "tcp") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800131 client = thrift.createClient(ThriftTest, connection);
132 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400133} else if (type === "multiplex") {
134 const mp = new thrift.Multiplexer();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800135 client = mp.createClient("ThriftTest", ThriftTest, connection);
bforbisda1169d2018-10-28 11:27:38 -0400136 const secondclient = mp.createClient(
137 "SecondService",
138 SecondService,
139 connection
140 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800141
bforbisda1169d2018-10-28 11:27:38 -0400142 connection.on("connect", function() {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800143 secondclient.secondtestString("Test", function(err, response) {
144 assert(!err);
bforbisda1169d2018-10-28 11:27:38 -0400145 assert.equal('testString("Test")', response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800146 });
147
148 runTests();
149 });
bforbisda1169d2018-10-28 11:27:38 -0400150} else if (type === "http") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800151 client = thrift.createHttpClient(ThriftTest, connection);
152 runTests();
bforbisda1169d2018-10-28 11:27:38 -0400153} else if (type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800154 client = thrift.createWSClient(ThriftTest, connection);
155 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800156}
Roger Meier8909cbd2014-01-26 11:44:27 +0100157
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800158function runTests() {
bforbisda1169d2018-10-28 11:27:38 -0400159 testDriver(client, function(status) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800160 console.log(status);
bforbisda1169d2018-10-28 11:27:38 -0400161 if (type !== "http" && type !== "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800162 connection.end();
163 }
bforbisda1169d2018-10-28 11:27:38 -0400164 if (type !== "multiplex") {
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700165 process.exit(0);
166 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800167 });
168}
169
Roger Meier8909cbd2014-01-26 11:44:27 +0100170exports.expressoTest = function() {};