Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env node |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 2 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 3 | /* |
| 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 Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 22 | var fs = require('fs'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 23 | var assert = require('assert'); |
| 24 | var thrift = require('thrift'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 25 | var helpers = require('./helpers'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 26 | var ThriftTest = require('./gen-nodejs/ThriftTest'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 27 | var ThriftTestDriver = require('./test_driver').ThriftTestDriver; |
| 28 | var ThriftTestDriverPromise = require('./test_driver').ThriftTestDriverPromise; |
| 29 | var SecondService = require('./gen-nodejs/SecondService'); |
| 30 | var ttypes = require('./gen-nodejs/ThriftTest_types'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 31 | |
| 32 | var program = require('commander'); |
| 33 | |
| 34 | program |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 35 | .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json) [protocol]') |
| 36 | .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed) [transport]') |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 37 | .option('--port <port>', 'Set thift server port number to connect', 9090) |
| 38 | .option('--host <host>', 'Set thift server host to connect', 'localhost') |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 39 | .option('--ssl', 'use SSL transport') |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 40 | .option('--promise', 'test with promise style functions') |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 41 | .option('-t, --type <type>', 'Select server type (tcp|multiplex|http)', 'tcp') |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 42 | .parse(process.argv); |
| 43 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 44 | var host = program.host; |
| 45 | var port = program.port; |
| 46 | var type = program.type; |
| 47 | var ssl = program.ssl; |
| 48 | var promise = program.promise; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 49 | |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 50 | var options = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 51 | transport: helpers.transports[program.transport], |
| 52 | protocol: helpers.protocols[program.protocol] |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 55 | if (type === 'http' || type === 'websocket') { |
| 56 | options.path = '/test'; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 57 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 58 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 59 | if (type === 'http') { |
| 60 | options.headers = {"Connection": "close"}; |
| 61 | } |
| 62 | |
| 63 | if (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 | |
| 75 | var connection; |
| 76 | var client; |
| 77 | var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver; |
| 78 | |
| 79 | if (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 Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 89 | |
| 90 | connection.on('error', function(err) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 91 | assert(false, err); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 92 | }); |
| 93 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 94 | if (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 Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 116 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 117 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame^] | 118 | function 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 Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 129 | exports.expressoTest = function() {}; |