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 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 22 | const assert = require("assert"); |
| 23 | const thrift = require("thrift"); |
| 24 | const helpers = require("./helpers"); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 25 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 26 | const ThriftTest = require(`./${helpers.genPath}/ThriftTest`); |
| 27 | const ThriftTestDriver = require("./test_driver").ThriftTestDriver; |
| 28 | const ThriftTestDriverPromise = require("./test_driver") |
| 29 | .ThriftTestDriverPromise; |
| 30 | const SecondService = require(`./${helpers.genPath}/SecondService`); |
| 31 | |
| 32 | const program = require("commander"); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 33 | |
| 34 | program |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 35 | .option( |
| 36 | "-p, --protocol <protocol>", |
| 37 | "Set thrift protocol (binary|compact|json) [protocol]" |
| 38 | ) |
| 39 | .option( |
| 40 | "-t, --transport <transport>", |
| 41 | "Set thrift transport (buffered|framed|http) [transport]" |
| 42 | ) |
| 43 | .option("--port <port>", "Set thrift server port number to connect", 9090) |
| 44 | .option("--host <host>", "Set thrift server host to connect", "localhost") |
| 45 | .option( |
| 46 | "--domain-socket <path>", |
| 47 | "Set thrift server unix domain socket to connect" |
| 48 | ) |
| 49 | .option("--ssl", "use SSL transport") |
| 50 | .option("--callback", "test with callback style functions") |
| 51 | .option( |
| 52 | "-t, --type <type>", |
| 53 | "Select server type (http|multiplex|tcp|websocket)", |
| 54 | "tcp" |
| 55 | ) |
| 56 | .option("--es6", "Use es6 code") |
| 57 | .option("--es5", "Use es5 code") |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 58 | .parse(process.argv); |
| 59 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 60 | const host = program.host; |
| 61 | const port = program.port; |
| 62 | const domainSocket = program.domainSocket; |
| 63 | const ssl = program.ssl; |
| 64 | let type = program.type; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 65 | |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame] | 66 | /* for compatibility with cross test invocation for http transport testing */ |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 67 | if (program.transport === "http") { |
| 68 | program.transport = "buffered"; |
| 69 | type = "http"; |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame] | 70 | } |
| 71 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 72 | const options = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 73 | transport: helpers.transports[program.transport], |
| 74 | protocol: helpers.protocols[program.protocol] |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 77 | if (type === "http" || type === "websocket") { |
| 78 | options.path = "/test"; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 79 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 80 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 81 | if (type === "http") { |
| 82 | options.headers = { Connection: "close" }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (ssl) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 86 | if (type === "tcp" || type === "multiplex") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 87 | options.rejectUnauthorized = false; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 88 | } else if (type === "http") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 89 | options.nodeOptions = { rejectUnauthorized: false }; |
| 90 | options.https = true; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 91 | } else if (type === "websocket") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 92 | options.wsOptions = { rejectUnauthorized: false }; |
| 93 | options.secure = true; |
| 94 | } |
| 95 | } |
| 96 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 97 | let connection; |
| 98 | let client; |
| 99 | const testDriver = program.callback |
| 100 | ? ThriftTestDriver |
| 101 | : ThriftTestDriverPromise; |
| 102 | if (helpers.ecmaMode === "es6" && program.callback) { |
| 103 | console.log("ES6 does not support callback style"); |
| 104 | process.exit(0); |
| 105 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 106 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 107 | if (type === "tcp" || type === "multiplex") { |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 108 | if (domainSocket) { |
| 109 | connection = thrift.createUDSConnection(domainSocket, options); |
| 110 | } else { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 111 | connection = ssl |
| 112 | ? thrift.createSSLConnection(host, port, options) |
| 113 | : thrift.createConnection(host, port, options); |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 114 | } |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 115 | } else if (type === "http") { |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 116 | if (domainSocket) { |
| 117 | connection = thrift.createHttpUDSConnection(domainSocket, options); |
| 118 | } else { |
| 119 | connection = thrift.createHttpConnection(host, port, options); |
| 120 | } |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 121 | } else if (type === "websocket") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 122 | connection = thrift.createWSConnection(host, port, options); |
| 123 | connection.open(); |
| 124 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 125 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 126 | connection.on("error", function(err) { |
| 127 | assert(false, err); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 128 | }); |
| 129 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 130 | if (type === "tcp") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 131 | client = thrift.createClient(ThriftTest, connection); |
| 132 | runTests(); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 133 | } else if (type === "multiplex") { |
| 134 | const mp = new thrift.Multiplexer(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 135 | client = mp.createClient("ThriftTest", ThriftTest, connection); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 136 | const secondclient = mp.createClient( |
| 137 | "SecondService", |
| 138 | SecondService, |
| 139 | connection |
| 140 | ); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 141 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 142 | connection.on("connect", function() { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 143 | secondclient.secondtestString("Test", function(err, response) { |
| 144 | assert(!err); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 145 | assert.equal('testString("Test")', response); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 146 | }); |
| 147 | |
| 148 | runTests(); |
| 149 | }); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 150 | } else if (type === "http") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 151 | client = thrift.createHttpClient(ThriftTest, connection); |
| 152 | runTests(); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 153 | } else if (type === "websocket") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 154 | client = thrift.createWSClient(ThriftTest, connection); |
| 155 | runTests(); |
Randy Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 156 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 157 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 158 | function runTests() { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 159 | testDriver(client, function(status) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 160 | console.log(status); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 161 | if (type !== "http" && type !== "websocket") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 162 | connection.end(); |
| 163 | } |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 164 | if (type !== "multiplex") { |
Randy Abernethy | 983bf7d | 2015-10-09 12:28:57 -0700 | [diff] [blame] | 165 | process.exit(0); |
| 166 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 167 | }); |
| 168 | } |
| 169 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 170 | exports.expressoTest = function() {}; |