blob: 55839f6165b5360895a756fbb04e877b678f5653 [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
Roger Meier57b354b2014-02-22 01:01:58 +010022var fs = require('fs');
Roger Meier8909cbd2014-01-26 11:44:27 +010023var assert = require('assert');
24var thrift = require('thrift');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080025var helpers = require('./helpers');
Roger Meier8909cbd2014-01-26 11:44:27 +010026var ThriftTest = require('./gen-nodejs/ThriftTest');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080027var ThriftTestDriver = require('./test_driver').ThriftTestDriver;
28var ThriftTestDriverPromise = require('./test_driver').ThriftTestDriverPromise;
29var SecondService = require('./gen-nodejs/SecondService');
30var ttypes = require('./gen-nodejs/ThriftTest_types');
Roger Meier8909cbd2014-01-26 11:44:27 +010031
32var program = require('commander');
33
34program
James E. King, III375bfee2017-10-26 00:09:34 -040035 .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|compact|json) [protocol]')
36 .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed|http) [transport]')
Roger Meier47accfc2015-03-24 21:01:46 +010037 .option('--port <port>', 'Set thrift server port number to connect', 9090)
38 .option('--host <host>', 'Set thrift server host to connect', 'localhost')
Daniel Shihe41de0f2018-03-21 08:28:38 +080039 .option('--domain-socket <path>', 'Set thrift server unix domain socket to connect')
Roger Meier57b354b2014-02-22 01:01:58 +010040 .option('--ssl', 'use SSL transport')
henrique31236232014-02-23 20:16:44 +010041 .option('--promise', 'test with promise style functions')
James E. King, III375bfee2017-10-26 00:09:34 -040042 .option('-t, --type <type>', 'Select server type (http|multiplex|tcp|websocket)', 'tcp')
Roger Meier8909cbd2014-01-26 11:44:27 +010043 .parse(process.argv);
44
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080045var host = program.host;
46var port = program.port;
Daniel Shihe41de0f2018-03-21 08:28:38 +080047var domainSocket = program.domainSocket;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080048var type = program.type;
49var ssl = program.ssl;
50var promise = program.promise;
henrique31236232014-02-23 20:16:44 +010051
James E. King, III375bfee2017-10-26 00:09:34 -040052/* for compatibility with cross test invocation for http transport testing */
53if (program.transport === 'http') {
54 program.transport = 'buffered';
55 type = 'http';
56}
57
Roger Meier57b354b2014-02-22 01:01:58 +010058var options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080059 transport: helpers.transports[program.transport],
60 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010061};
62
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080063if (type === 'http' || type === 'websocket') {
64 options.path = '/test';
Roger Meier57b354b2014-02-22 01:01:58 +010065}
Roger Meier8909cbd2014-01-26 11:44:27 +010066
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080067if (type === 'http') {
68 options.headers = {"Connection": "close"};
69}
70
71if (ssl) {
72 if (type === 'tcp' || type === 'multiplex') {
73 options.rejectUnauthorized = false;
74 } else if (type === 'http') {
75 options.nodeOptions = { rejectUnauthorized: false };
76 options.https = true;
77 } else if (type === 'websocket') {
78 options.wsOptions = { rejectUnauthorized: false };
79 options.secure = true;
80 }
81}
82
83var connection;
84var client;
85var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver;
86
87if (type === 'tcp' || type === 'multiplex') {
Daniel Shihe41de0f2018-03-21 08:28:38 +080088 if (domainSocket) {
89 connection = thrift.createUDSConnection(domainSocket, options);
90 } else {
91 connection = ssl ?
92 thrift.createSSLConnection(host, port, options) :
93 thrift.createConnection(host, port, options);
94 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080095} else if (type === 'http') {
Daniel Shihe41de0f2018-03-21 08:28:38 +080096 if (domainSocket) {
97 connection = thrift.createHttpUDSConnection(domainSocket, options);
98 } else {
99 connection = thrift.createHttpConnection(host, port, options);
100 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800101} else if (type === 'websocket') {
102 connection = thrift.createWSConnection(host, port, options);
103 connection.open();
104}
Roger Meier8909cbd2014-01-26 11:44:27 +0100105
106connection.on('error', function(err) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800107 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +0100108});
109
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800110if (type === 'tcp') {
111 client = thrift.createClient(ThriftTest, connection);
112 runTests();
113} else if (type === 'multiplex') {
114 var mp = new thrift.Multiplexer();
115 client = mp.createClient("ThriftTest", ThriftTest, connection);
116 secondclient = mp.createClient("SecondService", SecondService, connection);
117
118 connection.on('connect', function() {
119 secondclient.secondtestString("Test", function(err, response) {
120 assert(!err);
James E. King, III58402ff2017-11-17 14:41:46 -0500121 assert.equal("testString(\"Test\")", response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800122 });
123
124 runTests();
125 });
126} else if (type === 'http') {
127 client = thrift.createHttpClient(ThriftTest, connection);
128 runTests();
129} else if (type === 'websocket') {
130 client = thrift.createWSClient(ThriftTest, connection);
131 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800132}
Roger Meier8909cbd2014-01-26 11:44:27 +0100133
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800134function runTests() {
135 testDriver(client, function (status) {
136 console.log(status);
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700137 if (type !== 'http' && type !== 'websocket') {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800138 connection.end();
139 }
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700140 if (type !== 'multiplex') {
141 process.exit(0);
142 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800143 });
144}
145
Roger Meier8909cbd2014-01-26 11:44:27 +0100146exports.expressoTest = function() {};