THRIFT-2969
Client: nodejs
Patch: Andrew de Andrade

General node test cleanup and consolidation.
diff --git a/lib/nodejs/test/client.js b/lib/nodejs/test/client.js
old mode 100755
new mode 100644
index b8de7ce..6d512b4
--- a/lib/nodejs/test/client.js
+++ b/lib/nodejs/test/client.js
@@ -1,4 +1,4 @@
-#!/usr/bin/env nodejs
+#!/usr/bin/env node
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -19,80 +19,111 @@
  * under the License.
  */
 
-//This is the client side test for the standard Apache Thrift
-//"ThriftTest" suite. This client will test any protocol/transport
-//combination specified on the command line.
-
 var fs = require('fs');
 var assert = require('assert');
 var thrift = require('thrift');
+var helpers = require('./helpers');
 var ThriftTest = require('./gen-nodejs/ThriftTest');
-var ThriftTestDriver = require('./thrift_test_driver').ThriftTestDriver;
-var ThriftTestDriverPromise = require('./thrift_test_driver_promise').ThriftTestDriver;
+var ThriftTestDriver = require('./test_driver').ThriftTestDriver;
+var ThriftTestDriverPromise = require('./test_driver').ThriftTestDriverPromise;
+var SecondService = require('./gen-nodejs/SecondService');
+var ttypes = require('./gen-nodejs/ThriftTest_types');
 
 var program = require('commander');
 
 program
   .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json) [protocol]')
   .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed) [transport]')
-  .option('--port <port>', 'Set thift server port number to connect')
-  .option('--host <host>', 'Set thift server host to connect')
+  .option('--port <port>', 'Set thift server port number to connect', 9090)
+  .option('--host <host>', 'Set thift server host to connect', 'localhost')
   .option('--ssl', 'use SSL transport')
   .option('--promise', 'test with promise style functions')
+  .option('-t, --type <type>', 'Select server type (tcp|multiplex|http)', 'tcp')
   .parse(process.argv);
 
-var host = "localhost";
-if(String(program.host) === "undefined") {
-}else{
-   host = program.host;
-}
-
-var port = 9090;
-if(String(program.port) === "undefined") {
-}else{
-  port = program.port;
-}
-
-var protocol = thrift.TBinaryProtocol;
-if (program.protocol === "json") {
-  protocol = thrift.TJSONProtocol;
-} else if (program.protocol === "compact") {
-  protocol = thrift.TCompactProtocol;
-}
-
-var transport =  thrift.TBufferedTransport;
-if (program.transport === "framed") {
-  transport = thrift.TFramedTransport;
-}
+var host = program.host;
+var port = program.port;
+var type = program.type;
+var ssl = program.ssl;
+var promise = program.promise;
 
 var options = {
-  transport: transport,
-  protocol: protocol
+  transport: helpers.transports[program.transport],
+  protocol: helpers.protocols[program.protocol]
 };
 
-var connection;
-
-if (program.ssl) {
-  options.rejectUnauthorized = false;
-  connection = thrift.createSSLConnection(host, port, options);
-} else {
-  connection = thrift.createConnection(host,port, options);
+if (type === 'http' || type === 'websocket') {
+  options.path = '/test';
 }
 
-var client = thrift.createClient(ThriftTest, connection);
+if (type === 'http') {
+  options.headers = {"Connection": "close"};
+}
+
+if (ssl) {
+  if (type === 'tcp' || type === 'multiplex') {
+    options.rejectUnauthorized = false;
+  } else if (type === 'http') {
+    options.nodeOptions = { rejectUnauthorized: false };
+    options.https = true;
+  } else if (type === 'websocket') {
+    options.wsOptions = { rejectUnauthorized: false };
+    options.secure = true;
+  }
+}
+
+var connection;
+var client;
+var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver;
+
+if (type === 'tcp' || type === 'multiplex') {
+  connection = ssl ?
+    thrift.createSSLConnection(host, port, options) :
+    thrift.createConnection(host, port, options);
+} else if (type === 'http') {
+  connection = thrift.createHttpConnection(host, port, options);
+} else if (type === 'websocket') {
+  connection = thrift.createWSConnection(host, port, options);
+  connection.open();
+}
 
 connection.on('error', function(err) {
-  assert(false, err);
+    assert(false, err);
 });
 
-var testDriver = ThriftTestDriver;
-if (program.promise) {
-  testDriver = ThriftTestDriverPromise;
+if (type === 'tcp') {
+  client = thrift.createClient(ThriftTest, connection);
+  runTests();
+} else if (type === 'multiplex') {
+  var mp = new thrift.Multiplexer();
+  client = mp.createClient("ThriftTest", ThriftTest, connection);
+  secondclient = mp.createClient("SecondService", SecondService, connection);
+
+  connection.on('connect', function() {
+    secondclient.secondtestString("Test", function(err, response) {
+      assert(!err);
+      assert.equal("Test", response);
+    });
+
+    runTests();
+  });
+} else if (type === 'http') {
+  client = thrift.createHttpClient(ThriftTest, connection);
+  runTests();
+} else if (type === 'websocket') {
+  client = thrift.createWSClient(ThriftTest, connection);
+  runTests();
 }
-testDriver(client, function (status) {
-  console.log(status);
-  connection.end();
-});
 
-// to make it also run on expresso
+function runTests() {
+  testDriver(client, function (status) {
+    console.log(status);
+    if (type === 'http' || type === 'websocket') {
+      process.exit(0);
+    } else {
+      connection.end();
+    }
+  });
+}
+
 exports.expressoTest = function() {};