THRIFT-2493:Node.js lib needs HTTP client
Client: node
Patch: Randy Abernethy
Adds http client to node.js along with tests.
diff --git a/lib/nodejs/test/http_client.js b/lib/nodejs/test/http_client.js
new file mode 100644
index 0000000..08a7d27
--- /dev/null
+++ b/lib/nodejs/test/http_client.js
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * 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 ThriftTest = require('./gen-nodejs/ThriftTest');
+var ThriftTestDriver = require('./thrift_test_driver').ThriftTestDriver;
+var ThriftTestDriverPromise = require('./thrift_test_driver_promise').ThriftTestDriver;
+
+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('--ssl', 'use SSL transport')
+ .option('--promise', 'test with promise style functions')
+ .parse(process.argv);
+
+
+var protocol = thrift.TBinaryProtocol;
+if (program.protocol === "json") {
+ protocol = thrift.TJSONProtocol;
+}
+
+var transport = thrift.TBufferedTransport;
+if (program.transport === "framed") {
+ transport = thrift.TFramedTransport;
+}
+
+var options = {
+ transport: transport,
+ protocol: protocol,
+ headers: {"Connection": "close"},
+ path: "/test"
+};
+
+var connection = undefined;
+
+if (program.ssl) {
+ options.rejectUnauthorized = false;
+ connection = thrift.createHttpConnection("localhost", 9090, options);
+} else {
+ connection = thrift.createHttpConnection("localhost", 9090, options);
+}
+
+var client = thrift.createHttpClient(ThriftTest, connection);
+
+//connection.on('error', function(err) {
+// assert(false, err);
+//});
+
+var testDriver = ThriftTestDriver;
+if (program.promise) {
+ testDriver = ThriftTestDriverPromise;
+}
+testDriver(client, function (status) {
+ console.log(status);
+ process.exit(0);
+});
+
+// to make it also run on expresso
+exports.expressoTest = function() {};
diff --git a/lib/nodejs/test/http_server.js b/lib/nodejs/test/http_server.js
new file mode 100644
index 0000000..d8ef73f
--- /dev/null
+++ b/lib/nodejs/test/http_server.js
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * 'License'); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var fs = require('fs');
+var path = require('path');
+var thrift = require('thrift');
+var ThriftTest = require('./gen-nodejs/ThriftTest');
+var ThriftTestHandler = require('./test_handler').ThriftTestHandler;
+var ThriftTestHandlerPromise = require('./test_handler_promise').ThriftTestHandler;
+
+var program = require('commander');
+
+program
+ .option('-p, --protocol <protocol>', 'Set thift protocol (binary|json) [protocol]')
+ .option('-t, --transport <transport>', 'Set thift transport (buffered|framed) [transport]')
+ .option('--ssl', 'use ssl transport')
+ .option('--promise', 'test with promise style functions')
+ .parse(process.argv);
+
+var transport = thrift.TBufferedTransport;
+if (program.transport === "framed") {
+ transport = thrift.TFramedTransport;
+}
+
+var protocol = thrift.TBinaryProtocol;
+if (program.protocol === "json") {
+ protocol = thrift.TJSONProtocol;
+}
+
+var handler = ThriftTestHandler;
+if (program.promise) {
+ handler = ThriftTestHandlerPromise;
+}
+
+var SvcOpt = {
+ handler: handler,
+ processor: ThriftTest,
+ protocol: protocol,
+ transport: transport
+};
+var serverOpt = { services: { "/test": SvcOpt } }
+thrift.createWebServer(serverOpt).listen(9090);
+
+
diff --git a/lib/nodejs/test/testAll.sh b/lib/nodejs/test/testAll.sh
index 96f8a2a..87bbb9d 100755
--- a/lib/nodejs/test/testAll.sh
+++ b/lib/nodejs/test/testAll.sh
@@ -47,6 +47,18 @@
return $RET
}
+testHttpClientServer()
+{
+ echo " Testing HTTP Client/Server with protocol $1 and transport $2 $3";
+ RET=0
+ node ${DIR}/http_server.js -p $1 -t $2 $3 &
+ SERVERPID=$!
+ sleep 1
+ node ${DIR}/http_client.js -p $1 -t $2 $3 || RET=1
+ kill -9 $SERVERPID || RET=1
+ return $RET
+}
+
TESTOK=0
@@ -60,6 +72,7 @@
#integration tests
+#TCP connection tests
testClientServer binary buffered || TESTOK=1
testClientServer json buffered || TESTOK=1
testClientServer binary framed || TESTOK=1
@@ -78,4 +91,10 @@
#test promise style
testClientServer binary framed --promise || TESTOK=1
+#HTTP tests
+testHttpClientServer json buffered || TESTOK=1
+testHttpClientServer json framed || TESTOK=1
+testHttpClientServer binary buffered || TESTOK=1
+testHttpClientServer binary framed || TESTOK=1
+
exit $TESTOK
diff --git a/lib/nodejs/test/thrift_test_driver.js b/lib/nodejs/test/thrift_test_driver.js
index ccc105b..a21c9c5 100644
--- a/lib/nodejs/test/thrift_test_driver.js
+++ b/lib/nodejs/test/thrift_test_driver.js
@@ -288,7 +288,7 @@
test_complete = true;
});
-//We wait up to retry_limit * retry_interval for the test suite to complete
+ //We wait up to retry_limit * retry_interval for the test suite to complete
function TestForCompletion() {
if(test_complete) {
if (callback) {
@@ -298,14 +298,14 @@
if (++retrys < retry_limit) {
setTimeout(TestForCompletion, retry_interval);
} else {
- if (callback) {
- callback("Server test failed to complete after " +
- (retry_limit*retry_interval/1000) + " seconds");
- }
+ if (callback) {
+ callback("Server test failed to complete after " +
+ (retry_limit*retry_interval/1000) + " seconds");
+ }
}
}
}
setTimeout(TestForCompletion, retry_interval);
})();
-}
+};