blob: 006fad2dc23510e485d8cbfd12e2c133ee41a380 [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')
Roger Meier57b354b2014-02-22 01:01:58 +010039 .option('--ssl', 'use SSL transport')
henrique31236232014-02-23 20:16:44 +010040 .option('--promise', 'test with promise style functions')
James E. King, III375bfee2017-10-26 00:09:34 -040041 .option('-t, --type <type>', 'Select server type (http|multiplex|tcp|websocket)', 'tcp')
Roger Meier8909cbd2014-01-26 11:44:27 +010042 .parse(process.argv);
43
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080044var host = program.host;
45var port = program.port;
46var type = program.type;
47var ssl = program.ssl;
48var promise = program.promise;
henrique31236232014-02-23 20:16:44 +010049
James E. King, III375bfee2017-10-26 00:09:34 -040050/* for compatibility with cross test invocation for http transport testing */
51if (program.transport === 'http') {
52 program.transport = 'buffered';
53 type = 'http';
54}
55
Roger Meier57b354b2014-02-22 01:01:58 +010056var options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080057 transport: helpers.transports[program.transport],
58 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010059};
60
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080061if (type === 'http' || type === 'websocket') {
62 options.path = '/test';
Roger Meier57b354b2014-02-22 01:01:58 +010063}
Roger Meier8909cbd2014-01-26 11:44:27 +010064
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080065if (type === 'http') {
66 options.headers = {"Connection": "close"};
67}
68
69if (ssl) {
70 if (type === 'tcp' || type === 'multiplex') {
71 options.rejectUnauthorized = false;
72 } else if (type === 'http') {
73 options.nodeOptions = { rejectUnauthorized: false };
74 options.https = true;
75 } else if (type === 'websocket') {
76 options.wsOptions = { rejectUnauthorized: false };
77 options.secure = true;
78 }
79}
80
81var connection;
82var client;
83var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver;
84
85if (type === 'tcp' || type === 'multiplex') {
86 connection = ssl ?
87 thrift.createSSLConnection(host, port, options) :
88 thrift.createConnection(host, port, options);
89} else if (type === 'http') {
90 connection = thrift.createHttpConnection(host, port, options);
91} else if (type === 'websocket') {
92 connection = thrift.createWSConnection(host, port, options);
93 connection.open();
94}
Roger Meier8909cbd2014-01-26 11:44:27 +010095
96connection.on('error', function(err) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080097 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +010098});
99
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800100if (type === 'tcp') {
101 client = thrift.createClient(ThriftTest, connection);
102 runTests();
103} else if (type === 'multiplex') {
104 var mp = new thrift.Multiplexer();
105 client = mp.createClient("ThriftTest", ThriftTest, connection);
106 secondclient = mp.createClient("SecondService", SecondService, connection);
107
108 connection.on('connect', function() {
109 secondclient.secondtestString("Test", function(err, response) {
110 assert(!err);
James E. King, III58402ff2017-11-17 14:41:46 -0500111 assert.equal("testString(\"Test\")", response);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800112 });
113
114 runTests();
115 });
116} else if (type === 'http') {
117 client = thrift.createHttpClient(ThriftTest, connection);
118 runTests();
119} else if (type === 'websocket') {
120 client = thrift.createWSClient(ThriftTest, connection);
121 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800122}
Roger Meier8909cbd2014-01-26 11:44:27 +0100123
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800124function runTests() {
125 testDriver(client, function (status) {
126 console.log(status);
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700127 if (type !== 'http' && type !== 'websocket') {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800128 connection.end();
129 }
Randy Abernethy983bf7d2015-10-09 12:28:57 -0700130 if (type !== 'multiplex') {
131 process.exit(0);
132 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800133 });
134}
135
Roger Meier8909cbd2014-01-26 11:44:27 +0100136exports.expressoTest = function() {};