This commit is contained in:
Ashen One
2022-12-22 07:22:29 +04:00
commit a417031af4
889 changed files with 108839 additions and 0 deletions

13
node_modules/node-rest-client/LICENSE generated vendored Normal file
View File

@ -0,0 +1,13 @@
Copyright (c) 2013 Alejandro Alvarez Acero. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2
node_modules/node-rest-client/debug.bat generated vendored Normal file
View File

@ -0,0 +1,2 @@
start node-inspector
start mocha --debug-brk -R spec %1%

36
node_modules/node-rest-client/debug.sh generated vendored Normal file
View File

@ -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 &

745
node_modules/node-rest-client/lib/node-rest-client.js generated vendored Normal file
View File

@ -0,0 +1,745 @@
var http = require('follow-redirects').http,
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,
// parser response manager
parserManager = require("./nrc-parser-manager")(),
serializerManager = require("./nrc-serializer-manager")(),
// connection manager
connectManager = new ConnectManager(this, parserManager),
// 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 || {};
// 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];
}
}
// don't use tunnel to connect to proxy, direct request
// and delete proxy options
if (!self.useProxyTunnel){
for (var proxyOption in result.proxy){
result[proxyOption] = result.proxy[proxyOption];
}
delete result.proxy;
}
// add general request and response config to connect options
result.requestConfig = self.requestConfig;
result.responseConfig = self.responseConfig;
return result;
},
decodeQueryFromURL: function(connectURL){
var url = urlParser.parse(connectURL),
query = url.query.substring(1).split("&"),
keyValue,
result={};
// create decoded args from key value elements in query+
for (var i=0;i<query.length;i++){
keyValue = query[i].split("=");
result[keyValue[0]] = decodeURIComponent(keyValue[1]);
}
return result;
},
serializeEncodeQueryFromArgs:function(args){
function serialize(obj, parent) {
var tokens = [], propertyName;
// iterate over all properties
for(propertyName in obj) {
// if object has property (it's not an array iteration)
if (obj.hasOwnProperty(propertyName)) {
// if property has parent, add nested reference
var parsedProperty = parent ? parent + "[" + propertyName + "]" : propertyName, propertyValue = obj[propertyName];
// if property has value and is object (we must iterate
// again, not final leaf)
// iterate over object property passing current parsed
// property as parent
// else add encoded parsed property and value to result
// array
tokens.push((propertyValue !== null && typeof propertyValue === "object") ?
serialize(propertyValue, parsedProperty) :
encodeURIComponent(parsedProperty) + "=" + encodeURIComponent(propertyValue));
}
}
return tokens.join("&");
}
debug("args is", args);
// check args consistency
if (args && typeof args !== 'object' )
self.emit('error','cannot serialize parameters: invalid type ' + (typeof args) + ' should be an object type');
return serialize(args);
},
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){
//wrapper for emit function on client
var clientEmitterWrapper = function (client){
var client = client;
return function(type, event){client.emit(type, event);};
};
// 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];
}
}
}
// 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);
// 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)){
serializerManager.get(options).serialize(args.data, clientEmitterWrapper(self), function(serializedData){
options.data = serializedData;
options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = Buffer.byteLength(options.data, 'utf8');
});
}else{
options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0;
}
}
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) {
var client = 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.configureOptions = function(options){
var followRedirectsProps =["followRedirects", "maxRedirects"];
function configureProps(propsArray, optionsElement){
for (var index in propsArray){
if (optionsElement.hasOwnProperty(propsArray[index]))
options[propsArray[index]] = optionsElement[propsArray[index]];
}
}
//add follows-redirects config
configureProps(followRedirectsProps, options.requestConfig);
// 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);
};
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;
self.configureOptions(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(Buffer.from(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)request.write(options.data);
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;
self.configureOptions(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(Buffer.from(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)request.write(options.data);
request.end(); // end request when data is written
// 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);
};

156
node_modules/node-rest-client/lib/nrc-parser-manager.js generated vendored Normal file
View File

@ -0,0 +1,156 @@
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<this.contentTypes.length;i++){
result = this.contentTypes[i].trim().toLowerCase() === contentType.trim().toLowerCase();
if (result) break;
}
return result;
}
};
//add default parser managers: JSON,XML and unknown content/type
parserManager.add(Object.assign({
"name":"XML",
"options":{"explicitArray":false},
"contentTypes":["application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8"],
"parseString":require('xml2js').parseString,
"parse":function(byteBuffer,nrcEventEmitter,parsedCallback){
this.parseString(byteBuffer.toString(),this.options, function (err, result) {
parsedCallback(result);
});
}
}, BaseParser));
parserManager.add(Object.assign({
"name":"JSON",
"contentTypes":["application/json","application/json;charset=utf-8"],
"isValidData":function(data){
return data !== undefined && (data.length !== undefined && data.length > 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;
}

View File

@ -0,0 +1,185 @@
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<this.contentTypes.length;i++){
result = this.contentTypes[i].trim().toLowerCase() === contentType.trim().toLowerCase();
if (result) break;
}
return result;
}
};
//add default serializer managers: JSON,XML and unknown content/type
serializerManager.add(Object.assign({
"name":"XML",
"options":{},
"contentTypes":["application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8"],
"xmlSerializer":new xmlserializer.Builder(this.options),
"serialize":function(data,nrcEventEmitter,serializedCallback){
if (typeof data === 'object')
data = xmlSerializer.buildObject(data);
serializedCallback(data);
}
},BaseSerializer));
serializerManager.add(Object.assign({
"name":"JSON",
"contentTypes":["application/json","application/json;charset=utf-8"],
"serialize":function(data,nrcEventEmitter,serializedCallback){
if(typeof data === 'object')
data = JSON.stringify(data);
serializedCallback(data);
}
},BaseSerializer));
serializerManager.add(Object.assign({
"name":"FORM-ENCODED",
"contentTypes":["application/x-www-form-urlencoded","multipart/form-data","text/plain"],
"encode":function (obj, parent) {
var tokens = [], propertyName;
//iterate over all properties
for(propertyName in obj) {
//if object has property (it's not an array iteration)
if (obj.hasOwnProperty(propertyName)) {
//if property has parent, add nested reference
var parsedProperty = parent ? parent + "[" + propertyName + "]" : propertyName, propertyValue = obj[propertyName];
// if property has value and is object (we must iterate again, not final leaf)
// iterate over object property passing current parsed property as parent
// else add encoded parsed property and value to result array
tokens.push((propertyValue !== null && typeof propertyValue === "object") ?
serialize(propertyValue, parsedProperty) :
encodeURIComponent(parsedProperty) + "=" + encodeURIComponent(propertyValue));
}
}
return tokens.join("&");
},
"serialize":function(data,nrcEventEmitter,serializedCallback){
if(typeof data === 'object')
data = this.encode(data);
serializedCallback(data);
}
},BaseSerializer));
serializerManager.add({
"name":"DEFAULT",
"isDefault":true,
"serialize":function(data,nrcEventEmitter,serializedCallback){
serializedCallback(data.toString());
}
});
return serializerManager;
};

28
node_modules/node-rest-client/package.json generated vendored Normal file
View File

@ -0,0 +1,28 @@
{
"author": "Alejandro Alvarez Acero",
"name": "node-rest-client",
"description": "node API REST client",
"version": "3.1.1",
"repository": {
"type": "git",
"url": "https://github.com/aacerox/node-rest-client.git"
},
"main": "./lib/node-rest-client",
"dependencies": {
"xml2js": ">=0.4.23",
"debug": "~4.3.3",
"follow-redirects": ">=1.14.7"
},
"devDependencies": {
"mocha": "*",
"should": ">= 0.0.1"
},
"optionalDependencies": {},
"engines": {
"node": "*"
},
"scripts": {
"test": "mocha"
},
"license": "MIT"
}

873
node_modules/node-rest-client/readme.md generated vendored Normal file
View File

@ -0,0 +1,873 @@
# REST Client for Node.js
[![npm version](https://badge.fury.io/js/node-rest-client.svg)](https://www.npmjs.com/package/node-rest-client)
[![Build Status](https://travis-ci.org/olalonde/node-rest-client.svg?branch=master)](https://travis-ci.org/olalonde/node-rest-client)
[![NPM](https://nodei.co/npm/node-rest-client.png?downloads=true)](https://nodei.co/npm/node-rest-client.png?downloads=true)
## 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 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 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
$ npm install node-rest-client
## Usages
### Simple HTTP GET
Client has two ways to call a REST service: direct or using registered methods
```javascript
var Client = require('node-rest-client').Client;
var client = new Client();
// direct way
client.get("http://remote.site/rest/xml/method", function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET");
client.methods.jsonMethod(function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
### HTTP POST
POST, PUT or PATCH method invocation are configured like GET calls with the difference that you have to set "Content-Type" header in args passed to client method invocation:
```javascript
//Example POST method invocation
var Client = require('node-rest-client').Client;
var client = new Client();
// set content-type header and data as json in args parameter
var args = {
data: { test: "hello" },
headers: { "Content-Type": "application/json" }
};
client.post("http://remote.site/rest/xml/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("postMethod", "http://remote.site/rest/json/method", "POST");
client.methods.postMethod(args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
If no "Content-Type" header is set as client arg POST,PUT and PATCH methods will not work properly.
### Passing args to registered methods
You can pass diferents args to registered methods, simplifying reuse: path replace parameters, query parameters, custom headers
```javascript
var Client = require('node-rest-client').Client;
// direct way
var client = new Client();
var args = {
data: { test: "hello" }, // data passed to REST method (only useful in POST, PUT or PATCH methods)
path: { "id": 120 }, // path substitution var
parameters: { arg1: "hello", arg2: "world" }, // this is serialized as URL parameters
headers: { "test-header": "client-api" } // request headers
};
client.get("http://remote.site/rest/json/${id}/method", args,
function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("jsonMethod", "http://remote.site/rest/json/${id}/method", "GET");
/* this would construct the following URL before invocation
*
* http://remote.site/rest/json/120/method?arg1=hello&arg2=world
*
*/
client.methods.jsonMethod(args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
You can even use path placeholders in query string in direct connection:
```javascript
var Client = require('node-rest-client').Client;
// direct way
var client = new Client();
var args = {
path: { "id": 120, "arg1": "hello", "arg2": "world" },
headers: { "test-header": "client-api" }
};
client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args,
function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
### HTTP POST and PUT methods
To send data to remote site using POST or PUT methods, just add a data attribute to args object:
```javascript
var Client = require('node-rest-client').Client;
// direct way
var client = new Client();
var args = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>"
};
client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// registering remote methods
client.registerMethod("xmlMethod", "http://remote.site/rest/xml/${id}/method", "POST");
client.methods.xmlMethod(args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// posted data can be js object
var args_js = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: { "arg1": "hello", "arg2": 123 }
};
client.methods.xmlMethod(args_js, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
### Request/Response configuration
It's also possible to configure each request and response, passing its configuration as an
additional argument in method call.
```javascript
var client = new Client();
// request and response additional configuration
var args = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>",
requestConfig: {
timeout: 1000, //request timeout in milliseconds
noDelay: true, //Enable/disable the Nagle algorithm
keepAlive: true, //Enable/disable keep-alive functionalityidle socket.
keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent
},
responseConfig: {
timeout: 1000 //response timeout
}
};
client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
```
If you want to handle timeout events both in the request and in the response just add a new "requestTimeout"
or "responseTimeout" event handler to clientRequest returned by method call.
```javascript
var client = new Client();
// request and response additional configuration
var args = {
path: { "id": 120 },
parameters: { arg1: "hello", arg2: "world" },
headers: { "test-header": "client-api" },
data: "<xml><arg1>hello</arg1><arg2>world</arg2></xml>",
requestConfig: {
timeout: 1000, //request timeout in milliseconds
noDelay: true, //Enable/disable the Nagle algorithm
keepAlive: true, //Enable/disable keep-alive functionalityidle socket.
keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent
},
responseConfig: {
timeout: 1000 //response timeout
}
};
var req = client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
req.on('requestTimeout', function (req) {
console.log('request has expired');
req.abort();
});
req.on('responseTimeout', function (res) {
console.log('response has expired');
});
//it's usefull to handle request errors to avoid, for example, socket hang up errors on request timeouts
req.on('error', function (err) {
console.log('request error', err);
});
```
### Setup client to trust self-signed certificate with custom CA chain
In Internet mostly recommend solution to handle self-signed certificate is to just disable verification of server.
**NEVER DO THAT IN PRODUCTION!**
You can do that only for development purpose - never in production because it puts great security risk on your business.
However if you are connecting to a known server using self-signed certificate or a company server signed with corporate CA you can easily setup client to trust them and being secured in same moment.
So for example certificate chain:
```
+-- root-CA (self-signed)
| +-- department-CA (singed with root-CA)
| +-- domain (signed with department-CA)
```
a solution is as follow:
```javascript
var fs = required('fs');
var trustedCertificates = [
fs.readFileSync('/PATH/TO/DOMAIN/CERTIFICATE'),
fs.readFileSync('/PATH/TO/DEPARTMENT/CA'),
fs.readFileSync('/PATH/TO/ROOT/CA')
];
var options = {
connection: {
ca: trustedCertificates
}
};
var client = new Client(options);
```
Note that for readability format of certificate are skipped as multiple ones are supported.
### Follows Redirect
Node REST client follows redirects by default to a maximum of 21 redirects, but it's also possible to change follows redirect default config in each request done by the client
```javascript
var client = new Client();
// request and response additional configuration
var args = {
requestConfig: {
followRedirects:true,//whether redirects should be followed(default,true)
maxRedirects:10//set max redirects allowed (default:21)
},
responseConfig: {
timeout: 1000 //response timeout
}
};
```
### 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);
```
Additionally in this parser there's an attribute "options" where you can customize xml2js parser options. Please refer to [xml2js package](https://www.npmjs.com/package/xml2js) for valid parser options.
```javascript
var client = new Client();
client.parsers.find("XML").options= {"explicitArray":false, "ignoreAttrs":true};
```
- _**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"
Additionally in this parser there's an attribute "options" where you can customize xml2js serializer options. Please refer to [xml2js package](https://www.npmjs.com/package/xml2js) for valid builder options.
```javascript
var client = new Client();
client.serializers.find("XML").options={"renderOpts":{"pretty": true }};
```
- _**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
Just pass proxy configuration as option to client.
```javascript
var Client = require('node-rest-client').Client;
// configure proxy
var options_proxy = {
proxy: {
host: "proxy.foo.com",
port: 8080,
user: "proxyuser",
password: "123",
tunnel: true
}
};
var client = new Client(options_proxy);
```
client has 2 ways to connect to target site through a proxy server: tunnel or direct request, the first one is the default option
so if you want to use direct request you must set tunnel off.
```javascript
var Client = require('node-rest-client').Client;
// configure proxy
var options_proxy = {
proxy: {
host: "proxy.foo.com",
port: 8080,
user: "proxyuser",
password: "123",
tunnel: false // use direct request to proxy
}
};
var client = new Client(options_proxy);
```
### Basic HTTP auth
Just pass username and password or just username, if no password is required by remote site, as option to client. Every request done with the client will pass username and password or just username if no password is required as basic authorization header.
```javascript
var Client = require('node-rest-client').Client;
// configure basic http auth for every request
var options_auth = { user: "admin", password: "123" };
var client = new Client(options_auth);
```
### Options parameters
You can pass the following args when creating a new client:
```javascript
var options = {
// proxy configuration
proxy: {
host: "proxy.foo.com", // proxy host
port: 8080, // proxy port
user: "ellen", // proxy username if required
password: "ripley" // proxy pass if required
},
// aditional connection options passed to node http.request y https.request methods
// (ie: options to connect to IIS with SSL)
connection: {
secureOptions: constants.SSL_OP_NO_TLSv1_2,
ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM',
honorCipherOrder: true
},
// 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"]
},
user: "admin", // basic http auth username if required
password: "123", // basic http auth password if required
requestConfig: {
timeout: 1000, //request timeout in milliseconds
noDelay: true, //Enable/disable the Nagle algorithm
keepAlive: true, //Enable/disable keep-alive functionalityidle socket.
keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent
},
responseConfig: {
timeout: 1000 //response timeout
}
};
```
Note that requestConfig and responseConfig options if set on client instantiation apply to all of its requests/responses
and is only overriden by request or reponse configs passed as args in method calls.
### Managing Requests
Each REST method invocation returns a request object with specific request options and error, requestTimeout and responseTimeout event handlers.
```javascript
var Client = require('node-rest-client').Client;
var client = new Client();
var args = {
requesConfig: { timeout: 1000 },
responseConfig: { timeout: 2000 }
};
// direct way
var req1 = client.get("http://remote.site/rest/xml/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// view req1 options
console.log(req1.options);
req1.on('requestTimeout', function (req) {
console.log("request has expired");
req.abort();
});
req1.on('responseTimeout', function (res) {
console.log("response has expired");
});
// registering remote methods
client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET");
var req2 = client.methods.jsonMethod(function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
// handling specific req2 errors
req2.on('error', function (err) {
console.log('something went wrong on req2!!', err.request.options);
});
```
### Error Handling
Now you can handle error events in two places: on client or on each request.
```javascript
var client = new Client(options_auth);
// handling request error events
client.get("http://remote.site/rest/xml/method", function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
// handling client error events
client.on('error', function (err) {
console.error('Something went wrong on the client', err);
});
```
**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". _

1
node_modules/node-rest-client/test.bat generated vendored Normal file
View File

@ -0,0 +1 @@
npm test

2
node_modules/node-rest-client/test.sh generated vendored Normal file
View File

@ -0,0 +1,2 @@
#!/bin/sh
npm test

4
node_modules/node-rest-client/test/mocha.opts generated vendored Normal file
View File

@ -0,0 +1,4 @@
--require should
--reporter spec test/specs/*.js
--ui bdd
--recursive

34
node_modules/node-rest-client/test/server/message.json generated vendored Normal file
View File

@ -0,0 +1,34 @@
{"catalog":{
"books":[{
"id":"bk101",
"author":"Gambardella, Matthew",
"title":"XML Developer's Guide",
"genre":"Computer",
"price":44.95,
"publish_date":"2000-10-10",
"description":"An in-depth look at creating applications with XML."
},
{
"id":"bk102",
"author":"Gambardella, Matthew",
"title":"JAVA Developer's Guide",
"genre":"Computer",
"price":188.95,
"publish_date":"2000-10-10",
"description":"An in-depth look at creating applications with JAVA."
},
{
"id":"bk103",
"author":"Gambardella, Matthew",
"title":"JSON Developer's Guide",
"genre":"Computer",
"price":422.95,
"publish_date":"2000-10-10",
"description":"An in-depth look at creating applications with JSON."
}
]
}
}

43
node_modules/node-rest-client/test/server/message.xml generated vendored Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
</catalog>

View File

@ -0,0 +1,210 @@
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'});
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();
},
"/followRedirects":function(req, res){
var repeatOffset = req.url.indexOf("?"),
repeat = parseInt(req.url.substring(repeatOffset + 1),10),
location = "";
if (repeatOffset === 0){
res.writeHead(301, {
'Location':'http://localhost:4444/redirected'
});
}else{
if (repeat > 0){
res.writeHead(301, {
'Location':'http://localhost:4444/followRedirects?' + --repeat
});
}else{
res.writeHead(301, {
'Location':'http://localhost:4444/redirected'
});
}
}
res.end();
},
"/redirected":function(req, res){
var message={"redirected":++this.redirectCount};
res.writeHead(200, {
'Content-Type' : 'application/json; charset=utf-8'
});
res.write(JSON.stringify(message));
res.end();
}
},
"sleep" : function(ms) {
var stop = new Date().getTime();
while (new Date().getTime() < stop + ms) {
;
}
},
"redirectCount":0,
"redirectLimit":10
};
// 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);
};

View File

@ -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");
});
});

View File

@ -0,0 +1,70 @@
var server =require("../server/mock-server"),
Client=require("../../lib/node-rest-client").Client;
describe('Follows Redirects', function () {
this.timeout(150000);
before(function () {
server.listen(4444);
console.log("server started on port 4444");
});
describe("#Follows Redirects",function(){
it("follows Redirect", function(done){
var client = new Client();
client.post(server.baseURL + "/followRedirects", function(data, response){
data.should.not.equal(null);
data.should.type("object");
data.redirected.should.equal(1);
done();
});
});
it("disable follows Redirect", function(done){
var client = new Client();
var args ={
requestConfig:{followRedirects:false}
};
client.post(server.baseURL + "/followRedirects",args, function(data, response){
response.statusCode.should.be.equal(301);
done();
});
});
it("set max redirects", function(done){
var client = new Client();
var args ={
requestConfig:{maxRedirects:3}
};
var req = client.post(server.baseURL + "/followRedirects?5",args, function(data, response){
response.statusCode.should.be.equal(301);
});
req.on('error', function(err){
err.message.should.be.equal("Max redirects exceeded.")
done();
});
});
});
after(function () {
server.close();
console.log("server stopped");
});
});

View File

@ -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");
});
});

View File

@ -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:"<?xml version='1.0'?><testData><testNumber>123</testNumber><testString>123</testString></testData>"
};
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.should.be.equal("123");
data.testData.testString.should.be.equal("123");
done();
});
});
});
after(function () {
server.close();
console.log("server stopped");
});
});

View File

@ -0,0 +1,317 @@
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();
});
});

91
node_modules/node-rest-client/test/test-proxy.js generated vendored Normal file
View File

@ -0,0 +1,91 @@
var http = require('http');
var fs = require('fs');
var blacklist = [];
var iplist = [];
fs.watchFile('./blacklist', function(c,p) { update_blacklist(); });
fs.watchFile('./iplist', function(c,p) { update_iplist(); });
function update_blacklist() {
console.log("Updating blacklist.");
blacklist = fs.readFileSync('./blacklist', {encoding: 'utf-8'}).split('\n')
.filter(function(rx) { return rx.length })
.map(function(rx) { return RegExp(rx) });
}
function update_iplist() {
console.log("Updating iplist.");
iplist = fs.readFileSync('./iplist', {encoding: 'utf-8'}).split('\n')
.filter(function(rx) { return rx.length });
}
function ip_allowed(ip) {
for (i in iplist) {
if (iplist[i] == ip) {
return true;
}
}
return false;
}
function host_allowed(host) {
for (i in blacklist) {
if (blacklist[i].test(host)) {
return false;
}
}
return true;
}
function deny(response, msg) {
response.writeHead(401);
response.write(msg);
response.end();
}
http.createServer(function(request, response) {
var ip = request.connection.remoteAddress;
if (!ip_allowed(ip)) {
msg = "IP " + ip + " is not allowed to use this proxy";
deny(response, msg);
console.log(msg);
return;
}
if (!host_allowed(request.url)) {
msg = "Host " + request.url + " has been denied by proxy configuration";
deny(response, msg);
console.log(msg);
return;
}
console.log(ip + ": " + request.method + " " + request.url);
var agent = new http.Agent({ host: request.headers['host'], port: 80, maxSockets: 1 });
var proxy_request = http.request({
host: request.headers['host'],
port: 80,
method: request.method,
path: request.url,
headers: request.headers,
agent: agent
});
proxy_request.addListener('response', function(proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
response.end();
});
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
request.addListener('end', function() {
proxy_request.end();
});
}).listen(8080);
update_blacklist();
update_iplist();