blob: 6d512b476d451c1458db67feed958ea41870431e [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
henriquea2de4102014-02-07 14:12:56 +010035 .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json) [protocol]')
36 .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed) [transport]')
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080037 .option('--port <port>', 'Set thift server port number to connect', 9090)
38 .option('--host <host>', 'Set thift 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')
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080041 .option('-t, --type <type>', 'Select server type (tcp|multiplex|http)', '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
Roger Meier57b354b2014-02-22 01:01:58 +010050var options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080051 transport: helpers.transports[program.transport],
52 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010053};
54
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080055if (type === 'http' || type === 'websocket') {
56 options.path = '/test';
Roger Meier57b354b2014-02-22 01:01:58 +010057}
Roger Meier8909cbd2014-01-26 11:44:27 +010058
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080059if (type === 'http') {
60 options.headers = {"Connection": "close"};
61}
62
63if (ssl) {
64 if (type === 'tcp' || type === 'multiplex') {
65 options.rejectUnauthorized = false;
66 } else if (type === 'http') {
67 options.nodeOptions = { rejectUnauthorized: false };
68 options.https = true;
69 } else if (type === 'websocket') {
70 options.wsOptions = { rejectUnauthorized: false };
71 options.secure = true;
72 }
73}
74
75var connection;
76var client;
77var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver;
78
79if (type === 'tcp' || type === 'multiplex') {
80 connection = ssl ?
81 thrift.createSSLConnection(host, port, options) :
82 thrift.createConnection(host, port, options);
83} else if (type === 'http') {
84 connection = thrift.createHttpConnection(host, port, options);
85} else if (type === 'websocket') {
86 connection = thrift.createWSConnection(host, port, options);
87 connection.open();
88}
Roger Meier8909cbd2014-01-26 11:44:27 +010089
90connection.on('error', function(err) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080091 assert(false, err);
Roger Meier8909cbd2014-01-26 11:44:27 +010092});
93
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080094if (type === 'tcp') {
95 client = thrift.createClient(ThriftTest, connection);
96 runTests();
97} else if (type === 'multiplex') {
98 var mp = new thrift.Multiplexer();
99 client = mp.createClient("ThriftTest", ThriftTest, connection);
100 secondclient = mp.createClient("SecondService", SecondService, connection);
101
102 connection.on('connect', function() {
103 secondclient.secondtestString("Test", function(err, response) {
104 assert(!err);
105 assert.equal("Test", response);
106 });
107
108 runTests();
109 });
110} else if (type === 'http') {
111 client = thrift.createHttpClient(ThriftTest, connection);
112 runTests();
113} else if (type === 'websocket') {
114 client = thrift.createWSClient(ThriftTest, connection);
115 runTests();
Randy Abernethy96f4f072015-02-10 02:29:15 -0800116}
Roger Meier8909cbd2014-01-26 11:44:27 +0100117
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800118function runTests() {
119 testDriver(client, function (status) {
120 console.log(status);
121 if (type === 'http' || type === 'websocket') {
122 process.exit(0);
123 } else {
124 connection.end();
125 }
126 });
127}
128
Roger Meier8909cbd2014-01-26 11:44:27 +0100129exports.expressoTest = function() {};