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 |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame] | 35 | .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|compact|json) [protocol]') |
| 36 | .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed|http) [transport]') |
Roger Meier | 47accfc | 2015-03-24 21:01:46 +0100 | [diff] [blame] | 37 | .option('--port <port>', 'Set thrift server port number to connect', 9090) |
| 38 | .option('--host <host>', 'Set thrift 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') |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame] | 41 | .option('-t, --type <type>', 'Select server type (http|multiplex|tcp|websocket)', '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 | |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame] | 50 | /* for compatibility with cross test invocation for http transport testing */ |
| 51 | if (program.transport === 'http') { |
| 52 | program.transport = 'buffered'; |
| 53 | type = 'http'; |
| 54 | } |
| 55 | |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 56 | var options = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 57 | transport: helpers.transports[program.transport], |
| 58 | protocol: helpers.protocols[program.protocol] |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 61 | if (type === 'http' || type === 'websocket') { |
| 62 | options.path = '/test'; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 63 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 64 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 65 | if (type === 'http') { |
| 66 | options.headers = {"Connection": "close"}; |
| 67 | } |
| 68 | |
| 69 | if (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 | |
| 81 | var connection; |
| 82 | var client; |
| 83 | var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver; |
| 84 | |
| 85 | if (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 Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 95 | |
| 96 | connection.on('error', function(err) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 97 | assert(false, err); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 98 | }); |
| 99 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 100 | if (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, III | 58402ff | 2017-11-17 14:41:46 -0500 | [diff] [blame] | 111 | assert.equal("testString(\"Test\")", response); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 112 | }); |
| 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 Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 122 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 123 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 124 | function runTests() { |
| 125 | testDriver(client, function (status) { |
| 126 | console.log(status); |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 127 | if (type !== 'http' && type !== 'websocket') { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 128 | connection.end(); |
| 129 | } |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 130 | if (type !== 'multiplex') { |
| 131 | process.exit(0); |
| 132 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 133 | }); |
| 134 | } |
| 135 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 136 | exports.expressoTest = function() {}; |