diff --git a/.gitignore b/.gitignore index a53b36e..a4db2ae 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,8 @@ node_modules test/multiple-clients-test.js test/manual-test.js + +.DS_Store + +.project +mocha-eclipse.js \ No newline at end of file diff --git a/debug.bat b/debug.bat new file mode 100644 index 0000000..248436f --- /dev/null +++ b/debug.bat @@ -0,0 +1,2 @@ +start node-inspector +start mocha --debug-brk -R spec %1% \ No newline at end of file diff --git a/debug.sh b/debug.sh new file mode 100644 index 0000000..1026750 --- /dev/null +++ b/debug.sh @@ -0,0 +1,36 @@ +#!/bin/sh +#set -x +# find existing web inspector + + +killprocess(){ + + echo ps -ef | awk "!/awk/ && /$1/ {print $2}" + #pid=`ps -ef | awk -v keyword=$1 "!/awk/ && /${keyword}/ {print $2}"`; + pid=`ps -ef | awk -v a=node-inspector '!/awk/ && /${a}/ {print $2}'`; + echo current $1 process is $pid; + + if [ -n '$pid' ] + then + echo killing $1 process $pid; + #kill $pid; + + else + echo $1 is not active; + + fi +} + + + + +killprocess node-inspector +killprocess mocha + +echo launching node-inspector +node-inspector & + +echo launching test $1 +mocha --debug-brk -R spec $1 & + + diff --git a/lib/node-rest-client.js b/lib/node-rest-client.js index dc6893d..e913bf8 100644 --- a/lib/node-rest-client.js +++ b/lib/node-rest-client.js @@ -1,130 +1,193 @@ 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"); +https = require('follow-redirects').https, +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, + var self = this, + // parser response manager + parserManager = require("./nrc-parser-manager")(), + serializerManager = require("./nrc-serializer-manager")(), + // connection manager + connectManager = new ConnectManager(this, parserManager, serializerManager), + // io facade to parsers and serailiazers + ioFacade = function(parserManager, serializerManager){ + // error execution context + var errorContext = function(logic){ + return function(){ + try{ + return logic.apply(this, arguments); + }catch(err){ + self.emit('error',err); + } + }; + }, + result={"parsers":{}, "serializers":{}}; + + // parsers facade + result.parsers.add = errorContext(parserManager.add); + result.parsers.remove = errorContext(parserManager.remove); + result.parsers.find = errorContext(parserManager.find); + result.parsers.getAll = errorContext(parserManager.getAll); + result.parsers.getDefault = errorContext(parserManager.getDefault); + result.parsers.clean = errorContext(parserManager.clean); + + // serializers facade + result.serializers.add = errorContext(serializerManager.add); + result.serializers.remove = errorContext(serializerManager.remove); + result.serializers.find = errorContext(serializerManager.find); + result.serializers.getAll = errorContext(serializerManager.getAll); + result.serializers.getDefault = errorContext(serializerManager.getDefault); + result.serializers.clean = errorContext(serializerManager.clean); + + return result; + + }(parserManager,serializerManager), + // declare util constants + 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 - }; - } + 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 || {}; + + // namespaces for methods, parsers y serializers + this.methods={}; + this.parsers={}; + this.serializers={}; + + // 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]; + 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]; - } + for (var proxyOption in result.proxy){ + result[proxyOption] = result.proxy[proxyOption]; + } - delete result.proxy; + delete result.proxy; } // add general request and response config to connect options @@ -133,525 +196,532 @@ exports.Client = function (options){ 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 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); + 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 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 + ']'; + }, + connect : function(method, url, args, callback, clientRequest){ + // check args type if we use it + if (callback && args && typeof args !== 'object')self.emit('error','args should be and object'); + + // 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 adding query + // parameter separation char + + // check if URL already has '?' path parameter separator + // char in any position that is not final + // if true throw error + var pathLength = options.path.length, + pathParameterSepCharPos = options.path.indexOf("?"); + + if (pathParameterSepCharPos >= 0 && pathParameterSepCharPos!== pathLength -1 ) + self.emit('error','parameters argument cannot be used if parameters are already defined in URL ' + options.path); + + options.path +=(options.path.charAt(pathLength-1) === '?'?"":"?"); + // check if we have serializable parameter container, that + // must be serialized and encoded + // directly, as javascript object + options.path = options.path.concat(Util.serializeEncodeQueryFromArgs(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){ + // this function is left for backward compatibility, but will be + // deleted in future releases + var parser = null; + // merge mime-types passed as options to parsers + if (mimetypes && typeof mimetypes === "object"){ + try{ + if (mimetypes.json && mimetypes.json instanceof Array && mimetypes.json.length > 0){ + parser = parserManager.find("JSON"); + parser.contentTypes = mimetypes.json; + }else if (mimetypes.xml && mimetypes.xml instanceof Array && mimetypes.xml.length > 0){ + parser = parserManager.find("XML"); + parser.contentTypes = mimetypes.xml; + } + }catch(err){ + self.emit('error', 'cannot assign custom content types to parser, cause: ' + err); + } + } + }, + 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 = {}; + } + + 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); + }; + + this.parsers = ioFacade.parsers; + + this.serializers = ioFacade.serializers; + + // merge mime types with connect manager + Util.mergeMimeTypes(self.mimetypes); + debug("ConnectManager", connectManager); + +}; + + +var ConnectManager = function(client, parserManager, serializerManager) { + + var client = client, + //wrapper for emit function on client + clientEmitterWrapper = function (client){ + var client = client; + return function(type, event){client.emit(type, event);}; + }; + + 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){ + // find valid parser to be used with response content type, first one + // found + parserManager.get(res).parse(data, clientEmitterWrapper(client), function(parsedData){ + callback(parsedData,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 + client.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; + // find valid serializer to be used to serialize request data, + // first one found + // is the one to be used.if none found for match condition, + // default serializer is used + + if(options.data){ + serializerManager.get(options).serialize(options.data, clientEmitterWrapper(client), function(serializedData){ + request.write(serializedData); + request.end(); // end request when data is written + }); + }else{ + // no data to be serialized end request + request.end(); + } + + // 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 + client.emit('error',err); + } + }); + }); + + // proxy tunnel error are only handled by general error handler + proxyTunnel.on('error',function(e){ + client.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 + client.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); + + debug("options data", options.data); + // write POST/PUT data to request body; + // find valid serializer to be used to serialize request data, + // first one found + // is the one to be used.if none found for match condition, + // default serializer is used + if(options.data){ + serializerManager.get(options).serialize(options.data, clientEmitterWrapper(client), function(serializedData){ + request.write(serializedData); + request.end(); // end request when data is written + }); + }else{ + // no data to be serialized end request + request.end(); } - 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); - - - }; + + // 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 + client.emit('error',err); + } + }); + }; + }; + + + // event handlers for client and ConnectManager + util.inherits(exports.Client, 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/lib/nrc-parser-manager.js b/lib/nrc-parser-manager.js new file mode 100644 index 0000000..5ff5f96 --- /dev/null +++ b/lib/nrc-parser-manager.js @@ -0,0 +1,155 @@ +var ParserManager = function(){ + var registry={}, defaultParser = null; + + var _private={ + "validate":function(parser){ + + function validateProperties(parser, props){ + var result = true; + for (var propIndex in props){ + var propType = props[propIndex].split(":"); + if (!parser.hasOwnProperty([propType[0]]) || typeof parser[propType[0]] !== propType[1]){ + result = false; + break; + } + } + + return result; + } + + + result = validateProperties(parser,["name:string","parse:function","isDefault:boolean"]); + + // valid parser, check if its not default response parser, to validate non + // default parser props + if (result && !parser.isDefault) + result = validateProperties(parser,["match:function"]); + + + return result; + } + }; + + this.add = function(parser){ + if (!_private.validate(parser)) + throw "parser cannot be added: invalid parser definition"; + + if (parser.isDefault){ + defaultParser = parser; + }else{ + registry[parser.name] = parser; + } + }; + + this.remove = function(parserName){ + var result = registry[parserName]; + if (!result) + throw "cannot remove parser: " + parserName +" doesn't exists"; + + delete registry[parserName]; + }; + + this.clean = function(){ + registry={}; + }; + + this.find = function(parserName){ + var result = registry[parserName]; + if (!result) + throw "cannot find parser: " + parserName + " doesn't exists "; + + return result; + }; + + this.getDefault = function(){ + return defaultParser; + }; + + this.get = function(response){ + var result = null; + for (var parserName in registry){ + if (registry[parserName].match(response)){ + result = registry[parserName]; + break; + } + } + // if parser not found return default parser, else parser found + return (result === null)?defaultParser:result; + }; + + this.getAll=function(){ + var result=[]; + for (var parserName in registry){ + result.push(registry[parserName]); + } + return result; + } +}; + + +module.exports = function(){ + +var parserManager = new ParserManager(); + +var BaseParser = { + "isDefault":false, + "match":function(response){ + var result = false, + contentType = response.headers["content-type"] && response.headers["content-type"].replace(/ /g, ''); + + if (!contentType) return result; + + for (var i=0; i 0); + }, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ + var jsonData, + data = byteBuffer.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. + nrcEventEmitter('error','Error parsing response. response: [' +data + '], error: [' + err + ']'); + } + parsedCallback(jsonData); + } + },BaseParser)); + + +parserManager.add({ + "name":"DEFAULT", + "isDefault":true, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ + parsedCallback(byteBuffer); + } +}); + +return parserManager; +} diff --git a/lib/nrc-serializer-manager.js b/lib/nrc-serializer-manager.js new file mode 100644 index 0000000..941b685 --- /dev/null +++ b/lib/nrc-serializer-manager.js @@ -0,0 +1,184 @@ +var xmlserializer = require('xml2js'); + +var SerializerManager = function(){ + var registry={}, defaultSerializer = null; + + var _private={ + "validate":function(serializer){ + + function validateProperties(serializer, props){ + var result = true; + for (var propIndex in props){ + var propType = props[propIndex].split(":"); + if (!serializer.hasOwnProperty([propType[0]]) || typeof serializer[propType[0]] !== propType[1]){ + result = false; + break; + } + } + + return result; + } + + + result = validateProperties(serializer,["name:string","serialize:function","isDefault:boolean"]); + + // valid serializer, check if its not default request serializer, to validate non + // default serializer props + if (result && !serializer.isDefault) + result = validateProperties(serializer,["match:function"]); + + + return result; + } + }; + + this.add = function(serializer){ + if (!_private.validate(serializer)) + throw "serializer cannot be added: invalid serializer definition"; + + if (serializer.isDefault){ + defaultSerializer = serializer; + }else{ + registry[serializer.name] = serializer; + } + }; + + this.remove = function(serializerName){ + var result = registry[serializerName]; + if (!result) + throw "cannot remove serializer: " + serializerName +" doesn't exists"; + + delete registry[serializerName]; + }; + + this.find = function(serializerName){ + var result = registry[serializerName]; + if (!result) + throw "cannot find serializer: " + serializerName +" doesn't exists"; + + return result; + }; + + + this.clean = function(){ + registry={}; + }; + + this.get = function(request){ + var result = null; + for (var serializerName in registry){ + if (registry[serializerName].match(request)){ + result = registry[serializerName]; + break; + } + } + // if serializer not found return default serializer, else serializer found + return (result === null)?defaultSerializer:result; + }; + + this.getAll=function(){ + var result = []; + for (var serializerName in registry){ + result.push(registry[serializerName]); + } + return result; + }; + + this.getDefault = function(){ + return defaultSerializer; + }; +}; + + + + +module.exports = function(){ + +var serializerManager = new SerializerManager(); + +var BaseSerializer ={ + "isDefault":false, + "match":function(request){ + var result = false, + contentType = request.headers["content-type"] && request.headers["content-type"].replace(/ /g, ''); + + if (!contentType) return result; + + for (var i=0; i=1.2.0" }, "devDependencies": { - "jasmine-node":">=1.2.3" + "mocha": "*", + "should": ">= 0.0.1" }, "optionalDependencies": {}, "engines": { "node": "*" }, + "scripts": { + "test": "mocha" + }, "license": "MIT" } diff --git a/readme.md b/readme.md index 983262d..8de6349 100644 --- a/readme.md +++ b/readme.md @@ -4,21 +4,23 @@ [![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". _ +## Features 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 most common HTTP operations: GET, POST, PUT, DELETE, PATCH or any other method through custom connect method - 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. +- Register remote API operations as own client methods, simplifying reuse. - 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 +- Added support for custom request serializers (json,xml and url-encoded included by default) +- Added support for custom response parsers (json and xml included by default) + ## Installation @@ -29,7 +31,7 @@ $ npm install node-rest-client ### Simple HTTP GET -Client has 2 ways to call a REST service: direct or using registered methods +Client has two ways to call a REST service: direct or using registered methods ```javascript var Client = require('node-rest-client').Client; @@ -104,12 +106,12 @@ 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 + parameters: { arg1: "hello", arg2: "world" }, // this is serialized as URL parameters headers: { "test-header": "client-api" } // request headers }; -client.get("http://remote.site/rest/json/${id}/method?arg1=hello&arg2=world", args, +client.get("http://remote.site/rest/json/${id}/method", args, function (data, response) { // parsed response body as js object console.log(data); @@ -144,8 +146,7 @@ var Client = require('node-rest-client').Client; var client = new Client(); var args = { - path: { "id": 120, "arg1": "hello", "arg2": "world" }, - parameters: { arg1: "hello", arg2: "world" }, + path: { "id": 120, "arg1": "hello", "arg2": "world" }, headers: { "test-header": "client-api" } }; @@ -175,7 +176,7 @@ var args = { data: "helloworld" }; -client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { +client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) { // parsed response body as js object console.log(data); // raw response @@ -235,7 +236,7 @@ var args = { }; -client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { +client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) { // parsed response body as js object console.log(data); // raw response @@ -266,7 +267,7 @@ var args = { }; -var req = client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { +var req = client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) { // parsed response body as js object console.log(data); // raw response @@ -304,6 +305,338 @@ var args = { } }; +``` + + +### Response Parsers + +You can add your own response parsers to client, as many as you want. There are 2 parser types: + +- _**Regular parser**_: First ones to analyze responses. When a response arrives it will pass through all regular parsers, first parser whose `match` method return true will be the one to process the response. there can be as many regular parsers as you need. you can delete and replace regular parsers when it'll be needed. + +- _**Default parser**_: When no regular parser has been able to process the response, default parser will process it, so it's guaranteed that every response is processed. There can be only one default parser and cannot be deleted but it can be replaced adding a parser with `isDefault` attribute to true. + +Each parser - regular or default- needs to follow some conventions: + +* Must be and object + +* Must have the following attributes: + + * `name`: Used to identify parser in parsers registry + + * `isDefault`: Used to identify parser as regular parser or default parser. Default parser is applied when client cannot find any regular parser that match to received response + +* Must have the following methods: + + * `match(response)`: used to find which parser should be used with a response. First parser found will be the one to be used. Its arguments are: + 1. `response`:`http.ServerResponse`: you can use any argument available in node ServerResponse, for example `headers` + + * `parse(byteBuffer,nrcEventEmitter,parsedCallback)` : this method is where response body should be parsed and passed to client request callback. Its arguments are: + 1. `byteBuffer`:`Buffer`: Raw response body that should be parsed as js object or whatever you need + 2. `nrcEventEmitter`:`client event emitter`: useful to dispatch events during parsing process, for example error events + 3. `parsedCallback`:`function(parsedData)`: this callback should be invoked when parsing process has finished to pass parsed data to request callback. + +Of course any other method or attribute needed for parsing process can be added to parser. + +```javascript +// no "isDefault" attribute defined +var invalid = { + "name":"invalid-parser", + "match":function(response){...}, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){...} + }; + +var validParser = { + "name":"valid-parser", + "isDefault": false, + "match":function(response){...}, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){...}, + // of course any other args or methods can be added to parser + "otherAttr":"my value", + "otherMethod":function(a,b,c){...} + }; + +function OtherParser(name){ + this.name: name, + this.isDefault: false, + this.match=function(response){...}; + this.parse:function(byteBuffer,nrcEventEmitter,parsedCallback){...}; + +} + +var instanceParser = new OtherParser("instance-parser"); + +//valid parser complete example + +client.parsers.add({ + "name":"valid-parser", + "isDefault":false, + "match":function(response){ + // only match to responses with a test-header equal to "hello world!" + return response.headers["test-header"]==="hello world!"; + }, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ + // parsing process + var parsedData = null; + try{ + parsedData = JSON.parse(byteBuffer.toString()); + parsedData.parsed = true; + + // emit custom event + nrcEventEmitter('parsed','data has been parsed ' + parsedData); + + // pass parsed data to client request method callback + parsedCallback(parsedData); + }catch(err){ + nrcEmitter('error',err); + }; + + }); + +``` + +By default and to maintain backward compatibility, client comes with 2 regular parsers and 1 default parser: + +- _**JSON parser**_: it's named 'JSON' in parsers registry and processes responses to js object. As in previous versions you can change content-types used to match responses by adding a "mimetypes" attribute to client options. + +```javascript +var options = { + mimetypes: { + json: ["application/json", "application/my-custom-content-type-for-json;charset=utf-8"] + + } + }; + +var client = new Client(options); + +``` + +- _**XML parser**_: it's named 'XML' in parsers registry and processes responses returned as XML documents to js object. As in previous versions you can change content-types used to match responses by adding a "mimetypes" attribute to client options. + +```javascript +var options = { + mimetypes: { + xml: ["application/xml", "application/my-custom-content-type-for-xml"] + } + }; + +var client = new Client(options); + +``` + +- _**Default Parser**_: return responses as is, without any adittional processing. + +#### Parser Management + +Client can manage parsers through the following parsers namespace methods: + +* `add(parser)`: add a regular or default parser (depending on isDefault attribute value) to parsers registry. If you add a regular parser with the same name as an existing one, it will be overwritten + + 1. `parser`: valid parser object. If invalid parser is added an 'error' event is dispatched by client. + +* `remove(parserName)`: removes a parser from parsers registry. If not parser found an 'error' event is dispatched by client. + + 1. `parserName`: valid parser name previously added. + +* `find(parserName)`: find and return a parser searched by its name. If not parser found an 'error' event is dispatched by client. + + 1. `parserName`: valid parser name previously added. + +* `getAll()`: return a collection of current regular parsers. + +* `getDefault()`: return the default parser used to process responses that doesn't match with any regular parser. + +* `clean()`: clean regular parser registry. default parser is not afected by this method. + +```javascript +var client = new Client(); + +client.parsers.add({ + "name":"valid-parser", + "isDefault": false, + "match":function(response){...}, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){...}, + // of course any other args or methods can be added to parser + "otherAttr":"my value", + "otherMethod":function(a,b,c){...} + }); + +var parser = client.parsers.find("valid-parser"); + +var defaultParser = client.parsers.getDefault(); + +var regularParsers = client.parsers.getAll(); + +client.parsers.clean(); + + +``` + + +### Request Serializers + +You can add your own request serializers to client, as many as you want. There are 2 serializer types: + +- _**Regular serializer**_: First ones to analyze requests. When a request is sent it will pass through all regular serializers, first serializer whose `match` method return true will be the one to process the request. there can be as many regular serializers as you need. you can delete and replace regular serializers when it'll be needed. + +- _**Default serializer**_: When no regular serializer has been able to process the request, default serializer will process it, so it's guaranteed that every request is processed. There can be only one default serializer and cannot be deleted but it can be replaced adding a serializer with `isDefault` attribute to true. + +Each serializer - regular or default- needs to follow some conventions: + +* Must be and object + +* Must have the following attributes: + + * `name`: Used to identify serializer in serializers registry + + * `isDefault`: Used to identify serializer as regular serializer or default serializer. Default serializer is applied when client cannot find any regular serializer that match to sent request + +* Must have the following methods: + + * `match(request)`: used to find which serializer should be used with a request. First serializer found will be the one to be used. Its arguments are: + 1. `request`:`options passed to http.ClientRequest`: any option passed to a request through client options or request args, for example `headers` + + * `serialize(data,nrcEventEmitter,serializedCallback)` : this method is where request body should be serialized before passing to client request callback. Its arguments are: + 1. `data`:`args data attribute`: Raw request body as is declared in args request attribute that should be serialized. + + 2. `nrcEventEmitter`:`client event emitter`: useful to dispatch events during serialization process, for example error events + + 3. `serializedCallback`:`function(serializedData)`: this callback should be invoked when serialization process has finished to pass serialized data to request callback. + +Of course any other method or attribute needed for serialization process can be added to serializer. + +```javascript +// no "isDefault" attribute defined +var invalid = { + "name":"invalid-serializer", + "match":function(request){...}, + "serialize":function(data,nrcEventEmitter,serializedCallback){...} + }; + +var validserializer = { + "name":"valid-serializer", + "isDefault": false, + "match":function(request){...}, + "serialize":function(data,nrcEventEmitter,serializedCallback){...}, + // of course any other args or methods can be added to serializer + "otherAttr":"my value", + "otherMethod":function(a,b,c){...} + }; + +function OtherSerializer(name){ + this.name: name, + this.isDefault: false, + this.match=function(request){...}; + this.serialize:function(data,nrcEventEmitter,serializedCallback){...}; + +} + +var instanceserializer = new OtherSerializer("instance-serializer"); + +// valid serializer complete example + +client.serializers.add({ + "name":"example-serializer", + "isDefault":false, + "match":function(request){ + // only match to requests with a test-header equal to "hello world!" + return request.headers["test-header"]==="hello world!"; + }, + "serialize":function(data,nrcEventEmitter,serializedCallback){ + // serialization process + var serializedData = null; + + if (typeof data === 'string'){ + serializedData = data.concat(" I'm serialized!!"); + }else if (typeof data === 'object'){ + serializedData = data; + serializedData.state = "serialized" + serializedData = JSON.stringify(serializedData); + } + + nrcEventEmitter('serialized','data has been serialized ' + serializedData); + // pass serialized data to client to be sent to remote API + serializedCallback(serializedData); + + } + +}) + + +``` + +By default client comes with 3 regular serializers and 1 default serializer: + +- _**JSON serializer**_: it's named 'JSON' in serializers registry and serialize js objects to its JSON string representation. It will match any request sent **exactly** with the following content types: "application/json","application/json;charset=utf-8" + + +- _**XML serializer**_: it's named 'XML' in serializers registry and serialize js objects to its XML string representation. It will match any request sent **exactly** with the following content types: "application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8" + +- _**URL ENCODE serializer**_: it's named 'FORM-ENCODED' in serializers registry and serialize js objects to its FORM ENCODED string representation. It will match any request sent **exactly** with the following content types: "application/x-www-form-urlencoded","multipart/form-data","text/plain" + + +- _**Default serializer**_: serialize request to its string representation, applying toString() method to data parameter. + +#### serializer Management + +Client can manage serializers through the following serializers namespace methods: + +* `add(serializer)`: add a regular or default serializer (depending on isDefault attribute value) to serializers registry.If you add a regular serializer with the same name as an existing one, it will be overwritten + + 1. `serializer`: valid serializer object. If invalid serializer is added an 'error' event is dispatched by client. + +* `remove(serializerName)`: removes a serializer from serializers registry. If not serializer found an 'error' event is dispatched by client. + + 1. `serializerName`: valid serializer name previously added. + +* `find(serializerName)`: find and return a serializer searched by its name. If not serializer found an 'error' event is dispatched by client. + + 1. `serializerName`: valid serializer name previously added. + +* `getAll()`: return a collection of current regular serializers. + +* `getDefault()`: return the default serializer used to process requests that doesn't match with any regular serializer. + +* `clean()`: clean regular serializer registry. default serializer is not afected by this method. + + +```javascript +var client = new Client(); + +client.serializers.add({ + "name":"valid-serializer", + "isDefault":false, + "match":function(request){ + // only match to requests with a test-header equal to "hello world!" + return request.headers["test-header"]==="hello world!"; + }, + "serialize":function(data,nrcEventEmitter,serializedCallback){ + // serialization process + var serializedData = null; + + if (typeof data === 'string'){ + serializedData = data.concat(" I'm serialized!!"); + }else if (typeof data === 'object'){ + serializedData = data; + serializedData.state = "serialized" + serializedData = JSON.stringify(serializedData); + } + + nrcEventEmitter('serialized','data has been serialized ' + serializedData); + // pass serialized data to client to be sent to remote API + serializedCallback(serializedData); + + }); + +var serializer = client.serializers.find("valid-serializer"); + +var defaultParser = client.serializers.getDefault(); + +var regularSerializers = client.serializers.getAll(); + +client.serializers.clean(); + + ``` ### Connect through proxy @@ -383,7 +716,7 @@ var options = { 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 + // will replace content-types used to match responses in JSON and XML parsers mimetypes: { json: ["application/json", "application/json;charset=utf-8"], xml: ["application/xml", "application/xml;charset=utf-8"] @@ -480,3 +813,6 @@ client.on('error', function (err) { console.error('Something went wrong on the client', err); }); ``` + +**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". _ + diff --git a/test.bat b/test.bat new file mode 100644 index 0000000..18de984 --- /dev/null +++ b/test.bat @@ -0,0 +1 @@ +npm test \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..a593fa1 --- /dev/null +++ b/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh +npm test \ No newline at end of file diff --git a/test/local-test.js b/test/local-test.js deleted file mode 100644 index 2cd708c..0000000 --- a/test/local-test.js +++ /dev/null @@ -1,12 +0,0 @@ - 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/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..e7ac864 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,4 @@ +--require should +--reporter spec test/specs/*.js +--ui bdd +--recursive \ No newline at end of file diff --git a/test/message.json b/test/server/message.json similarity index 100% rename from test/message.json rename to test/server/message.json diff --git a/test/message.xml b/test/server/message.xml similarity index 100% rename from test/message.xml rename to test/server/message.xml diff --git a/test/server/mock-server.js b/test/server/mock-server.js new file mode 100644 index 0000000..09f33e0 --- /dev/null +++ b/test/server/mock-server.js @@ -0,0 +1,176 @@ +var http = require('http'), fs = require('fs'); + +var RouterOptions = { + "baseMessageDir" : "", + "JSONMessageFile" : './test/server/message.json', + "XMLMessageFile" : './test/server/message.xml' + +}; + +var RouteManager = { + "findRoute" : function(req, res) { + var handler = null; + for ( var route in this.routes) { + if (req.url.startsWith(route)) { + handler = this.routes[route]; + } + + } + 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(RouterOptions.JSONMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type' : 'application/json', + 'test-header' : 'test' + }); + res.write(message.toString()); + res.end(); + }, + "/json/path" : function(req, res) { + // this.sleep(5000); + var message = { + "url" : req.url + }; + res.writeHead(200, { + 'Content-Type' : 'application/json', + 'test-header' : req.url + }); + res.write(JSON.stringify(message)); + res.end(); + }, + "/xml" : function(req, res) { + var message = fs.readFileSync(RouterOptions.XMLMessageFile, '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/path/post" : function(req, res) { + req.on('data', function(data) { + var message = { + "url" : req.url + }; + // console.log("[SERVER] data = ", data); + res.writeHead(200, { + 'Content-Type' : 'application/json' + }); + // res.writeHead(200, {'Content-Type': 'text/plain'}); + message.postData = data.toString(); + res.write(JSON.stringify(message)); + res.end(); + }); + + }, + "/json/error" : function(req, res) { + // this.sleep(5000); + + + res.writeHead(500, {'Content-Type': 'text/plain'}); + res.end(); + + + }, + "/xml/path/post" : function(req, res) { + req.on('data', function(data) { + // console.log("[SERVER] data = ", data); + res.writeHead(200, { + 'Content-Type' : 'application/xml' + }); + // res.writeHead(200, {'Content-Type': 'text/plain'}); + console.log("en server responde ", data.toString()); + res.write(data.toString()); + res.end(); + }); + + }, + "/json/empty" : function(req, res) { + res.writeHead(200, { + '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(); + }, + "/json/test/content/type" : function(req, res) { + var message = fs.readFileSync(RouterOptions.JSONMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type' : 'test/json' + }); + res.write(message.toString()); + res.end(); + }, + "/xml/test/content/type" : function(req, res) { + var message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type' : 'test/xml' + }); + res.write(message.toString()); + res.end(); + } + }, + "sleep" : function(ms) { + + var stop = new Date().getTime(); + while (new Date().getTime() < stop + ms) { + ; + } + } + +}; + +// Create an HTTP server +this.server = http.createServer(function(req, res) { + // console.log("[SERVER] req.url", req.url); + RouteManager.findRoute(req, res); +}); + +exports.baseURL = "http://localhost:4444"; + +exports.listen = function() { + this.server.listen.apply(this.server, arguments); +}; + +exports.close = function(callback) { + this.server.close(callback); +}; + +exports.on = function(event, cb) { + this.server.on.apply(this.server, event, cb); +}; diff --git a/test/specs.js b/test/specs.js deleted file mode 100644 index e874cd3..0000000 --- a/test/specs.js +++ /dev/null @@ -1,32 +0,0 @@ -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/specs/TestErrorHandlers.js b/test/specs/TestErrorHandlers.js new file mode 100644 index 0000000..1606dfe --- /dev/null +++ b/test/specs/TestErrorHandlers.js @@ -0,0 +1,54 @@ +var server =require("../server/mock-server"), +Client=require("../../lib/node-rest-client").Client; + +describe('Error Handlers', function () { + + this.timeout(150000); + + before(function () { + server.listen(4444); + console.log("server started on port 4444"); + }); + + describe("Client Error Hanlers",function(){ + + + it("handle error with client handler", function(done){ + var client = new Client(); + client.on('error', function(err){ + done(); + }); + client.get(server.baseURL + "/json/error", function(data, response){ + client.emit('error', response.status); + }); + + }); + + + + + }); + +describe("#Request Error Handlers",function(){ + + it("handle error with request handler", function(done){ + var client = new Client(); + + var req =client.get(server.baseURL + "/json/error", function(data, response){ + req.emit('error', response.status); + }); + + req.on('error',function(err){ + done(); + }) + + }); + +}); + +after(function () { + server.close(); + console.log("server stopped"); +}); + +}); \ No newline at end of file diff --git a/test/specs/TestGETMethod.js b/test/specs/TestGETMethod.js new file mode 100644 index 0000000..e6a1fd4 --- /dev/null +++ b/test/specs/TestGETMethod.js @@ -0,0 +1,232 @@ +var server =require("../server/mock-server"), +Client=require("../../lib/node-rest-client").Client; + +describe('GET Method', function () { + + this.timeout(150000); + + before(function () { + server.listen(4444); + console.log("server started on port 4444"); + }); + + describe("#JSON",function(){ + + it("GET request with no args", function(done){ + var client = new Client(); + client.get(server.baseURL + "/json", function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + done(); + }); + }); + + it("GET request with path variable substitution", function(done){ + var client = new Client(); + var args ={ + path:{testNumber:123, testString:"test"} + }; + client.get(server.baseURL + "/json/path/${testNumber}/${testString}",args, function(data, response){ + + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/123/test"); + done(); + }); + }); + + + it("GET request with parameters", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"} + }; + client.get(server.baseURL + "/json/path/query",args, function(data, response){ + + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/query?testNumber=123&testString=test"); + done(); + }); + }); + + it("GET request with registered method and no args", function(done){ + var client = new Client(); + + + client.registerMethod("testMethod",server.baseURL + "/json","GET"); + + client.methods.testMethod( function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + done(); + }); + }); + + + + it("GET request with registered method and path variable substitution", function(done){ + var client = new Client(); + var args ={ + path:{testNumber:123, testString:"test"} + }; + + client.registerMethod("testMethod",server.baseURL + "/json/path/${testNumber}/${testString}","GET"); + + client.methods.testMethod(args, function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/123/test"); + done(); + }); + }); + + + it("GET request with registered method and parameters", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"} + }; + + client.registerMethod("testMethod",server.baseURL + "/json/path/query","GET"); + + client.methods.testMethod(args, function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/query?testNumber=123&testString=test"); + + done(); + }); + }); + + it("GET request with incompatible parameters URL", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"} + }; + + client.on('error', function(err){ + err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); + done(); + }); + + client.get(server.baseURL + "/json/path/query?testNumber=123&testString=test", args, function(data, response){ + //noop + }).should.throw(); + + }); + + it("GET request with invalid args type", function(done){ + var client = new Client(); + var args = "123"; + + client.on('error', function(err){ + err.should.startWith("args should be and object"); + done(); + }); + + + + client.get(server.baseURL + "/json/path/query",args, function(data, response){ + //noop + }).should.throw(); + + }); + + + it("GET request with invalid parameters type", function(done){ + var client = new Client(); + var args ={ + parameters:"{test='123'}" + }; + + client.on('error', function(err){ + err.should.startWith("cannot serialize"); + done(); + }); + + + + client.get(server.baseURL + "/json/path/query",args, function(data, response){ + //noop + }).should.throw(); + + }); + + it("GET request with registered method and incompatible parameters URL", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"} + }; + + client.on('error', function(err){ + err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); + done(); + }); + + client.registerMethod("testMethod",server.baseURL + "/json/path/query?testNumber=123&testString=test","GET"); + + client.methods.testMethod(args, function(data, response){ + //noop + }).should.throw(); + + }); + + it("GET request with registered method and invalid args type", function(done){ + var client = new Client(); + var args ="123"; + + client.on('error', function(err){ + err.should.startWith("args should be and object"); + done(); + }); + + client.registerMethod("testMethod",server.baseURL + "/json/path/query","GET"); + + client.methods.testMethod(args, function(data, response){ + //noop + }).should.throw(); + + }); + + + it("GET request with registered method and invalid parameters type", function(done){ + var client = new Client(); + var args ={ + parameters:"{test='123'}" + }; + + client.on('error', function(err){ + err.should.startWith("cannot serialize"); + done(); + }); + + client.registerMethod("testMethod",server.baseURL + "/json/path/query","GET"); + + client.methods.testMethod(args, function(data, response){ + //noop + }).should.throw(); + + }); + }); + + +describe("#XML",function(){ + + it("GET request with no args", function(done){ + var client = new Client(); + client.get(server.baseURL + "/xml", function(data, response){ + console.log("data es ", data); + data.should.not.equal(null); + data.should.type("object"); + done(); + }); + }); + +}); + +after(function () { + server.close(); + console.log("server stopped"); +}); +}); \ No newline at end of file diff --git a/test/specs/TestIOFacade.js b/test/specs/TestIOFacade.js new file mode 100644 index 0000000..6a39308 --- /dev/null +++ b/test/specs/TestIOFacade.js @@ -0,0 +1,318 @@ +var server =require("../server/mock-server"), +Client=require("../../lib/node-rest-client").Client; + +describe('IO Facade', function () { + + this.timeout(150000); + + before(function () { + server.listen(4444); + console.log("server started on port 4444"); + }); + + describe("#Parsers",function(){ + + var testParser = {"name":"test-parser", + "isDefault":false, + "match":function(response){ + return response.headers["test-header"] === "test"; + }, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ + var message = JSON.parse(byteBuffer.toString()); + message.parsed = true; + parsedCallback(message); + } + }, + defaultTestParser = {"name":"default-test-parser", + "isDefault":true, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ + var message = JSON.parse(byteBuffer.toString()); + message.defaultParsed = true; + parsedCallback(message); + } + }; + + it("add invalid parser to client", function(done){ + var client = new Client(); + + client.on('error', function(err){ + err.should.startWith("parser cannot be added: invalid parser definition"); + done(); + }); + + client.parsers.add({"invalid":123, "parser":456}).should.throw(); + }); + + + it("add parser to client", function(done){ + var client = new Client(); + client.parsers.add(testParser); + var parser = client.parsers.find("test-parser"); + + parser.should.not.equal(null); + parser.should.type("object"); + done(); + }); + + + it("remove parser from client", function(done){ + var client = new Client(); + + client.on('error', function(err){ + err.should.startWith("cannot find parser: test-parser doesn't exists"); + done(); + }); + + client.parsers.add(testParser); + var parser = client.parsers.find("test-parser"); + + parser.should.not.equal(null); + parser.should.type("object"); + + client.parsers.remove("test-parser"); + client.parsers.find("test-parser"); + }); + + it("response match parser", function(done){ + var client = new Client(); + client.parsers.clean(); + client.parsers.add(testParser); + + client.get(server.baseURL + "/json", function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.should.have.property("parsed"); + data.parsed.should.be.a.Boolean; + data.parsed.should.be.true; + done(); + }); + + }); + + + it("add and use default parser", function(done){ + var client = new Client(); + client.parsers.clean(); + + client.parsers.add(testParser); + client.parsers.add(defaultTestParser); + // no parsers defined, default must be used + + client.get(server.baseURL + "/json/path?default-test", function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.should.have.property("defaultParsed"); + data.defaultParsed.should.be.a.Boolean; + data.defaultParsed.should.be.true; + done(); + }); + + }); + + + it("add custom types to args in JSON parser", function(done){ + var options={ + // customize mime types for json or xml connections + mimetypes: { + json: ["test/json"] + } + }; + var client = new Client(options); + + client.get(server.baseURL + "/json/test/content/type", function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + done(); + }); + + }); + + + it("add custom types to args in XML parser", function(done){ + var options={ + // customize mime types for json or xml connections + mimetypes: { + xml: ["test/xml"] + } + }; + var client = new Client(options); + + client.get(server.baseURL + "/xml/test/content/type", function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + done(); + }); + + }); + + + it("get all regular parsers", function(done){ + + var client = new Client(); + var parsers = client.parsers.getAll(); + parsers.should.have.length(2); + done(); + + }); + + it("emit custom event from parser to client", function(done){ + var client = new Client(); + client.on('customEvent',function(event){ + event.should.be.equal("my custom event"); + done(); + }); + + + client.parsers.clean(); + client.parsers.add({ + "name":"example-parser", + "isDefault":false, + "match":function(request){return true; }, + "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ + nrcEventEmitter('customEvent','my custom event'); + // pass serialized data to client to be sent to remote API + parsedCallback(byteBuffer.toString()); + + } + }); + + var args ={data:"test data"} + + client.post(server.baseURL + "/json/path/post/query",args, function(data, response){}); + + }); + + }); + + +describe("#Serializers",function(){ + + var testSerializer = {"name":"test-serializer", + "isDefault":false, + "match":function(request){ + return request.headers["test-header"] === "test"; + }, + "serialize":function(data,nrcEventEmitter,serializedCallback){ + if (typeof data === 'object') + data.serialized = true; + serializedCallback(JSON.stringify(data)); + } + }, + defaultTestSerializer = {"name":"default-test-serializer", + "isDefault":true, + "serialize":function(data,nrcEventEmitter,serializedCallback){ + if (typeof data === 'object') + data.defaultParsed = true; + serializedCallback(data); + } + }; + + + it("add invalid serializer to client", function(done){ + var client = new Client(); + + client.on('error', function(err){ + err.should.startWith("serializer cannot be added: invalid serializer definition"); + done(); + }); + + client.serializers.add({"invalid":123, "serializer":456}).should.throw(); + }); + + + + it("add serializer to client", function(done){ + var client = new Client(); + client.serializers.add(testSerializer); + var serializer = client.serializers.find("test-serializer"); + + serializer.should.not.equal(null); + serializer.should.type("object"); + done(); + }); + + + it("remove serializer from client", function(done){ + var client = new Client(); + + client.on('error', function(err){ + err.should.startWith("cannot find serializer: test-serializer doesn't exists"); + done(); + }); + + client.serializers.add(testSerializer); + var serializer = client.serializers.find("test-serializer"); + + serializer.should.not.equal(null); + serializer.should.type("object"); + + client.serializers.remove("test-serializer"); + client.serializers.find("test-serializer"); + }); + + + it("request match serializer", function(done){ + var client = new Client(), + args={ + headers:{"test-header":"test"}, + data:{"testNumber":123, "testString":"abc"} + }; + + client.serializers.clean(); + client.serializers.add(testSerializer); + + var request = client.post(server.baseURL + "/json/path/post",args, function(data, response){ + data.postData.should.not.equal(null); + data.postData.should.type("object"); + data.postData.should.have.property("serialized"); + data.postData.serialized.should.be.a.Boolean; + data.postData.serialized.should.be.true; + }); + + done(); + + }); + + it("get all regular serializers", function(done){ + + var client = new Client(); + var serializers = client.serializers.getAll(); + serializers.should.have.length(3); + done(); + + }); + + it("emit custom event from serializer to client", function(done){ + var client = new Client(); + client.on('customEvent',function(event){ + event.should.be.equal("my custom event"); + done(); + }); + + + client.serializers.clean(); + client.serializers.add({ + "name":"example-serializer", + "isDefault":false, + "match":function(request){return true; }, + "serialize":function(data,nrcEventEmitter,serializedCallback){ + nrcEventEmitter('customEvent','my custom event'); + // pass serialized data to client to be sent to remote API + serializedCallback(data.toString()); + + } + }); + + var args ={data:"test data"} + + client.post(server.baseURL + "/json/path/post/query",args, function(data, response){}); + + }); + +}); + +after(function () { + server.close(); + console.log("server stopped"); +}); +}); \ No newline at end of file diff --git a/test/specs/TestPOSTMethod.js b/test/specs/TestPOSTMethod.js new file mode 100644 index 0000000..1300006 --- /dev/null +++ b/test/specs/TestPOSTMethod.js @@ -0,0 +1,241 @@ +var server =require("../server/mock-server"), +Client=require("../../lib/node-rest-client").Client; + +describe('POST Method', function () { + + this.timeout(150000); + + before(function () { + server.listen(4444); + console.log("server started on port 4444"); + }); + + describe("#JSON",function(){ + + it("POST request with path variable substitution", function(done){ + var client = new Client(); + var args ={ + path:{testNumber:123, testString:"test"}, + data:'{"dataNumber":123, "dataString":"test"}' + + }; + client.post(server.baseURL + "/json/path/post/${testNumber}/${testString}",args, function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/post/123/test"); + data.postData.should.equal('{"dataNumber":123, "dataString":"test"}'); + done(); + }); + }); + + + it("POST request with parameters", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"}, + data:'{"dataNumber":123,"dataString":"test"}' + }; + client.post(server.baseURL + "/json/path/post/query",args, function(data, response){ + + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/post/query?testNumber=123&testString=test"); + data.postData.should.equal('{"dataNumber":123,"dataString":"test"}'); + done(); + }); + }); + + it("POST request with registered method and no args", function(done){ + var client = new Client(); + + + client.registerMethod("testMethod",server.baseURL + "/json","POST"); + + client.methods.testMethod( function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + done(); + }); + }); + + + + it("POST request with registered method and path variable substitution", function(done){ + var client = new Client(); + var args ={ + path:{testNumber:123, testString:"test"}, + data:'{"dataNumber":123,"dataString":"test"}' + }; + + client.registerMethod("testMethod",server.baseURL + "/json/path/post/${testNumber}/${testString}","POST"); + + client.methods.testMethod(args, function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/post/123/test"); + data.postData.should.equal('{"dataNumber":123,"dataString":"test"}'); + done(); + }); + }); + + + it("POST request with registered method and parameters", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"}, + data:'{"dataNumber":123,"dataString":"test"}' + }; + + client.registerMethod("testMethod",server.baseURL + "/json/path/post/query","POST"); + + client.methods.testMethod(args, function(data, response){ + data.should.not.equal(null); + data.should.type("object"); + data.url.should.equal("/json/path/post/query?testNumber=123&testString=test"); + data.postData.should.equal('{"dataNumber":123,"dataString":"test"}'); + done(); + }); + }); + + it("POST request with incompatible parameters URL", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"}, + data:{dataNumber:123, dataString:"test"} + }; + + client.on('error', function(err){ + err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); + done(); + }); + + client.post(server.baseURL + "/json/path/post/query?testNumber=123&testString=test", args, function(data, response){ + // noop + }).should.throw(); + + }); + + it("POST request with invalid args type", function(done){ + var client = new Client(); + var args = "123"; + + client.on('error', function(err){ + err.should.startWith("args should be and object"); + done(); + }); + + + + client.post(server.baseURL + "/json/path/post/query",args, function(data, response){ + // noop + }).should.throw(); + + }); + + + it("POST request with invalid parameters type", function(done){ + var client = new Client(); + var args ={ + parameters:"{test='123'}" + }; + + client.on('error', function(err){ + err.should.startWith("cannot serialize"); + done(); + }); + + + + client.post(server.baseURL + "/json/path/post/query",args, function(data, response){ + // noop + }).should.throw(); + + }); + + it("POST request with registered method and incompatible parameters URL", function(done){ + var client = new Client(); + var args ={ + parameters:{testNumber:123, testString:"test"}, + data:{dataNumber:123, dataString:"test"} + }; + + client.on('error', function(err){ + err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); + done(); + }); + + client.registerMethod("testMethod",server.baseURL + "/json/path/post/query?testNumber=123&testString=test","POST"); + + client.methods.testMethod(args, function(data, response){ + // noop + }).should.throw(); + + }); + + it("POST request with registered method and invalid args type", function(done){ + var client = new Client(); + var args ="123"; + + client.on('error', function(err){ + err.should.startWith("args should be and object"); + done(); + }); + + client.registerMethod("testMethod",server.baseURL + "/json/path/post/query","POST"); + + client.methods.testMethod(args, function(data, response){ + // noop + }).should.throw(); + + }); + + + it("POST request with registered method and invalid parameters type", function(done){ + var client = new Client(); + var args ={ + parameters:"{test='123'}" + }; + + client.on('error', function(err){ + err.should.startWith("cannot serialize"); + done(); + }); + + client.registerMethod("testMethod",server.baseURL + "/json/path/post/query","POST"); + + client.methods.testMethod(args, function(data, response){ + // noop + }).should.throw(); + + }); + }); + +describe("#XML",function(){ + + it("POST request with parameters", function(done){ + var client = new Client(); + var args ={ + data:"123123" + }; + client.post(server.baseURL + "/xml/path/post/query",args, function(data, response){ + data.should.type("object"); + data.testData.should.be.ok; + data.testData.testNumber.should.be.ok; + data.testData.testString.should.be.ok; + data.testData.testNumber.should.be.a.Number; + data.testData.testString.should.be.a.String; + data.testData.testNumber[0].should.be.equal("123"); + data.testData.testString[0].should.be.equal("123"); + + + done(); + }); + }); + +}); + +after(function () { + server.close(); + console.log("server stopped"); +}); +}); \ No newline at end of file diff --git a/test/test-proxy.js b/test/test-proxy.js index bfb9285..122eceb 100644 --- a/test/test-proxy.js +++ b/test/test-proxy.js @@ -11,14 +11,14 @@ 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) }); + .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 }); + .filter(function(rx) { return rx.length }); } function ip_allowed(ip) { diff --git a/test/test-servers.js b/test/test-servers.js deleted file mode 100644 index cd0ea84..0000000 --- a/test/test-servers.js +++ /dev/null @@ -1,81 +0,0 @@ -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);