summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYing-Chun Liu (PaulLiu) <paulliu@debian.org>2017-10-04 18:36:24 +0200
committerYing-Chun Liu (PaulLiu) <paulliu@debian.org>2017-10-04 18:36:24 +0200
commitbcb03bad9c620a83cf66efee3f76489e15be65d9 (patch)
tree0f609be114f293d04ce05706f62e36f99cc5df8f
Import node-node-rest-client_2.5.0.orig.tar.gz
[dgit import orig node-node-rest-client_2.5.0.orig.tar.gz]
-rw-r--r--.npmignore11
-rw-r--r--LICENSE13
-rw-r--r--lib/node-rest-client.js657
-rw-r--r--package.json24
-rw-r--r--readme.md482
-rw-r--r--test/local-test.js12
-rw-r--r--test/message.json34
-rw-r--r--test/message.xml43
-rw-r--r--test/specs.js32
-rw-r--r--test/test-proxy.js85
-rw-r--r--test/test-servers.js81
11 files changed, 1474 insertions, 0 deletions
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..e0ab719
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,11 @@
+
+test/man-test.js
+
+test/spec/test.js
+
+node_modules
+*.log
+
+test/multiple-clients-test.js
+
+test/manual-test.js
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..64f4138
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2013 Alejandro Alvarez Acero. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
+THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/lib/node-rest-client.js b/lib/node-rest-client.js
new file mode 100644
index 0000000..dc6893d
--- /dev/null
+++ b/lib/node-rest-client.js
@@ -0,0 +1,657 @@
+var http = require('follow-redirects').http,
+ https = require('follow-redirects').https,
+ parseString = require('xml2js').parseString,
+ urlParser = require('url'),
+ util = require("util"),
+ events = require("events"),
+ zlib = require("zlib"),
+ node_debug = require("debug")("NRC");
+
+exports.Client = function (options){
+ var self = this;
+ var connectManager = new ConnectManager();
+ //declare util constants
+ var CONSTANTS={
+ HEADER_CONTENT_LENGTH:"Content-Length"
+ };
+
+
+ self.options = options || {},
+ self.useProxy = (self.options.proxy || false)?true:false,
+ self.useProxyTunnel = (!self.useProxy || self.options.proxy.tunnel===undefined)?false:self.options.proxy.tunnel,
+ self.proxy = self.options.proxy,
+ self.connection = self.options.connection || {},
+ self.mimetypes = self.options.mimetypes || {},
+ self.requestConfig = self.options.requestConfig || {},
+ self.responseConfig = self.options.responseConfig || {};
+
+ this.methods={};
+
+ // Client Request to be passed to ConnectManager and returned
+ // for each REST method invocation
+ var ClientRequest =function(){
+ events.EventEmitter.call(this);
+ };
+
+
+ util.inherits(ClientRequest, events.EventEmitter);
+
+
+ ClientRequest.prototype.end = function(){
+ if(this._httpRequest) {
+ this._httpRequest.end();
+ }
+ };
+
+ ClientRequest.prototype.setHttpRequest=function(req){
+ this._httpRequest = req;
+ };
+
+
+
+ var Util = {
+ createProxyPath:function(url){
+ var result = url.host;
+ // check url protocol to set path in request options
+ if (url.protocol === "https:"){
+ // port is set, leave it, otherwise use default https 443
+ result = (url.host.indexOf(":") == -1?url.hostname + ":443":url.host);
+ }
+
+ return result;
+ },
+ createProxyHeaders:function(url){
+ var result ={};
+ // if proxy requires authentication, create Proxy-Authorization headers
+ if (self.proxy.user && self.proxy.password){
+ result["Proxy-Authorization"] = "Basic " + new Buffer([self.proxy.user,self.proxy.password].join(":")).toString("base64");
+ }
+ // no tunnel proxy connection, we add the host to the headers
+ if(!self.useProxyTunnel)
+ result["host"] = url.host;
+
+ return result;
+ },
+ createConnectOptions:function(connectURL, connectMethod){
+ debug("connect URL = ", connectURL);
+ var url = urlParser.parse(connectURL),
+ path,
+ result={},
+ protocol = url.protocol.indexOf(":") == -1?url.protocol:url.protocol.substring(0,url.protocol.indexOf(":")),
+ defaultPort = protocol === 'http'?80:443;
+
+ result ={
+ host: url.host.indexOf(":") == -1?url.host:url.host.substring(0,url.host.indexOf(":")),
+ port: url.port === undefined?defaultPort:url.port,
+ path: url.path,
+ protocol:protocol,
+ href:url.href
+ };
+
+ if (self.useProxy) result.agent = false; // cannot use default agent in proxy mode
+
+ if (self.options.user && self.options.password){
+ result.auth = [self.options.user,self.options.password].join(":");
+
+ } else if (self.options.user && !self.options.password){
+ // some sites only needs user with no password to authenticate
+ result.auth = self.options.user + ":";
+ }
+
+
+ // configure proxy connection to establish a tunnel
+ if (self.useProxy){
+
+ result.proxy ={
+ host: self.proxy.host,
+ port: self.proxy.port,
+ method: self.useProxyTunnel?'CONNECT':connectMethod,//if proxy tunnel use 'CONNECT' method, else get method from request,
+ path: self.useProxyTunnel?this.createProxyPath(url):connectURL, // if proxy tunnel set proxy path else get request path,
+ headers: this.createProxyHeaders(url) // createProxyHeaders add correct headers depending of proxy connection type
+ };
+ }
+
+ if(self.connection && typeof self.connection === 'object'){
+ for(var option in self.connection){
+ result[option] = self.connection[option];
+ }
+ }
+
+ // don't use tunnel to connect to proxy, direct request
+ // and delete proxy options
+ if (!self.useProxyTunnel){
+ for (option in result.proxy){
+ result[option] = result.proxy[option];
+ }
+
+ delete result.proxy;
+ }
+
+ // add general request and response config to connect options
+
+ result.requestConfig = self.requestConfig;
+ result.responseConfig = self.responseConfig;
+
+
+ return result;
+ },
+ decodeQueryFromURL: function(connectURL){
+ var url = urlParser.parse(connectURL),
+ query = url.query.substring(1).split("&"),
+ keyValue,
+ result={};
+
+ // create decoded args from key value elements in query+
+ for (var i=0;i<query.length;i++){
+ keyValue = query[i].split("=");
+ result[keyValue[0]] = decodeURIComponent(keyValue[1]);
+ }
+
+ return result;
+
+ },
+ encodeQueryFromArgs: function(args){
+ var result="?", counter = 1;
+ // create enconded URL from args
+ for (var key in args) {
+ var keyValue = "";
+ if ( args[key] instanceof Array ) {
+ /*
+ * We are dealing with an array in the query string ?key=Value0&key=Value1
+ * That a REST application translates into key=[Value0, Value1]
+ */
+ for ( var ii=0, sizeArray = args[key].length; ii < sizeArray; ii++ ) {
+ result = result.concat((counter > 1 ? "&": "") + key + "=" + encodeURIComponent(args[key][ii]));
+ counter++;
+ }
+ } else { //No array, just a single &key=value
+ keyValue = key + "=" + encodeURIComponent(args[key]);
+ result = result.concat((counter > 1 ? "&":"") + keyValue);
+ }
+
+ counter++;
+ }
+
+ return result;
+ },
+ parsePathParameters:function(args,url){
+ var result = url;
+ if (!args || !args.path) return url;
+
+ for (var placeholder in args.path){
+ var regex = new RegExp("\\$\\{" + placeholder + "\\}","i");
+ result = result.replace(regex,args.path[placeholder]);
+
+ }
+
+ return result;
+
+ },
+ overrideClientConfig:function(connectOptions,methodOptions){
+ function validateReqResOptions(reqResOption){
+ return (reqResOption && typeof reqResOption === 'object');
+ }
+ // check if we have particular request or response config set on this method invocation
+ // and override general request/response config
+ if (validateReqResOptions(methodOptions.requestConfig)){
+ util._extend(connectOptions.requestConfig,methodOptions.requestConfig);
+ }
+
+ if (validateReqResOptions(methodOptions.responseConfig)){
+ util._extend(connectOptions.responseConfig,methodOptions.responseConfig);
+ }
+
+
+ },
+ connect : function(method, url, args, callback, clientRequest){
+ // configure connect options based on url parameter parse
+ var options = this.createConnectOptions(this.parsePathParameters(args,url), method);
+ debug("options pre connect",options);
+ options.method = method,
+ clientRequest.href=options.href,
+ options.clientRequest = clientRequest,
+ options.headers= options.headers || {};
+
+ debug("args = ", args);
+ debug("args.data = ", args !== undefined?args.data:undefined);
+ // no args passed
+ if (typeof args === 'function'){
+ callback = args;
+ //add Content-length to POST/PUT/DELETE/PATCH methods
+ if (method === 'POST' || method === 'PUT' || method === 'DELETE' || method === 'PATCH'){
+ options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0;
+ }
+ } else if (typeof args === 'object') {
+ // add headers and POST/PUT/DELETE/PATCH data to connect options to be passed
+ // with request, but without deleting other headers like non-tunnel proxy headers
+ if (args.headers){
+ for (var headerName in args.headers){
+ if (args.headers.hasOwnProperty(headerName)) {
+ options.headers[headerName] = args.headers[headerName];
+ }
+ }
+
+ }
+
+ //always set Content-length header if not set previously
+ //set Content lentgh for some servers to work (nginx, apache)
+ if (args.data !== undefined && !options.headers.hasOwnProperty(CONSTANTS.HEADER_CONTENT_LENGTH)){
+ options.data = args.data;
+ options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = Buffer.byteLength((typeof args.data === 'string' ? args.data:JSON.stringify(args.data)), 'utf8');
+ }else{
+ options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0;
+ }
+ // we have args, go and check if we have parameters
+ if (args.parameters && Object.keys(args.parameters).length > 0){
+ // validate URL consistency, and fix it
+ options.path +=(options.path.charAt(url.length-1) === '?'?"?":"");
+ options.path = options.path.concat(Util.encodeQueryFromArgs(args.parameters));
+ debug("options.path after request parameters = ", options.path);
+ }
+
+ // override client config, by the moment just for request response config
+ this.overrideClientConfig(options,args);
+ }
+
+
+ debug("options post connect",options);
+ debug("FINAL SELF object ====>", self);
+
+ if (self.useProxy && self.useProxyTunnel){
+ connectManager.proxy(options,callback);
+ }else{
+ // normal connection and direct proxy connections (no tunneling)
+ connectManager.normal(options,callback);
+ }
+ },
+ mergeMimeTypes:function(mimetypes){
+ // merge mime-types passed as options to client
+ if (mimetypes && typeof mimetypes === "object"){
+ if (mimetypes.json && mimetypes.json instanceof Array && mimetypes.json.length > 0){
+ connectManager.jsonctype = mimetypes.json;
+ }else if (mimetypes.xml && mimetypes.xml instanceof Array && mimetypes.xml.length > 0){
+ connectManager.xmlctype = mimetypes.xml;
+ }
+ }
+ },
+ createHttpMethod:function(methodName){
+ return function(url, args, callback){
+ var clientRequest = new ClientRequest();
+ Util.connect(methodName.toUpperCase(), url, args, callback, clientRequest);
+ return clientRequest;
+ };
+ }
+ },
+ Method = function(url, method){
+ var httpMethod = self[method.toLowerCase()];
+
+ return function(args,callback){
+ var completeURL = url;
+ //no args
+ if (typeof args === 'function'){
+ callback = args;
+ args = {};
+ }else if (typeof args === 'object'){
+ // we have args, go and check if we have parameters
+ if (args.parameters && Object.keys(args.parameters).length > 0){
+ // validate URL consistency, and fix it
+ url +=(url.charAt(url.length-1) === '?'?"?":"");
+ completeURL = url.concat(Util.encodeQueryFromArgs(args.parameters));
+ //delete args parameters we don't need it anymore in registered
+ // method invocation
+ delete args.parameters;
+ }
+ }
+ return httpMethod(completeURL, args , callback);
+ };
+ };
+
+
+
+
+ this.get = Util.createHttpMethod("get");
+
+ this.post = Util.createHttpMethod("post");
+
+ this.put = Util.createHttpMethod("put");
+
+ this.delete = Util.createHttpMethod("delete");
+
+ this.patch = Util.createHttpMethod("patch");
+
+
+ this.registerMethod = function(name, url, method){
+ // create method in method registry with preconfigured REST invocation
+ // method
+ this.methods[name] = new Method(url,method);
+ };
+
+ this.unregisterMethod = function(name){
+ delete this.methods[name];
+ };
+
+ this.addCustomHttpMethod=function(methodName){
+ self[methodName.toLowerCase()] = Util.createHttpMethod(methodName);
+ }
+
+ // handle ConnectManager events
+ connectManager.on('error',function(err){
+ self.emit('error',err);
+ });
+
+ // merge mime types with connect manager
+ Util.mergeMimeTypes(self.mimetypes);
+ debug("ConnectManager", connectManager);
+
+};
+
+
+ var ConnectManager = function() {
+ this.xmlctype = ["application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8"];
+ this.jsonctype = ["application/json","application/json;charset=utf-8"],
+ this.isXML = function(content){
+ var result = false;
+ if (!content) return result;
+
+ for (var i=0; i<this.xmlctype.length;i++){
+ result = this.xmlctype[i].trim().toLowerCase() === content.trim().toLowerCase();
+ if (result) break;
+ }
+
+ return result;
+ },
+ this.isJSON = function(content){
+ var result = false;
+ if (!content) return result;
+
+ for (var i=0; i<this.jsonctype.length;i++){
+ result = this.jsonctype[i].trim().toLowerCase() === content.trim().toLowerCase();
+ if (result) break;
+ }
+
+ return result;
+ },
+ this.isValidData = function(data){
+ return data !== undefined && (data.length !== undefined && data.length > 0);
+ },
+ this.configureRequest = function(req, config, clientRequest){
+
+ if (config.timeout){
+ req.setTimeout(config.timeout, function(){
+ clientRequest.emit('requestTimeout',req);
+ });
+ }
+
+
+ if(config.noDelay)
+ req.setNoDelay(config.noDelay);
+
+ if(config.keepAlive)
+ req.setSocketKeepAlive(config.noDelay,config.keepAliveDelay || 0);
+
+ },
+ this.configureResponse = function(res,config, clientRequest){
+ if (config.timeout){
+ res.setTimeout(config.timeout, function(){
+ clientRequest.emit('responseTimeout',res);
+ res.close();
+ });
+ }
+ },
+ this.handleEnd = function(res,buffer,callback){
+
+ var self = this,
+ content = res.headers["content-type"],
+ encoding = res.headers["content-encoding"];
+
+ debug("content-type: ", content);
+ debug("content-encoding: ",encoding);
+
+ if(encoding !== undefined && encoding.indexOf("gzip") >= 0){
+ debug("gunzip");
+ zlib.gunzip(Buffer.concat(buffer),function(er,gunzipped){
+ self.handleResponse(res,gunzipped,callback);
+ });
+ }else if(encoding !== undefined && encoding.indexOf("deflate") >= 0){
+ debug("inflate");
+ zlib.inflate(Buffer.concat(buffer),function(er,inflated){
+ self.handleResponse(res,inflated,callback);
+ });
+ }else {
+ debug("not compressed");
+ self.handleResponse(res,Buffer.concat(buffer),callback);
+ }
+ },
+ this.handleResponse = function(res,data,callback){
+ var content = res.headers["content-type"] && res.headers["content-type"].replace(/ /g, '');
+
+ debug("response content is [",content,"]");
+ // XML data need to be parsed as JS object
+ if (this.isXML(content)){
+ debug("detected XML response");
+ parseString(data.toString(), function (err, result) {
+ callback(result, res);
+ });
+ }else if (this.isJSON(content)){
+ debug("detected JSON response");
+ var jsonData,
+ data = data.toString();
+ try {
+ jsonData = this.isValidData(data)?JSON.parse(data):data;
+ } catch (err) {
+ // Something went wrong when parsing json. This can happen
+ // for many reasons, including a bad implementation on the
+ // server.
+ jsonData = 'Error parsing response. response: [' +
+ data + '], error: [' + err + ']';
+
+ }
+ callback(jsonData, res);
+ }else{
+ debug("detected unknown response");
+ callback(data, res);
+ }
+ },
+ this.prepareData = function(data){
+ var result;
+ if ((data instanceof Buffer) || (typeof data !== 'object')){
+ result = data;
+ }else{
+ result = JSON.stringify(data);
+ }
+ return result;
+ },
+ this.proxy = function(options, callback){
+
+ debug("proxy options",options.proxy);
+
+ // creare a new proxy tunnel, and use to connect to API URL
+ var proxyTunnel = http.request(options.proxy),
+ self = this;
+
+
+ proxyTunnel.on('connect',function(res, socket, head){
+ debug("proxy connected",socket);
+
+ // set tunnel socket in request options, that's the tunnel itself
+ options.socket = socket;
+
+ var buffer=[],
+ protocol = (options.protocol =="http")?http:https,
+ clientRequest = options.clientRequest,
+ requestConfig = options.requestConfig,
+ responseConfig = options.responseConfig;
+
+ //remove "protocol" and "clientRequest" option from options, cos is not allowed by http/hppts node objects
+ delete options.protocol;
+ delete options.clientRequest;
+ delete options.requestConfig;
+ delete options.responseConfig;
+
+ // add request options to request returned to calling method
+ clientRequest.options = options;
+
+ var request = protocol.request(options, function(res){
+ //configure response
+ self.configureResponse(res,responseConfig, clientRequest);
+
+ // concurrent data chunk handler
+ res.on('data',function(chunk){
+ buffer.push(new Buffer(chunk));
+ });
+
+ res.on('end',function(){
+ self.handleEnd(res,buffer,callback);
+ });
+
+
+ // handler response errors
+ res.on('error',function(err){
+ if (clientRequest !== undefined && typeof clientRequest === 'object'){
+ // add request as property of error
+ err.request = clientRequest;
+ err.response = res;
+ // request error handler
+ clientRequest.emit('error',err);
+ }else{
+ // general error handler
+ self.emit('error',err);
+ }
+ });
+ });
+
+
+
+ // configure request and add it to clientRequest
+ // and add it to request returned
+ self.configureRequest(request,requestConfig, clientRequest);
+ clientRequest.setHttpRequest(request);
+
+
+ // write POST/PUT data to request body;
+ if(options.data) request.write(self.prepareData(options.data));
+
+
+ // handle request errors and handle them by request or general error handler
+ request.on('error',function(err){
+ if (clientRequest !== undefined && typeof clientRequest === 'object'){
+ // add request as property of error
+ err.request = clientRequest;
+
+ // request error handler
+ clientRequest.emit('error',err);
+ }else{
+ // general error handler
+ self.emit('error',err);
+ }
+ });
+
+
+ request.end();
+ });
+
+ // proxy tunnel error are only handled by general error handler
+ proxyTunnel.on('error',function(e){
+ self.emit('error',e);
+ });
+
+ proxyTunnel.end();
+
+ },
+ this.normal = function(options, callback){
+
+ var buffer = [],
+ protocol = (options.protocol === "http")?http:https,
+ clientRequest = options.clientRequest,
+ requestConfig = options.requestConfig,
+ responseConfig = options.responseConfig,
+ self = this;
+
+ //remove "protocol" and "clientRequest" option from options, cos is not allowed by http/hppts node objects
+ delete options.protocol;
+ delete options.clientRequest;
+ delete options.requestConfig;
+ delete options.responseConfig;
+ debug("options pre connect", options);
+
+ // add request options to request returned to calling method
+ clientRequest.options = options;
+
+ var request = protocol.request(options, function(res){
+ //configure response
+ self.configureResponse(res,responseConfig, clientRequest);
+
+ // concurrent data chunk handler
+ res.on('data',function(chunk){
+ buffer.push(new Buffer(chunk));
+ });
+
+ res.on('end',function(){
+
+ self.handleEnd(res,buffer,callback);
+
+ });
+
+ // handler response errors
+ res.on('error',function(err){
+ if (clientRequest !== undefined && typeof clientRequest === 'object'){
+ // add request as property of error
+ err.request = clientRequest;
+ err.response = res;
+ // request error handler
+ clientRequest.emit('error',err);
+ }else{
+ // general error handler
+ self.emit('error',err);
+ }
+ });
+ });
+
+ // configure request and add it to clientRequest
+ // and add it to request returned
+ self.configureRequest(request,requestConfig, clientRequest);
+ debug("clientRequest",clientRequest);
+
+ clientRequest.setHttpRequest(request);
+
+ // handle request errors and handle them by request or general error handler
+ request.on('error',function(err){
+ debug('request error', clientRequest);
+ if (clientRequest !== undefined && typeof clientRequest === 'object'){
+ // add request as property of error
+ err.request = clientRequest;
+ // request error handler
+ clientRequest.emit('error',err);
+ }else{
+ // general error handler
+ self.emit('error',err);
+ }
+ });
+
+
+ debug("options data", options.data);
+ // write POST/PUT data to request body;
+ if(options.data) request.write(this.prepareData(options.data));
+
+ request.end();
+
+ }
+ };
+
+
+ // event handlers for client and ConnectManager
+ util.inherits(exports.Client, events.EventEmitter);
+ util.inherits(ConnectManager,events.EventEmitter);
+
+
+ var debug = function(){
+ if (!process.env.DEBUG) return;
+
+ var now = new Date(),
+ header =now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " [NRC CLIENT]" + arguments.callee.caller.name + " -> ",
+ args = Array.prototype.slice.call(arguments);
+ args.splice(0,0,header);
+ node_debug.apply(console,args);
+
+
+ };
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..b4a0fc7
--- /dev/null
+++ b/package.json
@@ -0,0 +1,24 @@
+{
+ "author": "Alejandro Alvarez Acero",
+ "name": "node-rest-client",
+ "description": "node API REST client",
+ "version": "2.5.0",
+ "repository": {
+ "type":"git",
+ "url": "https://github.com/aacerox/node-rest-client.git"
+ },
+ "main": "./lib/node-rest-client",
+ "dependencies": {
+ "xml2js":">=0.2.4",
+ "debug": "~2.2.0",
+ "follow-redirects":">=1.2.0"
+ },
+ "devDependencies": {
+ "jasmine-node":">=1.2.3"
+ },
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "license": "MIT"
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..225b757
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,482 @@
+# REST Client for Node.js
+[![npm version](https://badge.fury.io/js/node-rest-client.svg)](https://www.npmjs.com/package/node-rest-client)
+[![Build Status](https://travis-ci.org/olalonde/node-rest-client.svg?branch=master)](https://travis-ci.org/olalonde/node-rest-client)
+
+[![NPM](https://nodei.co/npm/node-rest-client.png?downloads=true)](https://nodei.co/npm/node-rest-client.png?downloads=true)
+
+**NOTE:** _Since version 0.8.0 node does not contain node-waf anymore. The node-zlib package which node-rest-client make use of, depends on node-waf.Fortunately since version 0.8.0 zlib is a core dependency of node, so since version 1.0 of node-rest-client the explicit dependency to "zlib" has been removed from package.json. therefore if you are using a version below 0.8.0 of node please use a versiĆ³n below 1.0.0 of "node-rest-client". _
+
+Allows connecting to any API REST and get results as js Object. The client has the following features:
+
+- Transparent HTTP/HTTPS connection to remote API sites.
+- Allows simple HTTP basic authentication.
+- Allows most common HTTP operations: GET, POST, PUT, DELETE, PATCH.
+- Allows creation of custom HTTP Methods (PURGE, etc.)
+- Direct or through proxy connection to remote API sites.
+- Register remote API operations as client own methods, simplifying reuse.
+- Automatic parsing of XML and JSON response documents as js objects.
+- Dynamic path and query parameters and request headers.
+- Improved Error handling mechanism (client or specific request)
+- Added support for compressed responses: gzip and deflate
+- Added support for follow redirects thanks to great [follow-redirects](https://www.npmjs.com/package/follow-redirects) package
+
+
+## Installation
+
+$ npm install node-rest-client
+
+## Usages
+
+### Simple HTTP GET
+
+Client has 2 ways to call a REST service: direct or using registered methods
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+var client = new Client();
+
+// direct way
+client.get("http://remote.site/rest/xml/method", function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+// registering remote methods
+client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET");
+
+client.methods.jsonMethod(function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+```
+
+### HTTP POST
+
+POST, PUT or PATCH method invocation are configured like GET calls with the difference that you have to set "Content-Type" header in args passed to client method invocation:
+
+```javascript
+//Example POST method invocation
+var Client = require('node-rest-client').Client;
+
+var client = new Client();
+
+// set content-type header and data as json in args parameter
+var args = {
+ data: { test: "hello" },
+ headers: { "Content-Type": "application/json" }
+};
+
+client.post("http://remote.site/rest/xml/method", args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+// registering remote methods
+client.registerMethod("postMethod", "http://remote.site/rest/json/method", "POST");
+
+client.methods.postMethod(args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+```
+If no "Content-Type" header is set as client arg POST,PUT and PATCH methods will not work properly.
+
+
+### Passing args to registered methods
+
+You can pass diferents args to registered methods, simplifying reuse: path replace parameters, query parameters, custom headers
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+// direct way
+var client = new Client();
+
+var args = {
+ data: { test: "hello" }, // data passed to REST method (only useful in POST, PUT or PATCH methods)
+ path: { "id": 120 }, // path substitution var
+ parameters: { arg1: "hello", arg2: "world" }, // query parameter substitution vars
+ headers: { "test-header": "client-api" } // request headers
+};
+
+
+client.get("http://remote.site/rest/json/${id}/method?arg1=hello&arg2=world", args,
+ function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+ });
+
+
+// registering remote methods
+client.registerMethod("jsonMethod", "http://remote.site/rest/json/${id}/method", "GET");
+
+
+/* this would construct the following URL before invocation
+ *
+ * http://remote.site/rest/json/120/method?arg1=hello&arg2=world
+ *
+ */
+client.methods.jsonMethod(args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+```
+
+You can even use path placeholders in query string in direct connection:
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+// direct way
+var client = new Client();
+
+var args = {
+ path: { "id": 120, "arg1": "hello", "arg2": "world" },
+ parameters: { arg1: "hello", arg2: "world" },
+ headers: { "test-header": "client-api" }
+};
+
+client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args,
+ function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+ });
+```
+
+### HTTP POST and PUT methods
+
+To send data to remote site using POST or PUT methods, just add a data attribute to args object:
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+// direct way
+var client = new Client();
+
+var args = {
+ path: { "id": 120 },
+ parameters: { arg1: "hello", arg2: "world" },
+ headers: { "test-header": "client-api" },
+ data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>"
+};
+
+client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+// registering remote methods
+client.registerMethod("xmlMethod", "http://remote.site/rest/xml/${id}/method", "POST");
+
+
+client.methods.xmlMethod(args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+// posted data can be js object
+var args_js = {
+ path: { "id": 120 },
+ parameters: { arg1: "hello", arg2: "world" },
+ headers: { "test-header": "client-api" },
+ data: { "arg1": "hello", "arg2": 123 }
+};
+
+client.methods.xmlMethod(args_js, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+```
+
+### Request/Response configuration
+
+It's also possible to configure each request and response, passing its configuration as an
+additional argument in method call.
+
+```javascript
+var client = new Client();
+
+// request and response additional configuration
+var args = {
+ path: { "id": 120 },
+ parameters: { arg1: "hello", arg2: "world" },
+ headers: { "test-header": "client-api" },
+ data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>",
+ requestConfig: {
+ timeout: 1000, //request timeout in milliseconds
+ noDelay: true, //Enable/disable the Nagle algorithm
+ keepAlive: true, //Enable/disable keep-alive functionalityidle socket.
+ keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent
+ },
+ responseConfig: {
+ timeout: 1000 //response timeout
+ }
+};
+
+
+client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+```
+If you want to handle timeout events both in the request and in the response just add a new "requestTimeout"
+or "responseTimeout" event handler to clientRequest returned by method call.
+
+```javascript
+var client = new Client();
+
+// request and response additional configuration
+var args = {
+ path: { "id": 120 },
+ parameters: { arg1: "hello", arg2: "world" },
+ headers: { "test-header": "client-api" },
+ data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>",
+ requestConfig: {
+ timeout: 1000, //request timeout in milliseconds
+ noDelay: true, //Enable/disable the Nagle algorithm
+ keepAlive: true, //Enable/disable keep-alive functionalityidle socket.
+ keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent
+ },
+ responseConfig: {
+ timeout: 1000 //response timeout
+ }
+};
+
+
+var req = client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+req.on('requestTimeout', function (req) {
+ console.log('request has expired');
+ req.abort();
+});
+
+req.on('responseTimeout', function (res) {
+ console.log('response has expired');
+
+});
+
+//it's usefull to handle request errors to avoid, for example, socket hang up errors on request timeouts
+req.on('error', function (err) {
+ console.log('request error', err);
+});
+```
+### Follows Redirect
+Node REST client follows redirects by default to a maximum of 21 redirects, but it's also possible to change follows redirect default config in each request done by the client
+```javascript
+var client = new Client();
+
+// request and response additional configuration
+var args = {
+ requestConfig: {
+ followRedirects:true,//whether redirects should be followed(default,true)
+ maxRedirects:10//set max redirects allowed (default:21)
+ },
+ responseConfig: {
+ timeout: 1000 //response timeout
+ }
+};
+
+```
+
+### Connect through proxy
+
+Just pass proxy configuration as option to client.
+
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+// configure proxy
+var options_proxy = {
+ proxy: {
+ host: "proxy.foo.com",
+ port: 8080,
+ user: "proxyuser",
+ password: "123",
+ tunnel: true
+ }
+};
+
+var client = new Client(options_proxy);
+```
+
+client has 2 ways to connect to target site through a proxy server: tunnel or direct request, the first one is the default option
+so if you want to use direct request you must set tunnel off.
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+// configure proxy
+var options_proxy = {
+ proxy: {
+ host: "proxy.foo.com",
+ port: 8080,
+ user: "proxyuser",
+ password: "123",
+ tunnel: false // use direct request to proxy
+ }
+};
+
+var client = new Client(options_proxy);
+```
+
+
+
+### Basic HTTP auth
+
+Just pass username and password or just username, if no password is required by remote site, as option to client. Every request done with the client will pass username and password or just username if no password is required as basic authorization header.
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+// configure basic http auth for every request
+var options_auth = { user: "admin", password: "123" };
+
+var client = new Client(options_auth);
+```
+
+### Options parameters
+
+You can pass the following args when creating a new client:
+
+```javascript
+var options = {
+ // proxy configuration
+ proxy: {
+ host: "proxy.foo.com", // proxy host
+ port: 8080, // proxy port
+ user: "ellen", // proxy username if required
+ password: "ripley" // proxy pass if required
+ },
+ // aditional connection options passed to node http.request y https.request methods
+ // (ie: options to connect to IIS with SSL)
+ connection: {
+ secureOptions: constants.SSL_OP_NO_TLSv1_2,
+ ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM',
+ honorCipherOrder: true
+ },
+ // customize mime types for json or xml connections
+ mimetypes: {
+ json: ["application/json", "application/json;charset=utf-8"],
+ xml: ["application/xml", "application/xml;charset=utf-8"]
+ },
+ user: "admin", // basic http auth username if required
+ password: "123", // basic http auth password if required
+ requestConfig: {
+ timeout: 1000, //request timeout in milliseconds
+ noDelay: true, //Enable/disable the Nagle algorithm
+ keepAlive: true, //Enable/disable keep-alive functionalityidle socket.
+ keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent
+ },
+ responseConfig: {
+ timeout: 1000 //response timeout
+ }
+};
+```
+Note that requestConfig and responseConfig options if set on client instantiation apply to all of its requests/responses
+and is only overriden by request or reponse configs passed as args in method calls.
+
+
+### Managing Requests
+
+Each REST method invocation returns a request object with specific request options and error, requestTimeout and responseTimeout event handlers.
+
+```javascript
+var Client = require('node-rest-client').Client;
+
+var client = new Client();
+
+var args = {
+ requesConfig: { timeout: 1000 },
+ responseConfig: { timeout: 2000 }
+};
+
+// direct way
+var req1 = client.get("http://remote.site/rest/xml/method", args, function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+// view req1 options
+console.log(req1.options);
+
+
+req1.on('requestTimeout', function (req) {
+ console.log("request has expired");
+ req.abort();
+});
+
+req1.on('responseTimeout', function (res) {
+ console.log("response has expired");
+
+});
+
+
+// registering remote methods
+client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET");
+
+var req2 = client.methods.jsonMethod(function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+});
+
+// handling specific req2 errors
+req2.on('error', function (err) {
+ console.log('something went wrong on req2!!', err.request.options);
+});
+```
+
+### Error Handling
+
+ Now you can handle error events in two places: on client or on each request.
+
+```javascript
+var client = new Client(options_auth);
+
+// handling request error events
+client.get("http://remote.site/rest/xml/method", function (data, response) {
+ // parsed response body as js object
+ console.log(data);
+ // raw response
+ console.log(response);
+}).on('error', function (err) {
+ console.log('something went wrong on the request', err.request.options);
+});
+
+// handling client error events
+client.on('error', function (err) {
+ console.error('Something went wrong on the client', err);
+});
+```
diff --git a/test/local-test.js b/test/local-test.js
new file mode 100644
index 0000000..82d847d
--- /dev/null
+++ b/test/local-test.js
@@ -0,0 +1,12 @@
+ var Client = require("../lib/node-rest-client.js").Client;
+ var client = new Client();
+
+ console.log("client es ", client);
+/*
+ client.get("http://localhost:8080/xml",(data,response) => console.log("DATA IS ", data));
+*/
+ client.addCustomHttpMethod("purge");
+
+ var request = client.purge("http://localhost:8080/xml",(data,response) => console.log("DATA IS ", data) );
+
+ console.log("REQUEST is ", request);
diff --git a/test/message.json b/test/message.json
new file mode 100644
index 0000000..38165f6
--- /dev/null
+++ b/test/message.json
@@ -0,0 +1,34 @@
+{"catalog":{
+ "books":[{
+ "id":"bk101",
+ "author":"Gambardella, Matthew",
+ "title":"XML Developer's Guide",
+ "genre":"Computer",
+ "price":44.95,
+ "publish_date":"2000-10-10",
+ "description":"An in-depth look at creating applications with XML."
+ },
+ {
+ "id":"bk102",
+ "author":"Gambardella, Matthew",
+ "title":"JAVA Developer's Guide",
+ "genre":"Computer",
+ "price":188.95,
+ "publish_date":"2000-10-10",
+ "description":"An in-depth look at creating applications with JAVA."
+ },
+ {
+ "id":"bk103",
+ "author":"Gambardella, Matthew",
+ "title":"JSON Developer's Guide",
+ "genre":"Computer",
+ "price":422.95,
+ "publish_date":"2000-10-10",
+ "description":"An in-depth look at creating applications with JSON."
+ }
+ ]
+ }
+ }
+
+
+ \ No newline at end of file
diff --git a/test/message.xml b/test/message.xml
new file mode 100644
index 0000000..2a6e6c5
--- /dev/null
+++ b/test/message.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0"?>
+<catalog>
+ <book id="bk101">
+ <author>Gambardella, Matthew</author>
+ <title>XML Developer's Guide</title>
+ <genre>Computer</genre>
+ <price>44.95</price>
+ <publish_date>2000-10-01</publish_date>
+ <description>An in-depth look at creating applications
+ with XML.</description>
+ </book>
+ <book id="bk102">
+ <author>Ralls, Kim</author>
+ <title>Midnight Rain</title>
+ <genre>Fantasy</genre>
+ <price>5.95</price>
+ <publish_date>2000-12-16</publish_date>
+ <description>A former architect battles corporate zombies,
+ an evil sorceress, and her own childhood to become queen
+ of the world.</description>
+ </book>
+ <book id="bk103">
+ <author>Corets, Eva</author>
+ <title>Maeve Ascendant</title>
+ <genre>Fantasy</genre>
+ <price>5.95</price>
+ <publish_date>2000-11-17</publish_date>
+ <description>After the collapse of a nanotechnology
+ society in England, the young survivors lay the
+ foundation for a new society.</description>
+ </book>
+ <book id="bk104">
+ <author>Corets, Eva</author>
+ <title>Oberon's Legacy</title>
+ <genre>Fantasy</genre>
+ <price>5.95</price>
+ <publish_date>2001-03-10</publish_date>
+ <description>In post-apocalypse England, the mysterious
+ agent known only as Oberon helps to create a new life
+ for the inhabitants of London. Sequel to Maeve
+ Ascendant.</description>
+ </book>
+ </catalog>
diff --git a/test/specs.js b/test/specs.js
new file mode 100644
index 0000000..5777eb9
--- /dev/null
+++ b/test/specs.js
@@ -0,0 +1,32 @@
+var jasmine = require('jasmine-node');
+var sys = require('sys');
+
+for(var key in jasmine) {
+ global[key] = jasmine[key];
+}
+
+var isVerbose = true;
+var showColors = true;
+
+process.argv.forEach(function(arg){
+ switch(arg) {
+ case '--color': showColors = true; break;
+ case '--noColor': showColors = false; break;
+ case '--verbose': isVerbose = true; break;
+ }
+});
+
+var options = {"specFolder":__dirname.concat("\\spec"),
+ "onComplete": function(runner, log){
+ if (runner.results().failedCount == 0) {
+ process.exit(0);
+ }
+ else {
+ process.exit(1);
+ }
+ },
+ "isVerbose": isVerbose,
+ "showColors": showColors};
+
+jasmine.executeSpecsInFolder(options);
+
diff --git a/test/test-proxy.js b/test/test-proxy.js
new file mode 100644
index 0000000..9211ca9
--- /dev/null
+++ b/test/test-proxy.js
@@ -0,0 +1,85 @@
+var http = require('http');
+var sys = require('sys');
+var fs = require('fs');
+
+var blacklist = [];
+var iplist = [];
+
+fs.watchFile('./blacklist', function(c,p) { update_blacklist(); });
+fs.watchFile('./iplist', function(c,p) { update_iplist(); });
+
+function update_blacklist() {
+ sys.log("Updating blacklist.");
+ blacklist = fs.readFileSync('./blacklist').split('\n')
+ .filter(function(rx) { return rx.length })
+ .map(function(rx) { return RegExp(rx) });
+}
+
+function update_iplist() {
+ sys.log("Updating iplist.");
+ iplist = fs.readFileSync('./iplist').split('\n')
+ .filter(function(rx) { return rx.length });
+}
+
+function ip_allowed(ip) {
+ for (i in iplist) {
+ if (iplist[i] == ip) {
+ return true;
+ }
+ }
+ return false;
+}
+
+function host_allowed(host) {
+ for (i in blacklist) {
+ if (blacklist[i].test(host)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+function deny(response, msg) {
+ response.writeHead(401);
+ response.write(msg);
+ response.end();
+}
+
+http.createServer(function(request, response) {
+ var ip = request.connection.remoteAddress;
+ if (!ip_allowed(ip)) {
+ msg = "IP " + ip + " is not allowed to use this proxy";
+ deny(response, msg);
+ sys.log(msg);
+ return;
+ }
+
+ if (!host_allowed(request.url)) {
+ msg = "Host " + request.url + " has been denied by proxy configuration";
+ deny(response, msg);
+ sys.log(msg);
+ return;
+ }
+
+ sys.log(ip + ": " + request.method + " " + request.url);
+ var proxy = http.createClient(80, request.headers['host']);
+ var proxy_request = proxy.request(request.method, request.url, request.headers);
+ proxy_request.addListener('response', function(proxy_response) {
+ proxy_response.addListener('data', function(chunk) {
+ response.write(chunk, 'binary');
+ });
+ proxy_response.addListener('end', function() {
+ response.end();
+ });
+ response.writeHead(proxy_response.statusCode, proxy_response.headers);
+ });
+ request.addListener('data', function(chunk) {
+ proxy_request.write(chunk, 'binary');
+ });
+ request.addListener('end', function() {
+ proxy_request.end();
+ });
+}).listen(8080);
+
+update_blacklist();
+update_iplist(); \ No newline at end of file
diff --git a/test/test-servers.js b/test/test-servers.js
new file mode 100644
index 0000000..c840181
--- /dev/null
+++ b/test/test-servers.js
@@ -0,0 +1,81 @@
+var http = require('http'),
+ fs = require('fs');
+
+// Create an HTTP server
+var httpSrv = http.createServer(function (req, res) {
+ console.log("req.url", req.url);
+ RouteManager.findRoute(req,res);
+});
+
+var RouteManager ={
+ "findRoute":function(req,res){
+ var handler = this.routes[req.url];
+ if (!handler) throw "cannot find route " + req.url;
+ handler.call(this,req,res);
+ },
+ "routes":{
+ "/json":function(req,res){
+ //this.sleep(5000);
+ var message = fs.readFileSync('./message.json','utf8');
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ res.write(message.toString());
+ res.end();
+ },
+ "/xml":function(req,res){
+ var message = fs.readFileSync('./message.xml','utf8');
+ res.writeHead(200, {'Content-Type': 'application/xml'});
+ res.write(message.toString());
+ res.end();
+ },
+ "/120/json?arg1=hello&arg2=world":function(req,res){
+ if (!req.headers["test-header"]) throw "no test-header found!!";
+ res.setHeader("test-response-header",req.headers["test-header"]);
+ this.routes["/json"](req,res);
+ },
+ "/json?post":function(req,res){
+ req.on('data',function(data){
+ console.log("[SERVER] data = ", data);
+ res.writeHead(200, {'Content-Type': 'application/json'});
+ //res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write(data.toString());
+ res.end();
+ });
+
+ },
+ "/json/empty":function(req,res){
+ res.writeHead(204, {'Content-Type': 'application/json'});
+ res.end();
+ },
+ "/xml/empty":function(req,res){
+ res.writeHead(204, {'Content-Type': 'application/xml'});
+ res.end();
+ },
+ "/json/contenttypewithspace":function(req,res){
+ var message = fs.readFileSync('./message.json','utf8');
+ res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
+ res.write(message.toString());
+ res.end();
+ }
+ },
+ "sleep":function(ms){
+
+ var stop = new Date().getTime();
+ while(new Date().getTime() < stop + ms) {
+ ;
+ }
+ }
+
+};
+
+
+
+
+
+
+httpSrv.on('error',function(err){
+ console.error('error starting http test server',err);
+});
+
+httpSrv.listen(4444);
+
+console.log('http server Listening on port ' + 4444);