├── .gitignore ├── LICENSE ├── debug.bat ├── debug.sh ├── lib ├── node-rest-client.js ├── nrc-parser-manager.js └── nrc-serializer-manager.js ├── package-lock.json ├── package.json ├── readme.md ├── test.bat ├── test.sh └── test ├── mocha.opts ├── server ├── message.json ├── message.xml └── mock-server.js ├── specs ├── TestErrorHandlers.js ├── TestFollowsRedirect.js ├── TestGETMethod.js ├── TestPOSTMethod.js └── TestRIOFacade.js └── test-proxy.js /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | test/man-test.js 3 | 4 | test/spec/test.js 5 | 6 | node_modules 7 | *.log 8 | 9 | test/multiple-clients-test.js 10 | 11 | test/manual-test.js 12 | 13 | .DS_Store 14 | 15 | .project 16 | mocha-eclipse.js 17 | node-rest-client.sublime-project 18 | node-rest-client.sublime-workspace -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Alejandro Alvarez Acero. All rights reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 4 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation 5 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 6 | to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 11 | THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 12 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 13 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /debug.bat: -------------------------------------------------------------------------------- 1 | start node-inspector 2 | start mocha --debug-brk -R spec %1% -------------------------------------------------------------------------------- /debug.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #set -x 3 | # find existing web inspector 4 | 5 | 6 | killprocess(){ 7 | 8 | echo ps -ef | awk "!/awk/ && /$1/ {print $2}" 9 | #pid=`ps -ef | awk -v keyword=$1 "!/awk/ && /${keyword}/ {print $2}"`; 10 | pid=`ps -ef | awk -v a=node-inspector '!/awk/ && /${a}/ {print $2}'`; 11 | echo current $1 process is $pid; 12 | 13 | if [ -n '$pid' ] 14 | then 15 | echo killing $1 process $pid; 16 | #kill $pid; 17 | 18 | else 19 | echo $1 is not active; 20 | 21 | fi 22 | } 23 | 24 | 25 | 26 | 27 | killprocess node-inspector 28 | killprocess mocha 29 | 30 | echo launching node-inspector 31 | node-inspector & 32 | 33 | echo launching test $1 34 | mocha --debug-brk -R spec $1 & 35 | 36 | 37 | -------------------------------------------------------------------------------- /lib/node-rest-client.js: -------------------------------------------------------------------------------- 1 | var http = require('follow-redirects').http, 2 | https = require('follow-redirects').https, 3 | urlParser = require('url'), 4 | util = require("util"), 5 | events = require("events"), 6 | zlib = require("zlib"), 7 | node_debug = require("debug")("NRC"); 8 | 9 | exports.Client = function (options){ 10 | var self = this, 11 | // parser response manager 12 | parserManager = require("./nrc-parser-manager")(), 13 | serializerManager = require("./nrc-serializer-manager")(), 14 | // connection manager 15 | connectManager = new ConnectManager(this, parserManager), 16 | // io facade to parsers and serailiazers 17 | ioFacade = function(parserManager, serializerManager){ 18 | // error execution context 19 | var errorContext = function(logic){ 20 | return function(){ 21 | try{ 22 | return logic.apply(this, arguments); 23 | }catch(err){ 24 | self.emit('error',err); 25 | } 26 | }; 27 | }, 28 | result={"parsers":{}, "serializers":{}}; 29 | 30 | // parsers facade 31 | result.parsers.add = errorContext(parserManager.add); 32 | result.parsers.remove = errorContext(parserManager.remove); 33 | result.parsers.find = errorContext(parserManager.find); 34 | result.parsers.getAll = errorContext(parserManager.getAll); 35 | result.parsers.getDefault = errorContext(parserManager.getDefault); 36 | result.parsers.clean = errorContext(parserManager.clean); 37 | 38 | // serializers facade 39 | result.serializers.add = errorContext(serializerManager.add); 40 | result.serializers.remove = errorContext(serializerManager.remove); 41 | result.serializers.find = errorContext(serializerManager.find); 42 | result.serializers.getAll = errorContext(serializerManager.getAll); 43 | result.serializers.getDefault = errorContext(serializerManager.getDefault); 44 | result.serializers.clean = errorContext(serializerManager.clean); 45 | 46 | return result; 47 | 48 | }(parserManager,serializerManager), 49 | // declare util constants 50 | CONSTANTS={ 51 | HEADER_CONTENT_LENGTH:"Content-Length" 52 | }; 53 | 54 | 55 | self.options = options || {}, 56 | self.useProxy = (self.options.proxy || false)?true:false, 57 | self.useProxyTunnel = (!self.useProxy || self.options.proxy.tunnel===undefined)?false:self.options.proxy.tunnel, 58 | self.proxy = self.options.proxy, 59 | self.connection = self.options.connection || {}, 60 | self.mimetypes = self.options.mimetypes || {}, 61 | self.requestConfig = self.options.requestConfig || {}, 62 | self.responseConfig = self.options.responseConfig || {}; 63 | 64 | // namespaces for methods, parsers y serializers 65 | this.methods={}; 66 | this.parsers={}; 67 | this.serializers={}; 68 | 69 | // Client Request to be passed to ConnectManager and returned 70 | // for each REST method invocation 71 | var ClientRequest =function(){ 72 | events.EventEmitter.call(this); 73 | }; 74 | 75 | 76 | util.inherits(ClientRequest, events.EventEmitter); 77 | 78 | 79 | ClientRequest.prototype.end = function(){ 80 | if(this._httpRequest) { 81 | this._httpRequest.end(); 82 | } 83 | }; 84 | 85 | ClientRequest.prototype.setHttpRequest=function(req){ 86 | this._httpRequest = req; 87 | }; 88 | 89 | 90 | 91 | var Util = { 92 | createProxyPath:function(url){ 93 | var result = url.host; 94 | // check url protocol to set path in request options 95 | if (url.protocol === "https:"){ 96 | // port is set, leave it, otherwise use default https 443 97 | result = (url.host.indexOf(":") == -1?url.hostname + ":443":url.host); 98 | } 99 | 100 | return result; 101 | }, 102 | createProxyHeaders:function(url){ 103 | var result ={}; 104 | // if proxy requires authentication, create Proxy-Authorization headers 105 | if (self.proxy.user && self.proxy.password){ 106 | result["Proxy-Authorization"] = "Basic " + new Buffer([self.proxy.user,self.proxy.password].join(":")).toString("base64"); 107 | } 108 | // no tunnel proxy connection, we add the host to the headers 109 | if(!self.useProxyTunnel) 110 | result["host"] = url.host; 111 | 112 | return result; 113 | }, 114 | createConnectOptions:function(connectURL, connectMethod){ 115 | debug("connect URL = ", connectURL); 116 | var url = urlParser.parse(connectURL), 117 | path, 118 | result={}, 119 | protocol = url.protocol.indexOf(":") == -1?url.protocol:url.protocol.substring(0,url.protocol.indexOf(":")), 120 | defaultPort = protocol === 'http'?80:443; 121 | 122 | result ={ 123 | host: url.host.indexOf(":") == -1?url.host:url.host.substring(0,url.host.indexOf(":")), 124 | port: url.port === undefined?defaultPort:url.port, 125 | path: url.path, 126 | protocol:protocol, 127 | href:url.href 128 | }; 129 | 130 | if (self.useProxy) result.agent = false; // cannot use default 131 | // agent in proxy mode 132 | 133 | if (self.options.user && self.options.password){ 134 | result.auth = [self.options.user,self.options.password].join(":"); 135 | 136 | } else if (self.options.user && !self.options.password){ 137 | // some sites only needs user with no password to 138 | // authenticate 139 | result.auth = self.options.user + ":"; 140 | } 141 | 142 | // configure proxy connection to establish a tunnel 143 | if (self.useProxy){ 144 | 145 | result.proxy ={ 146 | host: self.proxy.host, 147 | port: self.proxy.port, 148 | method: self.useProxyTunnel?'CONNECT':connectMethod,// if 149 | // proxy 150 | // tunnel 151 | // use 152 | // 'CONNECT' 153 | // method, 154 | // else 155 | // get 156 | // method 157 | // from 158 | // request, 159 | path: self.useProxyTunnel?this.createProxyPath(url):connectURL, // if 160 | // proxy 161 | // tunnel 162 | // set 163 | // proxy 164 | // path 165 | // else 166 | // get 167 | // request 168 | // path, 169 | headers: this.createProxyHeaders(url) // createProxyHeaders 170 | // add correct 171 | // headers depending 172 | // of proxy 173 | // connection type 174 | }; 175 | } 176 | 177 | if(self.connection && typeof self.connection === 'object'){ 178 | for(var option in self.connection){ 179 | result[option] = self.connection[option]; 180 | } 181 | } 182 | 183 | // don't use tunnel to connect to proxy, direct request 184 | // and delete proxy options 185 | if (!self.useProxyTunnel){ 186 | for (var proxyOption in result.proxy){ 187 | result[proxyOption] = result.proxy[proxyOption]; 188 | } 189 | 190 | delete result.proxy; 191 | } 192 | 193 | // add general request and response config to connect options 194 | 195 | result.requestConfig = self.requestConfig; 196 | result.responseConfig = self.responseConfig; 197 | 198 | 199 | return result; 200 | }, 201 | decodeQueryFromURL: function(connectURL){ 202 | var url = urlParser.parse(connectURL), 203 | query = url.query.substring(1).split("&"), 204 | keyValue, 205 | result={}; 206 | 207 | // create decoded args from key value elements in query+ 208 | for (var i=0;i= 0 && pathParameterSepCharPos!== pathLength -1 ) 332 | self.emit('error','parameters argument cannot be used if parameters are already defined in URL ' + options.path); 333 | 334 | options.path +=(options.path.charAt(pathLength-1) === '?'?"":"?"); 335 | // check if we have serializable parameter container, that 336 | // must be serialized and encoded 337 | // directly, as javascript object 338 | options.path = options.path.concat(Util.serializeEncodeQueryFromArgs(args.parameters)); 339 | debug("options.path after request parameters = ", options.path); 340 | } 341 | 342 | // override client config, by the moment just for request 343 | // response config 344 | this.overrideClientConfig(options,args); 345 | 346 | // always set Content-length header if not set previously 347 | // set Content lentgh for some servers to work (nginx, apache) 348 | if (args.data !== undefined && !options.headers.hasOwnProperty(CONSTANTS.HEADER_CONTENT_LENGTH)){ 349 | serializerManager.get(options).serialize(args.data, clientEmitterWrapper(self), function(serializedData){ 350 | options.data = serializedData; 351 | options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = Buffer.byteLength(options.data, 'utf8'); 352 | }); 353 | }else{ 354 | options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0; 355 | } 356 | 357 | 358 | } 359 | 360 | 361 | debug("options post connect",options); 362 | debug("FINAL SELF object ====>", self); 363 | 364 | if (self.useProxy && self.useProxyTunnel){ 365 | connectManager.proxy(options,callback); 366 | }else{ 367 | // normal connection and direct proxy connections (no tunneling) 368 | connectManager.normal(options,callback); 369 | } 370 | }, 371 | mergeMimeTypes:function(mimetypes){ 372 | // this function is left for backward compatibility, but will be 373 | // deleted in future releases 374 | var parser = null; 375 | // merge mime-types passed as options to parsers 376 | if (mimetypes && typeof mimetypes === "object"){ 377 | try{ 378 | if (mimetypes.json && mimetypes.json instanceof Array && mimetypes.json.length > 0){ 379 | parser = parserManager.find("JSON"); 380 | parser.contentTypes = mimetypes.json; 381 | }else if (mimetypes.xml && mimetypes.xml instanceof Array && mimetypes.xml.length > 0){ 382 | parser = parserManager.find("XML"); 383 | parser.contentTypes = mimetypes.xml; 384 | } 385 | }catch(err){ 386 | self.emit('error', 'cannot assign custom content types to parser, cause: ' + err); 387 | } 388 | } 389 | }, 390 | createHttpMethod:function(methodName){ 391 | return function(url, args, callback){ 392 | var clientRequest = new ClientRequest(); 393 | Util.connect(methodName.toUpperCase(), url, args, callback, clientRequest); 394 | return clientRequest; 395 | }; 396 | } 397 | }, 398 | Method = function(url, method){ 399 | var httpMethod = self[method.toLowerCase()]; 400 | 401 | return function(args,callback){ 402 | var completeURL = url; 403 | // no args 404 | if (typeof args === 'function'){ 405 | callback = args; 406 | args = {}; 407 | } 408 | 409 | return httpMethod(completeURL, args , callback); 410 | }; 411 | }; 412 | 413 | 414 | 415 | 416 | this.get = Util.createHttpMethod("get"); 417 | 418 | this.post = Util.createHttpMethod("post"); 419 | 420 | this.put = Util.createHttpMethod("put"); 421 | 422 | this.delete = Util.createHttpMethod("delete"); 423 | 424 | this.patch = Util.createHttpMethod("patch"); 425 | 426 | 427 | this.registerMethod = function(name, url, method){ 428 | // create method in method registry with preconfigured REST invocation 429 | // method 430 | this.methods[name] = new Method(url,method); 431 | }; 432 | 433 | this.unregisterMethod = function(name){ 434 | delete this.methods[name]; 435 | }; 436 | 437 | this.addCustomHttpMethod=function(methodName){ 438 | self[methodName.toLowerCase()] = Util.createHttpMethod(methodName); 439 | }; 440 | 441 | this.parsers = ioFacade.parsers; 442 | 443 | this.serializers = ioFacade.serializers; 444 | 445 | // merge mime types with connect manager 446 | Util.mergeMimeTypes(self.mimetypes); 447 | debug("ConnectManager", connectManager); 448 | 449 | }; 450 | 451 | 452 | var ConnectManager = function(client, parserManager) { 453 | 454 | var client = client, 455 | clientEmitterWrapper = function (client){ 456 | var client = client; 457 | return function(type, event){client.emit(type, event);}; 458 | }; 459 | 460 | 461 | 462 | this.configureRequest = function(req, config, clientRequest){ 463 | 464 | if (config.timeout){ 465 | req.setTimeout(config.timeout, function(){ 466 | clientRequest.emit('requestTimeout',req); 467 | }); 468 | } 469 | 470 | 471 | if(config.noDelay) 472 | req.setNoDelay(config.noDelay); 473 | 474 | if(config.keepAlive) 475 | req.setSocketKeepAlive(config.noDelay,config.keepAliveDelay || 0); 476 | 477 | }; 478 | 479 | this.configureResponse = function(res,config, clientRequest){ 480 | if (config.timeout){ 481 | res.setTimeout(config.timeout, function(){ 482 | clientRequest.emit('responseTimeout',res); 483 | res.close(); 484 | }); 485 | } 486 | }; 487 | 488 | this.configureOptions = function(options){ 489 | var followRedirectsProps =["followRedirects", "maxRedirects"]; 490 | function configureProps(propsArray, optionsElement){ 491 | for (var index in propsArray){ 492 | if (optionsElement.hasOwnProperty(propsArray[index])) 493 | options[propsArray[index]] = optionsElement[propsArray[index]]; 494 | } 495 | } 496 | 497 | //add follows-redirects config 498 | configureProps(followRedirectsProps, options.requestConfig); 499 | 500 | 501 | // remove "protocol" and "clientRequest" option from options, 502 | // cos is not allowed by http/hppts node objects 503 | delete options.protocol; 504 | delete options.clientRequest; 505 | delete options.requestConfig; 506 | delete options.responseConfig; 507 | debug("options pre connect", options); 508 | }; 509 | 510 | this.handleEnd = function(res,buffer,callback){ 511 | 512 | var self = this, 513 | content = res.headers["content-type"], 514 | encoding = res.headers["content-encoding"]; 515 | 516 | debug("content-type: ", content); 517 | debug("content-encoding: ",encoding); 518 | 519 | if(encoding !== undefined && encoding.indexOf("gzip") >= 0){ 520 | debug("gunzip"); 521 | zlib.gunzip(Buffer.concat(buffer),function(er,gunzipped){ 522 | self.handleResponse(res,gunzipped,callback); 523 | }); 524 | }else if(encoding !== undefined && encoding.indexOf("deflate") >= 0){ 525 | debug("inflate"); 526 | zlib.inflate(Buffer.concat(buffer),function(er,inflated){ 527 | self.handleResponse(res,inflated,callback); 528 | }); 529 | }else { 530 | debug("not compressed"); 531 | self.handleResponse(res,Buffer.concat(buffer),callback); 532 | } 533 | }; 534 | 535 | this.handleResponse = function(res,data,callback){ 536 | // find valid parser to be used with response content type, first one 537 | // found 538 | parserManager.get(res).parse(data, clientEmitterWrapper(client), function(parsedData){ 539 | callback(parsedData,res); 540 | }); 541 | }; 542 | 543 | this.prepareData = function(data){ 544 | var result; 545 | if ((data instanceof Buffer) || (typeof data !== 'object')){ 546 | result = data; 547 | }else{ 548 | result = JSON.stringify(data); 549 | } 550 | return result; 551 | }; 552 | 553 | this.proxy = function(options, callback){ 554 | 555 | debug("proxy options",options.proxy); 556 | 557 | // creare a new proxy tunnel, and use to connect to API URL 558 | var proxyTunnel = http.request(options.proxy), 559 | self = this; 560 | 561 | 562 | proxyTunnel.on('connect',function(res, socket, head){ 563 | debug("proxy connected",socket); 564 | 565 | // set tunnel socket in request options, that's the tunnel 566 | // itself 567 | options.socket = socket; 568 | 569 | var buffer=[], 570 | protocol = (options.protocol =="http")?http:https, 571 | clientRequest = options.clientRequest, 572 | requestConfig = options.requestConfig, 573 | responseConfig = options.responseConfig; 574 | 575 | self.configureOptions(options); 576 | 577 | // add request options to request returned to calling method 578 | clientRequest.options = options; 579 | 580 | var request = protocol.request(options, function(res){ 581 | // configure response 582 | self.configureResponse(res,responseConfig, clientRequest); 583 | 584 | // concurrent data chunk handler 585 | res.on('data',function(chunk){ 586 | buffer.push(Buffer.from(chunk)); 587 | }); 588 | 589 | res.on('end',function(){ 590 | self.handleEnd(res,buffer,callback); 591 | }); 592 | 593 | 594 | // handler response errors 595 | res.on('error',function(err){ 596 | if (clientRequest !== undefined && typeof clientRequest === 'object'){ 597 | // add request as property of error 598 | err.request = clientRequest; 599 | err.response = res; 600 | // request error handler 601 | clientRequest.emit('error',err); 602 | }else{ 603 | // general error handler 604 | client.emit('error',err); 605 | } 606 | }); 607 | }); 608 | 609 | 610 | 611 | // configure request and add it to clientRequest 612 | // and add it to request returned 613 | self.configureRequest(request,requestConfig, clientRequest); 614 | clientRequest.setHttpRequest(request); 615 | 616 | 617 | // write POST/PUT data to request body; 618 | // find valid serializer to be used to serialize request data, 619 | // first one found 620 | // is the one to be used.if none found for match condition, 621 | // default serializer is used 622 | 623 | if(options.data)request.write(options.data); 624 | 625 | request.end(); 626 | 627 | 628 | // handle request errors and handle them by request or general 629 | // error handler 630 | request.on('error',function(err){ 631 | if (clientRequest !== undefined && typeof clientRequest === 'object'){ 632 | // add request as property of error 633 | err.request = clientRequest; 634 | 635 | // request error handler 636 | clientRequest.emit('error',err); 637 | }else{ 638 | // general error handler 639 | client.emit('error',err); 640 | } 641 | }); 642 | }); 643 | 644 | // proxy tunnel error are only handled by general error handler 645 | proxyTunnel.on('error',function(e){ 646 | client.emit('error',e); 647 | }); 648 | 649 | proxyTunnel.end(); 650 | 651 | }; 652 | 653 | this.normal = function(options, callback){ 654 | 655 | var buffer = [], 656 | protocol = (options.protocol === "http")?http:https, 657 | clientRequest = options.clientRequest, 658 | requestConfig = options.requestConfig, 659 | responseConfig = options.responseConfig, 660 | self = this; 661 | 662 | self.configureOptions(options); 663 | 664 | // add request options to request returned to calling method 665 | clientRequest.options = options; 666 | 667 | var request = protocol.request(options, function(res){ 668 | // configure response 669 | self.configureResponse(res,responseConfig, clientRequest); 670 | 671 | // concurrent data chunk handler 672 | res.on('data',function(chunk){ 673 | buffer.push(Buffer.from(chunk)); 674 | }); 675 | 676 | res.on('end',function(){ 677 | 678 | self.handleEnd(res,buffer,callback); 679 | 680 | }); 681 | 682 | // handler response errors 683 | res.on('error',function(err){ 684 | if (clientRequest !== undefined && typeof clientRequest === 'object'){ 685 | // add request as property of error 686 | err.request = clientRequest; 687 | err.response = res; 688 | // request error handler 689 | clientRequest.emit('error',err); 690 | }else{ 691 | // general error handler 692 | client.emit('error',err); 693 | } 694 | }); 695 | }); 696 | 697 | // configure request and add it to clientRequest 698 | // and add it to request returned 699 | self.configureRequest(request,requestConfig, clientRequest); 700 | debug("clientRequest",clientRequest); 701 | 702 | clientRequest.setHttpRequest(request); 703 | 704 | debug("options data", options.data); 705 | // write POST/PUT data to request body; 706 | // find valid serializer to be used to serialize request data, 707 | // first one found 708 | // is the one to be used.if none found for match condition, 709 | // default serializer is used 710 | if(options.data)request.write(options.data); 711 | request.end(); // end request when data is written 712 | 713 | // handle request errors and handle them by request or general 714 | // error handler 715 | request.on('error',function(err){ 716 | if (clientRequest !== undefined && typeof clientRequest === 'object'){ 717 | // add request as property of error 718 | err.request = clientRequest; 719 | 720 | // request error handler 721 | clientRequest.emit('error',err); 722 | }else{ 723 | // general error handler 724 | client.emit('error',err); 725 | } 726 | }); 727 | }; 728 | }; 729 | 730 | 731 | // event handlers for client and ConnectManager 732 | util.inherits(exports.Client, events.EventEmitter); 733 | 734 | 735 | var debug = function(){ 736 | if (!process.env.DEBUG) return; 737 | 738 | var now = new Date(), 739 | header =now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " [NRC CLIENT]" + arguments.callee.caller.name + " -> ", 740 | args = Array.prototype.slice.call(arguments); 741 | args.splice(0,0,header); 742 | node_debug.apply(console,args); 743 | 744 | 745 | }; 746 | -------------------------------------------------------------------------------- /lib/nrc-parser-manager.js: -------------------------------------------------------------------------------- 1 | var ParserManager = function(){ 2 | var registry={}, defaultParser = null; 3 | 4 | var _private={ 5 | "validate":function(parser){ 6 | 7 | function validateProperties(parser, props){ 8 | var result = true; 9 | for (var propIndex in props){ 10 | var propType = props[propIndex].split(":"); 11 | if (!parser.hasOwnProperty([propType[0]]) || typeof parser[propType[0]] !== propType[1]){ 12 | result = false; 13 | break; 14 | } 15 | } 16 | 17 | return result; 18 | } 19 | 20 | 21 | result = validateProperties(parser,["name:string","parse:function","isDefault:boolean"]); 22 | 23 | // valid parser, check if its not default response parser, to validate non 24 | // default parser props 25 | if (result && !parser.isDefault) 26 | result = validateProperties(parser,["match:function"]); 27 | 28 | 29 | return result; 30 | } 31 | }; 32 | 33 | this.add = function(parser){ 34 | if (!_private.validate(parser)) 35 | throw "parser cannot be added: invalid parser definition"; 36 | 37 | if (parser.isDefault){ 38 | defaultParser = parser; 39 | }else{ 40 | registry[parser.name] = parser; 41 | } 42 | }; 43 | 44 | this.remove = function(parserName){ 45 | var result = registry[parserName]; 46 | if (!result) 47 | throw "cannot remove parser: " + parserName +" doesn't exists"; 48 | 49 | delete registry[parserName]; 50 | }; 51 | 52 | this.clean = function(){ 53 | registry={}; 54 | }; 55 | 56 | this.find = function(parserName){ 57 | var result = registry[parserName]; 58 | if (!result) 59 | throw "cannot find parser: " + parserName + " doesn't exists "; 60 | 61 | return result; 62 | }; 63 | 64 | this.getDefault = function(){ 65 | return defaultParser; 66 | }; 67 | 68 | this.get = function(response){ 69 | var result = null; 70 | for (var parserName in registry){ 71 | if (registry[parserName].match(response)){ 72 | result = registry[parserName]; 73 | break; 74 | } 75 | } 76 | // if parser not found return default parser, else parser found 77 | return (result === null)?defaultParser:result; 78 | }; 79 | 80 | this.getAll=function(){ 81 | var result=[]; 82 | for (var parserName in registry){ 83 | result.push(registry[parserName]); 84 | } 85 | return result; 86 | } 87 | }; 88 | 89 | 90 | module.exports = function(){ 91 | 92 | var parserManager = new ParserManager(); 93 | 94 | var BaseParser = { 95 | "isDefault":false, 96 | "match":function(response){ 97 | var result = false, 98 | contentType = response.headers["content-type"] && response.headers["content-type"].replace(/ /g, ''); 99 | 100 | if (!contentType) return result; 101 | 102 | for (var i=0; i 0); 129 | }, 130 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ 131 | var jsonData, 132 | data = byteBuffer.toString(); 133 | 134 | try { 135 | jsonData = this.isValidData(data)?JSON.parse(data):data; 136 | } catch (err) { 137 | // Something went wrong when parsing json. This can happen 138 | // for many reasons, including a bad implementation on the 139 | // server. 140 | nrcEventEmitter('error','Error parsing response. response: [' +data + '], error: [' + err + ']'); 141 | } 142 | parsedCallback(jsonData); 143 | } 144 | },BaseParser)); 145 | 146 | 147 | parserManager.add({ 148 | "name":"DEFAULT", 149 | "isDefault":true, 150 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ 151 | parsedCallback(byteBuffer); 152 | } 153 | }); 154 | 155 | return parserManager; 156 | } 157 | -------------------------------------------------------------------------------- /lib/nrc-serializer-manager.js: -------------------------------------------------------------------------------- 1 | var xmlserializer = require('xml2js'); 2 | 3 | var SerializerManager = function(){ 4 | var registry={}, defaultSerializer = null; 5 | 6 | var _private={ 7 | "validate":function(serializer){ 8 | 9 | function validateProperties(serializer, props){ 10 | var result = true; 11 | for (var propIndex in props){ 12 | var propType = props[propIndex].split(":"); 13 | if (!serializer.hasOwnProperty([propType[0]]) || typeof serializer[propType[0]] !== propType[1]){ 14 | result = false; 15 | break; 16 | } 17 | } 18 | 19 | return result; 20 | } 21 | 22 | 23 | result = validateProperties(serializer,["name:string","serialize:function","isDefault:boolean"]); 24 | 25 | // valid serializer, check if its not default request serializer, to validate non 26 | // default serializer props 27 | if (result && !serializer.isDefault) 28 | result = validateProperties(serializer,["match:function"]); 29 | 30 | 31 | return result; 32 | } 33 | }; 34 | 35 | this.add = function(serializer){ 36 | if (!_private.validate(serializer)) 37 | throw "serializer cannot be added: invalid serializer definition"; 38 | 39 | if (serializer.isDefault){ 40 | defaultSerializer = serializer; 41 | }else{ 42 | registry[serializer.name] = serializer; 43 | } 44 | }; 45 | 46 | this.remove = function(serializerName){ 47 | var result = registry[serializerName]; 48 | if (!result) 49 | throw "cannot remove serializer: " + serializerName +" doesn't exists"; 50 | 51 | delete registry[serializerName]; 52 | }; 53 | 54 | this.find = function(serializerName){ 55 | var result = registry[serializerName]; 56 | if (!result) 57 | throw "cannot find serializer: " + serializerName +" doesn't exists"; 58 | 59 | return result; 60 | }; 61 | 62 | 63 | this.clean = function(){ 64 | registry={}; 65 | }; 66 | 67 | this.get = function(request){ 68 | var result = null; 69 | for (var serializerName in registry){ 70 | if (registry[serializerName].match(request)){ 71 | result = registry[serializerName]; 72 | break; 73 | } 74 | } 75 | // if serializer not found return default serializer, else serializer found 76 | return (result === null)?defaultSerializer:result; 77 | }; 78 | 79 | this.getAll=function(){ 80 | var result = []; 81 | for (var serializerName in registry){ 82 | result.push(registry[serializerName]); 83 | } 84 | return result; 85 | }; 86 | 87 | this.getDefault = function(){ 88 | return defaultSerializer; 89 | }; 90 | }; 91 | 92 | 93 | 94 | 95 | module.exports = function(){ 96 | 97 | var serializerManager = new SerializerManager(); 98 | 99 | var BaseSerializer ={ 100 | "isDefault":false, 101 | "match":function(request){ 102 | var result = false, 103 | contentType = request.headers["Content-Type"] && request.headers["Content-Type"].replace(/ /g, ''); 104 | 105 | if (!contentType) return result; 106 | 107 | for (var i=0; i=0.6.0", 696 | "xmlbuilder": "~11.0.0" 697 | } 698 | }, 699 | "xmlbuilder": { 700 | "version": "11.0.1", 701 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", 702 | "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" 703 | }, 704 | "y18n": { 705 | "version": "5.0.8", 706 | "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", 707 | "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", 708 | "dev": true 709 | }, 710 | "yargs": { 711 | "version": "16.2.0", 712 | "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", 713 | "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", 714 | "dev": true, 715 | "requires": { 716 | "cliui": "^7.0.2", 717 | "escalade": "^3.1.1", 718 | "get-caller-file": "^2.0.5", 719 | "require-directory": "^2.1.1", 720 | "string-width": "^4.2.0", 721 | "y18n": "^5.0.5", 722 | "yargs-parser": "^20.2.2" 723 | } 724 | }, 725 | "yargs-parser": { 726 | "version": "20.2.4", 727 | "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.4.tgz", 728 | "integrity": "sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==", 729 | "dev": true 730 | }, 731 | "yargs-unparser": { 732 | "version": "2.0.0", 733 | "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz", 734 | "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==", 735 | "dev": true, 736 | "requires": { 737 | "camelcase": "^6.0.0", 738 | "decamelize": "^4.0.0", 739 | "flat": "^5.0.2", 740 | "is-plain-obj": "^2.1.0" 741 | } 742 | }, 743 | "yocto-queue": { 744 | "version": "0.1.0", 745 | "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", 746 | "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", 747 | "dev": true 748 | } 749 | } 750 | } 751 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Alejandro Alvarez Acero", 3 | "name": "node-rest-client", 4 | "description": "node API REST client", 5 | "version": "3.1.1", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/aacerox/node-rest-client.git" 9 | }, 10 | "main": "./lib/node-rest-client", 11 | "dependencies": { 12 | "xml2js": ">=0.4.23", 13 | "debug": "~4.3.3", 14 | "follow-redirects": ">=1.14.7" 15 | }, 16 | "devDependencies": { 17 | "mocha": "*", 18 | "should": ">= 0.0.1" 19 | }, 20 | "optionalDependencies": {}, 21 | "engines": { 22 | "node": "*" 23 | }, 24 | "scripts": { 25 | "test": "mocha" 26 | }, 27 | "license": "MIT" 28 | } 29 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # REST Client for Node.js 2 | [![npm version](https://badge.fury.io/js/node-rest-client.svg)](https://www.npmjs.com/package/node-rest-client) 3 | [![Build Status](https://travis-ci.org/olalonde/node-rest-client.svg?branch=master)](https://travis-ci.org/olalonde/node-rest-client) 4 | 5 | [![NPM](https://nodei.co/npm/node-rest-client.png?downloads=true)](https://nodei.co/npm/node-rest-client.png?downloads=true) 6 | 7 | ## Features 8 | 9 | Allows connecting to any API REST and get results as js Object. The client has the following features: 10 | 11 | - Transparent HTTP/HTTPS connection to remote API sites. 12 | - Allows simple HTTP basic authentication. 13 | - Allows most common HTTP operations: GET, POST, PUT, DELETE, PATCH or any other method through custom connect method 14 | - Allows creation of custom HTTP Methods (PURGE, etc.) 15 | - Direct or through proxy connection to remote API sites. 16 | - Register remote API operations as own client methods, simplifying reuse. 17 | - Dynamic path and query parameters and request headers. 18 | - Improved Error handling mechanism (client or specific request) 19 | - Added support for compressed responses: gzip and deflate 20 | - Added support for follow redirects thanks to great [follow-redirects](https://www.npmjs.com/package/follow-redirects) package 21 | - Added support for custom request serializers (json,xml and url-encoded included by default) 22 | - Added support for custom response parsers (json and xml included by default) 23 | 24 | 25 | 26 | ## Installation 27 | 28 | $ npm install node-rest-client 29 | 30 | ## Usages 31 | 32 | ### Simple HTTP GET 33 | 34 | Client has two ways to call a REST service: direct or using registered methods 35 | 36 | ```javascript 37 | var Client = require('node-rest-client').Client; 38 | 39 | var client = new Client(); 40 | 41 | // direct way 42 | client.get("http://remote.site/rest/xml/method", function (data, response) { 43 | // parsed response body as js object 44 | console.log(data); 45 | // raw response 46 | console.log(response); 47 | }); 48 | 49 | // registering remote methods 50 | client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET"); 51 | 52 | client.methods.jsonMethod(function (data, response) { 53 | // parsed response body as js object 54 | console.log(data); 55 | // raw response 56 | console.log(response); 57 | }); 58 | ``` 59 | 60 | ### HTTP POST 61 | 62 | 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: 63 | 64 | ```javascript 65 | //Example POST method invocation 66 | var Client = require('node-rest-client').Client; 67 | 68 | var client = new Client(); 69 | 70 | // set content-type header and data as json in args parameter 71 | var args = { 72 | data: { test: "hello" }, 73 | headers: { "Content-Type": "application/json" } 74 | }; 75 | 76 | client.post("http://remote.site/rest/xml/method", args, function (data, response) { 77 | // parsed response body as js object 78 | console.log(data); 79 | // raw response 80 | console.log(response); 81 | }); 82 | 83 | // registering remote methods 84 | client.registerMethod("postMethod", "http://remote.site/rest/json/method", "POST"); 85 | 86 | client.methods.postMethod(args, function (data, response) { 87 | // parsed response body as js object 88 | console.log(data); 89 | // raw response 90 | console.log(response); 91 | }); 92 | ``` 93 | If no "Content-Type" header is set as client arg POST,PUT and PATCH methods will not work properly. 94 | 95 | 96 | ### Passing args to registered methods 97 | 98 | You can pass diferents args to registered methods, simplifying reuse: path replace parameters, query parameters, custom headers 99 | 100 | ```javascript 101 | var Client = require('node-rest-client').Client; 102 | 103 | // direct way 104 | var client = new Client(); 105 | 106 | var args = { 107 | data: { test: "hello" }, // data passed to REST method (only useful in POST, PUT or PATCH methods) 108 | path: { "id": 120 }, // path substitution var 109 | parameters: { arg1: "hello", arg2: "world" }, // this is serialized as URL parameters 110 | headers: { "test-header": "client-api" } // request headers 111 | }; 112 | 113 | 114 | client.get("http://remote.site/rest/json/${id}/method", args, 115 | function (data, response) { 116 | // parsed response body as js object 117 | console.log(data); 118 | // raw response 119 | console.log(response); 120 | }); 121 | 122 | 123 | // registering remote methods 124 | client.registerMethod("jsonMethod", "http://remote.site/rest/json/${id}/method", "GET"); 125 | 126 | 127 | /* this would construct the following URL before invocation 128 | * 129 | * http://remote.site/rest/json/120/method?arg1=hello&arg2=world 130 | * 131 | */ 132 | client.methods.jsonMethod(args, function (data, response) { 133 | // parsed response body as js object 134 | console.log(data); 135 | // raw response 136 | console.log(response); 137 | }); 138 | ``` 139 | 140 | You can even use path placeholders in query string in direct connection: 141 | 142 | ```javascript 143 | var Client = require('node-rest-client').Client; 144 | 145 | // direct way 146 | var client = new Client(); 147 | 148 | var args = { 149 | path: { "id": 120, "arg1": "hello", "arg2": "world" }, 150 | headers: { "test-header": "client-api" } 151 | }; 152 | 153 | client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args, 154 | function (data, response) { 155 | // parsed response body as js object 156 | console.log(data); 157 | // raw response 158 | console.log(response); 159 | }); 160 | ``` 161 | 162 | ### HTTP POST and PUT methods 163 | 164 | To send data to remote site using POST or PUT methods, just add a data attribute to args object: 165 | 166 | ```javascript 167 | var Client = require('node-rest-client').Client; 168 | 169 | // direct way 170 | var client = new Client(); 171 | 172 | var args = { 173 | path: { "id": 120 }, 174 | parameters: { arg1: "hello", arg2: "world" }, 175 | headers: { "test-header": "client-api" }, 176 | data: "helloworld" 177 | }; 178 | 179 | client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) { 180 | // parsed response body as js object 181 | console.log(data); 182 | // raw response 183 | console.log(response); 184 | }); 185 | 186 | // registering remote methods 187 | client.registerMethod("xmlMethod", "http://remote.site/rest/xml/${id}/method", "POST"); 188 | 189 | 190 | client.methods.xmlMethod(args, function (data, response) { 191 | // parsed response body as js object 192 | console.log(data); 193 | // raw response 194 | console.log(response); 195 | }); 196 | 197 | // posted data can be js object 198 | var args_js = { 199 | path: { "id": 120 }, 200 | parameters: { arg1: "hello", arg2: "world" }, 201 | headers: { "test-header": "client-api" }, 202 | data: { "arg1": "hello", "arg2": 123 } 203 | }; 204 | 205 | client.methods.xmlMethod(args_js, function (data, response) { 206 | // parsed response body as js object 207 | console.log(data); 208 | // raw response 209 | console.log(response); 210 | }); 211 | ``` 212 | 213 | ### Request/Response configuration 214 | 215 | It's also possible to configure each request and response, passing its configuration as an 216 | additional argument in method call. 217 | 218 | ```javascript 219 | var client = new Client(); 220 | 221 | // request and response additional configuration 222 | var args = { 223 | path: { "id": 120 }, 224 | parameters: { arg1: "hello", arg2: "world" }, 225 | headers: { "test-header": "client-api" }, 226 | data: "helloworld", 227 | requestConfig: { 228 | timeout: 1000, //request timeout in milliseconds 229 | noDelay: true, //Enable/disable the Nagle algorithm 230 | keepAlive: true, //Enable/disable keep-alive functionalityidle socket. 231 | keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent 232 | }, 233 | responseConfig: { 234 | timeout: 1000 //response timeout 235 | } 236 | }; 237 | 238 | 239 | client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) { 240 | // parsed response body as js object 241 | console.log(data); 242 | // raw response 243 | console.log(response); 244 | }); 245 | ``` 246 | If you want to handle timeout events both in the request and in the response just add a new "requestTimeout" 247 | or "responseTimeout" event handler to clientRequest returned by method call. 248 | 249 | ```javascript 250 | var client = new Client(); 251 | 252 | // request and response additional configuration 253 | var args = { 254 | path: { "id": 120 }, 255 | parameters: { arg1: "hello", arg2: "world" }, 256 | headers: { "test-header": "client-api" }, 257 | data: "helloworld", 258 | requestConfig: { 259 | timeout: 1000, //request timeout in milliseconds 260 | noDelay: true, //Enable/disable the Nagle algorithm 261 | keepAlive: true, //Enable/disable keep-alive functionalityidle socket. 262 | keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent 263 | }, 264 | responseConfig: { 265 | timeout: 1000 //response timeout 266 | } 267 | }; 268 | 269 | 270 | var req = client.post("http://remote.site/rest/xml/${id}/method", args, function (data, response) { 271 | // parsed response body as js object 272 | console.log(data); 273 | // raw response 274 | console.log(response); 275 | }); 276 | 277 | req.on('requestTimeout', function (req) { 278 | console.log('request has expired'); 279 | req.abort(); 280 | }); 281 | 282 | req.on('responseTimeout', function (res) { 283 | console.log('response has expired'); 284 | 285 | }); 286 | 287 | //it's usefull to handle request errors to avoid, for example, socket hang up errors on request timeouts 288 | req.on('error', function (err) { 289 | console.log('request error', err); 290 | }); 291 | ``` 292 | 293 | ### Setup client to trust self-signed certificate with custom CA chain 294 | In Internet mostly recommend solution to handle self-signed certificate is to just disable verification of server. 295 | **NEVER DO THAT IN PRODUCTION!** 296 | You can do that only for development purpose - never in production because it puts great security risk on your business. 297 | 298 | 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. 299 | So for example certificate chain: 300 | 301 | ``` 302 | +-- root-CA (self-signed) 303 | | +-- department-CA (singed with root-CA) 304 | | +-- domain (signed with department-CA) 305 | ``` 306 | 307 | a solution is as follow: 308 | 309 | ```javascript 310 | var fs = required('fs'); 311 | var trustedCertificates = [ 312 | fs.readFileSync('/PATH/TO/DOMAIN/CERTIFICATE'), 313 | fs.readFileSync('/PATH/TO/DEPARTMENT/CA'), 314 | fs.readFileSync('/PATH/TO/ROOT/CA') 315 | ]; 316 | 317 | var options = { 318 | connection: { 319 | ca: trustedCertificates 320 | } 321 | }; 322 | var client = new Client(options); 323 | ``` 324 | 325 | Note that for readability format of certificate are skipped as multiple ones are supported. 326 | 327 | ### Follows Redirect 328 | 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 329 | ```javascript 330 | var client = new Client(); 331 | 332 | // request and response additional configuration 333 | var args = { 334 | requestConfig: { 335 | followRedirects:true,//whether redirects should be followed(default,true) 336 | maxRedirects:10//set max redirects allowed (default:21) 337 | }, 338 | responseConfig: { 339 | timeout: 1000 //response timeout 340 | } 341 | }; 342 | 343 | ``` 344 | 345 | 346 | ### Response Parsers 347 | 348 | You can add your own response parsers to client, as many as you want. There are 2 parser types: 349 | 350 | - _**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. 351 | 352 | - _**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. 353 | 354 | Each parser - regular or default- needs to follow some conventions: 355 | 356 | * Must be and object 357 | 358 | * Must have the following attributes: 359 | 360 | * `name`: Used to identify parser in parsers registry 361 | 362 | * `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 363 | 364 | * Must have the following methods: 365 | 366 | * `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: 367 | 1. `response`:`http.ServerResponse`: you can use any argument available in node ServerResponse, for example `headers` 368 | 369 | * `parse(byteBuffer,nrcEventEmitter,parsedCallback)` : this method is where response body should be parsed and passed to client request callback. Its arguments are: 370 | 1. `byteBuffer`:`Buffer`: Raw response body that should be parsed as js object or whatever you need 371 | 2. `nrcEventEmitter`:`client event emitter`: useful to dispatch events during parsing process, for example error events 372 | 3. `parsedCallback`:`function(parsedData)`: this callback should be invoked when parsing process has finished to pass parsed data to request callback. 373 | 374 | Of course any other method or attribute needed for parsing process can be added to parser. 375 | 376 | ```javascript 377 | // no "isDefault" attribute defined 378 | var invalid = { 379 | "name":"invalid-parser", 380 | "match":function(response){...}, 381 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){...} 382 | }; 383 | 384 | var validParser = { 385 | "name":"valid-parser", 386 | "isDefault": false, 387 | "match":function(response){...}, 388 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){...}, 389 | // of course any other args or methods can be added to parser 390 | "otherAttr":"my value", 391 | "otherMethod":function(a,b,c){...} 392 | }; 393 | 394 | function OtherParser(name){ 395 | this.name: name, 396 | this.isDefault: false, 397 | this.match=function(response){...}; 398 | this.parse:function(byteBuffer,nrcEventEmitter,parsedCallback){...}; 399 | 400 | } 401 | 402 | var instanceParser = new OtherParser("instance-parser"); 403 | 404 | //valid parser complete example 405 | 406 | client.parsers.add({ 407 | "name":"valid-parser", 408 | "isDefault":false, 409 | "match":function(response){ 410 | // only match to responses with a test-header equal to "hello world!" 411 | return response.headers["test-header"]==="hello world!"; 412 | }, 413 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ 414 | // parsing process 415 | var parsedData = null; 416 | try{ 417 | parsedData = JSON.parse(byteBuffer.toString()); 418 | parsedData.parsed = true; 419 | 420 | // emit custom event 421 | nrcEventEmitter('parsed','data has been parsed ' + parsedData); 422 | 423 | // pass parsed data to client request method callback 424 | parsedCallback(parsedData); 425 | }catch(err){ 426 | nrcEmitter('error',err); 427 | }; 428 | 429 | }); 430 | 431 | ``` 432 | 433 | By default and to maintain backward compatibility, client comes with 2 regular parsers and 1 default parser: 434 | 435 | - _**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. 436 | 437 | ```javascript 438 | var options = { 439 | mimetypes: { 440 | json: ["application/json", "application/my-custom-content-type-for-json;charset=utf-8"] 441 | 442 | } 443 | }; 444 | 445 | var client = new Client(options); 446 | 447 | ``` 448 | 449 | - _**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. 450 | 451 | ```javascript 452 | var options = { 453 | mimetypes: { 454 | xml: ["application/xml", "application/my-custom-content-type-for-xml"] 455 | } 456 | }; 457 | 458 | var client = new Client(options); 459 | 460 | ``` 461 | 462 | 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. 463 | 464 | ```javascript 465 | 466 | var client = new Client(); 467 | 468 | client.parsers.find("XML").options= {"explicitArray":false, "ignoreAttrs":true}; 469 | 470 | ``` 471 | 472 | 473 | - _**Default Parser**_: return responses as is, without any adittional processing. 474 | 475 | #### Parser Management 476 | 477 | Client can manage parsers through the following parsers namespace methods: 478 | 479 | * `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 480 | 481 | 1. `parser`: valid parser object. If invalid parser is added an 'error' event is dispatched by client. 482 | 483 | * `remove(parserName)`: removes a parser from parsers registry. If not parser found an 'error' event is dispatched by client. 484 | 485 | 1. `parserName`: valid parser name previously added. 486 | 487 | * `find(parserName)`: find and return a parser searched by its name. If not parser found an 'error' event is dispatched by client. 488 | 489 | 1. `parserName`: valid parser name previously added. 490 | 491 | * `getAll()`: return a collection of current regular parsers. 492 | 493 | * `getDefault()`: return the default parser used to process responses that doesn't match with any regular parser. 494 | 495 | * `clean()`: clean regular parser registry. default parser is not afected by this method. 496 | 497 | ```javascript 498 | var client = new Client(); 499 | 500 | client.parsers.add({ 501 | "name":"valid-parser", 502 | "isDefault": false, 503 | "match":function(response){...}, 504 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){...}, 505 | // of course any other args or methods can be added to parser 506 | "otherAttr":"my value", 507 | "otherMethod":function(a,b,c){...} 508 | }); 509 | 510 | var parser = client.parsers.find("valid-parser"); 511 | 512 | var defaultParser = client.parsers.getDefault(); 513 | 514 | var regularParsers = client.parsers.getAll(); 515 | 516 | client.parsers.clean(); 517 | 518 | 519 | ``` 520 | 521 | 522 | ### Request Serializers 523 | 524 | You can add your own request serializers to client, as many as you want. There are 2 serializer types: 525 | 526 | - _**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. 527 | 528 | - _**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. 529 | 530 | Each serializer - regular or default- needs to follow some conventions: 531 | 532 | * Must be and object 533 | 534 | * Must have the following attributes: 535 | 536 | * `name`: Used to identify serializer in serializers registry 537 | 538 | * `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 539 | 540 | * Must have the following methods: 541 | 542 | * `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: 543 | 1. `request`:`options passed to http.ClientRequest`: any option passed to a request through client options or request args, for example `headers` 544 | 545 | * `serialize(data,nrcEventEmitter,serializedCallback)` : this method is where request body should be serialized before passing to client request callback. Its arguments are: 546 | 1. `data`:`args data attribute`: Raw request body as is declared in args request attribute that should be serialized. 547 | 548 | 2. `nrcEventEmitter`:`client event emitter`: useful to dispatch events during serialization process, for example error events 549 | 550 | 3. `serializedCallback`:`function(serializedData)`: this callback should be invoked when serialization process has finished to pass serialized data to request callback. 551 | 552 | Of course any other method or attribute needed for serialization process can be added to serializer. 553 | 554 | ```javascript 555 | // no "isDefault" attribute defined 556 | var invalid = { 557 | "name":"invalid-serializer", 558 | "match":function(request){...}, 559 | "serialize":function(data,nrcEventEmitter,serializedCallback){...} 560 | }; 561 | 562 | var validserializer = { 563 | "name":"valid-serializer", 564 | "isDefault": false, 565 | "match":function(request){...}, 566 | "serialize":function(data,nrcEventEmitter,serializedCallback){...}, 567 | // of course any other args or methods can be added to serializer 568 | "otherAttr":"my value", 569 | "otherMethod":function(a,b,c){...} 570 | }; 571 | 572 | function OtherSerializer(name){ 573 | this.name: name, 574 | this.isDefault: false, 575 | this.match=function(request){...}; 576 | this.serialize:function(data,nrcEventEmitter,serializedCallback){...}; 577 | 578 | } 579 | 580 | var instanceserializer = new OtherSerializer("instance-serializer"); 581 | 582 | // valid serializer complete example 583 | 584 | client.serializers.add({ 585 | "name":"example-serializer", 586 | "isDefault":false, 587 | "match":function(request){ 588 | // only match to requests with a test-header equal to "hello world!" 589 | return request.headers["test-header"]==="hello world!"; 590 | }, 591 | "serialize":function(data,nrcEventEmitter,serializedCallback){ 592 | // serialization process 593 | var serializedData = null; 594 | 595 | if (typeof data === 'string'){ 596 | serializedData = data.concat(" I'm serialized!!"); 597 | }else if (typeof data === 'object'){ 598 | serializedData = data; 599 | serializedData.state = "serialized" 600 | serializedData = JSON.stringify(serializedData); 601 | } 602 | 603 | nrcEventEmitter('serialized','data has been serialized ' + serializedData); 604 | // pass serialized data to client to be sent to remote API 605 | serializedCallback(serializedData); 606 | 607 | } 608 | 609 | }) 610 | 611 | 612 | ``` 613 | 614 | By default client comes with 3 regular serializers and 1 default serializer: 615 | 616 | - _**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" 617 | 618 | 619 | - _**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" 620 | 621 | 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. 622 | 623 | ```javascript 624 | var client = new Client(); 625 | 626 | client.serializers.find("XML").options={"renderOpts":{"pretty": true }}; 627 | 628 | ``` 629 | 630 | - _**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" 631 | 632 | 633 | - _**Default serializer**_: serialize request to its string representation, applying toString() method to data parameter. 634 | 635 | #### serializer Management 636 | 637 | Client can manage serializers through the following serializers namespace methods: 638 | 639 | * `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 640 | 641 | 1. `serializer`: valid serializer object. If invalid serializer is added an 'error' event is dispatched by client. 642 | 643 | * `remove(serializerName)`: removes a serializer from serializers registry. If not serializer found an 'error' event is dispatched by client. 644 | 645 | 1. `serializerName`: valid serializer name previously added. 646 | 647 | * `find(serializerName)`: find and return a serializer searched by its name. If not serializer found an 'error' event is dispatched by client. 648 | 649 | 1. `serializerName`: valid serializer name previously added. 650 | 651 | * `getAll()`: return a collection of current regular serializers. 652 | 653 | * `getDefault()`: return the default serializer used to process requests that doesn't match with any regular serializer. 654 | 655 | * `clean()`: clean regular serializer registry. default serializer is not afected by this method. 656 | 657 | 658 | ```javascript 659 | var client = new Client(); 660 | 661 | client.serializers.add({ 662 | "name":"valid-serializer", 663 | "isDefault":false, 664 | "match":function(request){ 665 | // only match to requests with a test-header equal to "hello world!" 666 | return request.headers["test-header"]==="hello world!"; 667 | }, 668 | "serialize":function(data,nrcEventEmitter,serializedCallback){ 669 | // serialization process 670 | var serializedData = null; 671 | 672 | if (typeof data === 'string'){ 673 | serializedData = data.concat(" I'm serialized!!"); 674 | }else if (typeof data === 'object'){ 675 | serializedData = data; 676 | serializedData.state = "serialized" 677 | serializedData = JSON.stringify(serializedData); 678 | } 679 | 680 | nrcEventEmitter('serialized','data has been serialized ' + serializedData); 681 | // pass serialized data to client to be sent to remote API 682 | serializedCallback(serializedData); 683 | 684 | }); 685 | 686 | var serializer = client.serializers.find("valid-serializer"); 687 | 688 | var defaultParser = client.serializers.getDefault(); 689 | 690 | var regularSerializers = client.serializers.getAll(); 691 | 692 | client.serializers.clean(); 693 | 694 | 695 | ``` 696 | 697 | ### Connect through proxy 698 | 699 | Just pass proxy configuration as option to client. 700 | 701 | 702 | ```javascript 703 | var Client = require('node-rest-client').Client; 704 | 705 | // configure proxy 706 | var options_proxy = { 707 | proxy: { 708 | host: "proxy.foo.com", 709 | port: 8080, 710 | user: "proxyuser", 711 | password: "123", 712 | tunnel: true 713 | } 714 | }; 715 | 716 | var client = new Client(options_proxy); 717 | ``` 718 | 719 | client has 2 ways to connect to target site through a proxy server: tunnel or direct request, the first one is the default option 720 | so if you want to use direct request you must set tunnel off. 721 | 722 | ```javascript 723 | var Client = require('node-rest-client').Client; 724 | 725 | // configure proxy 726 | var options_proxy = { 727 | proxy: { 728 | host: "proxy.foo.com", 729 | port: 8080, 730 | user: "proxyuser", 731 | password: "123", 732 | tunnel: false // use direct request to proxy 733 | } 734 | }; 735 | 736 | var client = new Client(options_proxy); 737 | ``` 738 | 739 | 740 | 741 | ### Basic HTTP auth 742 | 743 | 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. 744 | 745 | ```javascript 746 | var Client = require('node-rest-client').Client; 747 | 748 | // configure basic http auth for every request 749 | var options_auth = { user: "admin", password: "123" }; 750 | 751 | var client = new Client(options_auth); 752 | ``` 753 | 754 | ### Options parameters 755 | 756 | You can pass the following args when creating a new client: 757 | 758 | ```javascript 759 | var options = { 760 | // proxy configuration 761 | proxy: { 762 | host: "proxy.foo.com", // proxy host 763 | port: 8080, // proxy port 764 | user: "ellen", // proxy username if required 765 | password: "ripley" // proxy pass if required 766 | }, 767 | // aditional connection options passed to node http.request y https.request methods 768 | // (ie: options to connect to IIS with SSL) 769 | connection: { 770 | secureOptions: constants.SSL_OP_NO_TLSv1_2, 771 | ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM', 772 | honorCipherOrder: true 773 | }, 774 | // will replace content-types used to match responses in JSON and XML parsers 775 | mimetypes: { 776 | json: ["application/json", "application/json;charset=utf-8"], 777 | xml: ["application/xml", "application/xml;charset=utf-8"] 778 | }, 779 | user: "admin", // basic http auth username if required 780 | password: "123", // basic http auth password if required 781 | requestConfig: { 782 | timeout: 1000, //request timeout in milliseconds 783 | noDelay: true, //Enable/disable the Nagle algorithm 784 | keepAlive: true, //Enable/disable keep-alive functionalityidle socket. 785 | keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent 786 | }, 787 | responseConfig: { 788 | timeout: 1000 //response timeout 789 | } 790 | }; 791 | ``` 792 | Note that requestConfig and responseConfig options if set on client instantiation apply to all of its requests/responses 793 | and is only overriden by request or reponse configs passed as args in method calls. 794 | 795 | 796 | ### Managing Requests 797 | 798 | Each REST method invocation returns a request object with specific request options and error, requestTimeout and responseTimeout event handlers. 799 | 800 | ```javascript 801 | var Client = require('node-rest-client').Client; 802 | 803 | var client = new Client(); 804 | 805 | var args = { 806 | requesConfig: { timeout: 1000 }, 807 | responseConfig: { timeout: 2000 } 808 | }; 809 | 810 | // direct way 811 | var req1 = client.get("http://remote.site/rest/xml/method", args, function (data, response) { 812 | // parsed response body as js object 813 | console.log(data); 814 | // raw response 815 | console.log(response); 816 | }); 817 | 818 | // view req1 options 819 | console.log(req1.options); 820 | 821 | 822 | req1.on('requestTimeout', function (req) { 823 | console.log("request has expired"); 824 | req.abort(); 825 | }); 826 | 827 | req1.on('responseTimeout', function (res) { 828 | console.log("response has expired"); 829 | 830 | }); 831 | 832 | 833 | // registering remote methods 834 | client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET"); 835 | 836 | var req2 = client.methods.jsonMethod(function (data, response) { 837 | // parsed response body as js object 838 | console.log(data); 839 | // raw response 840 | console.log(response); 841 | }); 842 | 843 | // handling specific req2 errors 844 | req2.on('error', function (err) { 845 | console.log('something went wrong on req2!!', err.request.options); 846 | }); 847 | ``` 848 | 849 | ### Error Handling 850 | 851 | Now you can handle error events in two places: on client or on each request. 852 | 853 | ```javascript 854 | var client = new Client(options_auth); 855 | 856 | // handling request error events 857 | client.get("http://remote.site/rest/xml/method", function (data, response) { 858 | // parsed response body as js object 859 | console.log(data); 860 | // raw response 861 | console.log(response); 862 | }).on('error', function (err) { 863 | console.log('something went wrong on the request', err.request.options); 864 | }); 865 | 866 | // handling client error events 867 | client.on('error', function (err) { 868 | console.error('Something went wrong on the client', err); 869 | }); 870 | ``` 871 | 872 | **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". _ 873 | 874 | -------------------------------------------------------------------------------- /test.bat: -------------------------------------------------------------------------------- 1 | npm test -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | npm test -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require should 2 | --reporter spec test/specs/*.js 3 | --ui bdd 4 | --recursive -------------------------------------------------------------------------------- /test/server/message.json: -------------------------------------------------------------------------------- 1 | {"catalog":{ 2 | "books":[{ 3 | "id":"bk101", 4 | "author":"Gambardella, Matthew", 5 | "title":"XML Developer's Guide", 6 | "genre":"Computer", 7 | "price":44.95, 8 | "publish_date":"2000-10-10", 9 | "description":"An in-depth look at creating applications with XML." 10 | }, 11 | { 12 | "id":"bk102", 13 | "author":"Gambardella, Matthew", 14 | "title":"JAVA Developer's Guide", 15 | "genre":"Computer", 16 | "price":188.95, 17 | "publish_date":"2000-10-10", 18 | "description":"An in-depth look at creating applications with JAVA." 19 | }, 20 | { 21 | "id":"bk103", 22 | "author":"Gambardella, Matthew", 23 | "title":"JSON Developer's Guide", 24 | "genre":"Computer", 25 | "price":422.95, 26 | "publish_date":"2000-10-10", 27 | "description":"An in-depth look at creating applications with JSON." 28 | } 29 | ] 30 | } 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /test/server/message.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Gambardella, Matthew 5 | XML Developer's Guide 6 | Computer 7 | 44.95 8 | 2000-10-01 9 | An in-depth look at creating applications 10 | with XML. 11 | 12 | 13 | Ralls, Kim 14 | Midnight Rain 15 | Fantasy 16 | 5.95 17 | 2000-12-16 18 | A former architect battles corporate zombies, 19 | an evil sorceress, and her own childhood to become queen 20 | of the world. 21 | 22 | 23 | Corets, Eva 24 | Maeve Ascendant 25 | Fantasy 26 | 5.95 27 | 2000-11-17 28 | After the collapse of a nanotechnology 29 | society in England, the young survivors lay the 30 | foundation for a new society. 31 | 32 | 33 | Corets, Eva 34 | Oberon's Legacy 35 | Fantasy 36 | 5.95 37 | 2001-03-10 38 | In post-apocalypse England, the mysterious 39 | agent known only as Oberon helps to create a new life 40 | for the inhabitants of London. Sequel to Maeve 41 | Ascendant. 42 | 43 | 44 | -------------------------------------------------------------------------------- /test/server/mock-server.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), fs = require('fs'); 2 | 3 | var RouterOptions = { 4 | "baseMessageDir" : "", 5 | "JSONMessageFile" : './test/server/message.json', 6 | "XMLMessageFile" : './test/server/message.xml' 7 | 8 | }; 9 | 10 | var RouteManager = { 11 | "findRoute" : function(req, res) { 12 | var handler = null; 13 | for ( var route in this.routes) { 14 | if (req.url.startsWith(route)) { 15 | handler = this.routes[route]; 16 | } 17 | 18 | } 19 | if (!handler) 20 | throw "cannot find route " + req.url; 21 | handler.call(this, req, res); 22 | }, 23 | "routes" : { 24 | "/json" : function(req, res) { 25 | // this.sleep(5000); 26 | var message = fs 27 | .readFileSync(RouterOptions.JSONMessageFile, 'utf8'); 28 | res.writeHead(200, { 29 | 'Content-Type' : 'application/json', 30 | 'test-header' : 'test' 31 | }); 32 | res.write(message.toString()); 33 | res.end(); 34 | }, 35 | "/json/path" : function(req, res) { 36 | // this.sleep(5000); 37 | var message = { 38 | "url" : req.url 39 | }; 40 | res.writeHead(200, { 41 | 'Content-Type' : 'application/json', 42 | 'test-header' : req.url 43 | }); 44 | res.write(JSON.stringify(message)); 45 | res.end(); 46 | }, 47 | "/xml" : function(req, res) { 48 | var message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8'); 49 | res.writeHead(200, { 50 | 'Content-Type' : 'application/xml' 51 | }); 52 | res.write(message.toString()); 53 | res.end(); 54 | }, 55 | "/120/json?arg1=hello&arg2=world" : function(req, res) { 56 | if (!req.headers["test-header"]) 57 | throw "no test-header found!!"; 58 | res.setHeader("test-response-header", req.headers["test-header"]); 59 | this.routes["/json"](req, res); 60 | }, 61 | "/json?post" : function(req, res) { 62 | req.on('data', function(data) { 63 | // console.log("[SERVER] data = ", data); 64 | res.writeHead(200, { 65 | 'Content-Type' : 'application/json' 66 | }); 67 | // res.writeHead(200, {'Content-Type': 'text/plain'}); 68 | res.write(data.toString()); 69 | res.end(); 70 | }); 71 | 72 | }, 73 | "/json/path/post" : function(req, res) { 74 | req.on('data', function(data) { 75 | var message = { 76 | "url" : req.url 77 | }; 78 | // console.log("[SERVER] data = ", data); 79 | res.writeHead(200, { 80 | 'Content-Type' : 'application/json' 81 | }); 82 | // res.writeHead(200, {'Content-Type': 'text/plain'}); 83 | message.postData = data.toString(); 84 | res.write(JSON.stringify(message)); 85 | res.end(); 86 | }); 87 | 88 | }, 89 | "/json/error" : function(req, res) { 90 | // this.sleep(5000); 91 | 92 | 93 | res.writeHead(500, {'Content-Type': 'text/plain'}); 94 | res.end(); 95 | 96 | 97 | }, 98 | "/xml/path/post" : function(req, res) { 99 | req.on('data', function(data) { 100 | // console.log("[SERVER] data = ", data); 101 | res.writeHead(200, { 102 | 'Content-Type' : 'application/xml' 103 | }); 104 | // res.writeHead(200, {'Content-Type': 'text/plain'}); 105 | res.write(data.toString()); 106 | res.end(); 107 | }); 108 | 109 | }, 110 | "/json/empty" : function(req, res) { 111 | res.writeHead(200, { 112 | 'Content-Type' : 'application/json' 113 | }); 114 | res.end(); 115 | }, 116 | "/xml/empty" : function(req, res) { 117 | res.writeHead(204, { 118 | 'Content-Type' : 'application/xml' 119 | }); 120 | res.end(); 121 | }, 122 | "/json/contenttypewithspace" : function(req, res) { 123 | var message = fs.readFileSync('./message.json', 'utf8'); 124 | res.writeHead(200, { 125 | 'Content-Type' : 'application/json; charset=utf-8' 126 | }); 127 | res.write(message.toString()); 128 | res.end(); 129 | }, 130 | "/json/test/content/type" : function(req, res) { 131 | var message = fs.readFileSync(RouterOptions.JSONMessageFile, 'utf8'); 132 | res.writeHead(200, { 133 | 'Content-Type' : 'test/json' 134 | }); 135 | res.write(message.toString()); 136 | res.end(); 137 | }, 138 | "/xml/test/content/type" : function(req, res) { 139 | var message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8'); 140 | res.writeHead(200, { 141 | 'Content-Type' : 'test/xml' 142 | }); 143 | res.write(message.toString()); 144 | res.end(); 145 | }, 146 | "/followRedirects":function(req, res){ 147 | 148 | var repeatOffset = req.url.indexOf("?"), 149 | repeat = parseInt(req.url.substring(repeatOffset + 1),10), 150 | location = ""; 151 | 152 | if (repeatOffset === 0){ 153 | res.writeHead(301, { 154 | 'Location':'http://localhost:4444/redirected' 155 | }); 156 | }else{ 157 | if (repeat > 0){ 158 | res.writeHead(301, { 159 | 'Location':'http://localhost:4444/followRedirects?' + --repeat 160 | }); 161 | }else{ 162 | res.writeHead(301, { 163 | 'Location':'http://localhost:4444/redirected' 164 | }); 165 | } 166 | 167 | } 168 | res.end(); 169 | }, 170 | "/redirected":function(req, res){ 171 | var message={"redirected":++this.redirectCount}; 172 | res.writeHead(200, { 173 | 'Content-Type' : 'application/json; charset=utf-8' 174 | }); 175 | res.write(JSON.stringify(message)); 176 | res.end(); 177 | } 178 | 179 | }, 180 | "sleep" : function(ms) { 181 | 182 | var stop = new Date().getTime(); 183 | while (new Date().getTime() < stop + ms) { 184 | ; 185 | } 186 | }, 187 | "redirectCount":0, 188 | "redirectLimit":10 189 | 190 | }; 191 | 192 | // Create an HTTP server 193 | this.server = http.createServer(function(req, res) { 194 | // console.log("[SERVER] req.url", req.url); 195 | RouteManager.findRoute(req, res); 196 | }); 197 | 198 | exports.baseURL = "http://localhost:4444"; 199 | 200 | exports.listen = function() { 201 | this.server.listen.apply(this.server, arguments); 202 | }; 203 | 204 | exports.close = function(callback) { 205 | this.server.close(callback); 206 | }; 207 | 208 | exports.on = function(event, cb) { 209 | this.server.on.apply(this.server, event, cb); 210 | }; 211 | -------------------------------------------------------------------------------- /test/specs/TestErrorHandlers.js: -------------------------------------------------------------------------------- 1 | var server =require("../server/mock-server"), 2 | Client=require("../../lib/node-rest-client").Client; 3 | 4 | describe('Error Handlers', function () { 5 | 6 | this.timeout(150000); 7 | 8 | before(function () { 9 | server.listen(4444); 10 | console.log("server started on port 4444"); 11 | }); 12 | 13 | describe("Client Error Hanlers",function(){ 14 | 15 | 16 | it("handle error with client handler", function(done){ 17 | var client = new Client(); 18 | client.on('error', function(err){ 19 | done(); 20 | }); 21 | client.get(server.baseURL + "/json/error", function(data, response){ 22 | client.emit('error', response.status); 23 | }); 24 | 25 | }); 26 | 27 | 28 | 29 | 30 | }); 31 | 32 | describe("#Request Error Handlers",function(){ 33 | 34 | it("handle error with request handler", function(done){ 35 | var client = new Client(); 36 | 37 | var req =client.get(server.baseURL + "/json/error", function(data, response){ 38 | req.emit('error', response.status); 39 | }); 40 | 41 | req.on('error',function(err){ 42 | done(); 43 | }) 44 | 45 | }); 46 | 47 | }); 48 | 49 | after(function () { 50 | server.close(); 51 | console.log("server stopped"); 52 | }); 53 | 54 | }); -------------------------------------------------------------------------------- /test/specs/TestFollowsRedirect.js: -------------------------------------------------------------------------------- 1 | var server =require("../server/mock-server"), 2 | Client=require("../../lib/node-rest-client").Client; 3 | 4 | describe('Follows Redirects', function () { 5 | 6 | this.timeout(150000); 7 | 8 | before(function () { 9 | server.listen(4444); 10 | console.log("server started on port 4444"); 11 | }); 12 | 13 | 14 | describe("#Follows Redirects",function(){ 15 | 16 | it("follows Redirect", function(done){ 17 | var client = new Client(); 18 | 19 | client.post(server.baseURL + "/followRedirects", function(data, response){ 20 | data.should.not.equal(null); 21 | data.should.type("object"); 22 | data.redirected.should.equal(1); 23 | done(); 24 | }); 25 | 26 | }); 27 | 28 | it("disable follows Redirect", function(done){ 29 | var client = new Client(); 30 | var args ={ 31 | requestConfig:{followRedirects:false} 32 | }; 33 | 34 | client.post(server.baseURL + "/followRedirects",args, function(data, response){ 35 | response.statusCode.should.be.equal(301); 36 | done(); 37 | }); 38 | 39 | }); 40 | 41 | 42 | 43 | it("set max redirects", function(done){ 44 | var client = new Client(); 45 | var args ={ 46 | requestConfig:{maxRedirects:3} 47 | }; 48 | 49 | 50 | 51 | var req = client.post(server.baseURL + "/followRedirects?5",args, function(data, response){ 52 | response.statusCode.should.be.equal(301); 53 | 54 | }); 55 | 56 | req.on('error', function(err){ 57 | err.message.should.be.equal("Max redirects exceeded.") 58 | done(); 59 | }); 60 | 61 | }); 62 | }); 63 | 64 | 65 | 66 | after(function () { 67 | server.close(); 68 | console.log("server stopped"); 69 | }); 70 | }); -------------------------------------------------------------------------------- /test/specs/TestGETMethod.js: -------------------------------------------------------------------------------- 1 | var server =require("../server/mock-server"), 2 | Client=require("../../lib/node-rest-client").Client; 3 | 4 | describe('GET Method', function () { 5 | 6 | this.timeout(150000); 7 | 8 | before(function () { 9 | server.listen(4444); 10 | console.log("server started on port 4444"); 11 | }); 12 | 13 | describe("#JSON",function(){ 14 | 15 | it("GET request with no args", function(done){ 16 | var client = new Client(); 17 | client.get(server.baseURL + "/json", function(data, response){ 18 | data.should.not.equal(null); 19 | data.should.type("object"); 20 | done(); 21 | }); 22 | }); 23 | 24 | it("GET request with path variable substitution", function(done){ 25 | var client = new Client(); 26 | var args ={ 27 | path:{testNumber:123, testString:"test"} 28 | }; 29 | client.get(server.baseURL + "/json/path/${testNumber}/${testString}",args, function(data, response){ 30 | 31 | data.should.not.equal(null); 32 | data.should.type("object"); 33 | data.url.should.equal("/json/path/123/test"); 34 | done(); 35 | }); 36 | }); 37 | 38 | 39 | it("GET request with parameters", function(done){ 40 | var client = new Client(); 41 | var args ={ 42 | parameters:{testNumber:123, testString:"test"} 43 | }; 44 | client.get(server.baseURL + "/json/path/query",args, function(data, response){ 45 | 46 | data.should.not.equal(null); 47 | data.should.type("object"); 48 | data.url.should.equal("/json/path/query?testNumber=123&testString=test"); 49 | done(); 50 | }); 51 | }); 52 | 53 | it("GET request with registered method and no args", function(done){ 54 | var client = new Client(); 55 | 56 | 57 | client.registerMethod("testMethod",server.baseURL + "/json","GET"); 58 | 59 | client.methods.testMethod( function(data, response){ 60 | data.should.not.equal(null); 61 | data.should.type("object"); 62 | done(); 63 | }); 64 | }); 65 | 66 | 67 | 68 | it("GET request with registered method and path variable substitution", function(done){ 69 | var client = new Client(); 70 | var args ={ 71 | path:{testNumber:123, testString:"test"} 72 | }; 73 | 74 | client.registerMethod("testMethod",server.baseURL + "/json/path/${testNumber}/${testString}","GET"); 75 | 76 | client.methods.testMethod(args, function(data, response){ 77 | data.should.not.equal(null); 78 | data.should.type("object"); 79 | data.url.should.equal("/json/path/123/test"); 80 | done(); 81 | }); 82 | }); 83 | 84 | 85 | it("GET request with registered method and parameters", function(done){ 86 | var client = new Client(); 87 | var args ={ 88 | parameters:{testNumber:123, testString:"test"} 89 | }; 90 | 91 | client.registerMethod("testMethod",server.baseURL + "/json/path/query","GET"); 92 | 93 | client.methods.testMethod(args, function(data, response){ 94 | data.should.not.equal(null); 95 | data.should.type("object"); 96 | data.url.should.equal("/json/path/query?testNumber=123&testString=test"); 97 | 98 | done(); 99 | }); 100 | }); 101 | 102 | it("GET request with incompatible parameters URL", function(done){ 103 | var client = new Client(); 104 | var args ={ 105 | parameters:{testNumber:123, testString:"test"} 106 | }; 107 | 108 | client.on('error', function(err){ 109 | err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); 110 | done(); 111 | }); 112 | 113 | client.get(server.baseURL + "/json/path/query?testNumber=123&testString=test", args, function(data, response){ 114 | //noop 115 | }).should.throw(); 116 | 117 | }); 118 | 119 | it("GET request with invalid args type", function(done){ 120 | var client = new Client(); 121 | var args = "123"; 122 | 123 | client.on('error', function(err){ 124 | err.should.startWith("args should be and object"); 125 | done(); 126 | }); 127 | 128 | 129 | 130 | client.get(server.baseURL + "/json/path/query",args, function(data, response){ 131 | //noop 132 | }).should.throw(); 133 | 134 | }); 135 | 136 | 137 | it("GET request with invalid parameters type", function(done){ 138 | var client = new Client(); 139 | var args ={ 140 | parameters:"{test='123'}" 141 | }; 142 | 143 | client.on('error', function(err){ 144 | err.should.startWith("cannot serialize"); 145 | done(); 146 | }); 147 | 148 | 149 | 150 | client.get(server.baseURL + "/json/path/query",args, function(data, response){ 151 | //noop 152 | }).should.throw(); 153 | 154 | }); 155 | 156 | it("GET request with registered method and incompatible parameters URL", function(done){ 157 | var client = new Client(); 158 | var args ={ 159 | parameters:{testNumber:123, testString:"test"} 160 | }; 161 | 162 | client.on('error', function(err){ 163 | err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); 164 | done(); 165 | }); 166 | 167 | client.registerMethod("testMethod",server.baseURL + "/json/path/query?testNumber=123&testString=test","GET"); 168 | 169 | client.methods.testMethod(args, function(data, response){ 170 | //noop 171 | }).should.throw(); 172 | 173 | }); 174 | 175 | it("GET request with registered method and invalid args type", function(done){ 176 | var client = new Client(); 177 | var args ="123"; 178 | 179 | client.on('error', function(err){ 180 | err.should.startWith("args should be and object"); 181 | done(); 182 | }); 183 | 184 | client.registerMethod("testMethod",server.baseURL + "/json/path/query","GET"); 185 | 186 | client.methods.testMethod(args, function(data, response){ 187 | //noop 188 | }).should.throw(); 189 | 190 | }); 191 | 192 | 193 | it("GET request with registered method and invalid parameters type", function(done){ 194 | var client = new Client(); 195 | var args ={ 196 | parameters:"{test='123'}" 197 | }; 198 | 199 | client.on('error', function(err){ 200 | err.should.startWith("cannot serialize"); 201 | done(); 202 | }); 203 | 204 | client.registerMethod("testMethod",server.baseURL + "/json/path/query","GET"); 205 | 206 | client.methods.testMethod(args, function(data, response){ 207 | //noop 208 | }).should.throw(); 209 | 210 | }); 211 | }); 212 | 213 | 214 | describe("#XML",function(){ 215 | 216 | it("GET request with no args", function(done){ 217 | var client = new Client(); 218 | client.get(server.baseURL + "/xml", function(data, response){ 219 | console.log("data es ", data); 220 | data.should.not.equal(null); 221 | data.should.type("object"); 222 | done(); 223 | }); 224 | }); 225 | 226 | }); 227 | 228 | after(function () { 229 | server.close(); 230 | console.log("server stopped"); 231 | }); 232 | }); -------------------------------------------------------------------------------- /test/specs/TestPOSTMethod.js: -------------------------------------------------------------------------------- 1 | var server =require("../server/mock-server"), 2 | Client=require("../../lib/node-rest-client").Client; 3 | 4 | describe('POST Method', function () { 5 | 6 | this.timeout(150000); 7 | 8 | before(function () { 9 | server.listen(4444); 10 | console.log("server started on port 4444"); 11 | }); 12 | 13 | describe("#JSON",function(){ 14 | 15 | it("POST request with path variable substitution", function(done){ 16 | var client = new Client(); 17 | var args ={ 18 | path:{testNumber:123, testString:"test"}, 19 | data:'{"dataNumber":123, "dataString":"test"}' 20 | 21 | }; 22 | client.post(server.baseURL + "/json/path/post/${testNumber}/${testString}",args, function(data, response){ 23 | data.should.not.equal(null); 24 | data.should.type("object"); 25 | data.url.should.equal("/json/path/post/123/test"); 26 | data.postData.should.equal('{"dataNumber":123, "dataString":"test"}'); 27 | done(); 28 | }); 29 | }); 30 | 31 | 32 | it("POST request with parameters", function(done){ 33 | var client = new Client(); 34 | var args ={ 35 | parameters:{testNumber:123, testString:"test"}, 36 | data:'{"dataNumber":123,"dataString":"test"}' 37 | }; 38 | client.post(server.baseURL + "/json/path/post/query",args, function(data, response){ 39 | 40 | data.should.not.equal(null); 41 | data.should.type("object"); 42 | data.url.should.equal("/json/path/post/query?testNumber=123&testString=test"); 43 | data.postData.should.equal('{"dataNumber":123,"dataString":"test"}'); 44 | done(); 45 | }); 46 | }); 47 | 48 | it("POST request with registered method and no args", function(done){ 49 | var client = new Client(); 50 | 51 | 52 | client.registerMethod("testMethod",server.baseURL + "/json","POST"); 53 | 54 | client.methods.testMethod( function(data, response){ 55 | data.should.not.equal(null); 56 | data.should.type("object"); 57 | done(); 58 | }); 59 | }); 60 | 61 | 62 | 63 | it("POST request with registered method and path variable substitution", function(done){ 64 | var client = new Client(); 65 | var args ={ 66 | path:{testNumber:123, testString:"test"}, 67 | data:'{"dataNumber":123,"dataString":"test"}' 68 | }; 69 | 70 | client.registerMethod("testMethod",server.baseURL + "/json/path/post/${testNumber}/${testString}","POST"); 71 | 72 | client.methods.testMethod(args, function(data, response){ 73 | data.should.not.equal(null); 74 | data.should.type("object"); 75 | data.url.should.equal("/json/path/post/123/test"); 76 | data.postData.should.equal('{"dataNumber":123,"dataString":"test"}'); 77 | done(); 78 | }); 79 | }); 80 | 81 | 82 | it("POST request with registered method and parameters", function(done){ 83 | var client = new Client(); 84 | var args ={ 85 | parameters:{testNumber:123, testString:"test"}, 86 | data:'{"dataNumber":123,"dataString":"test"}' 87 | }; 88 | 89 | client.registerMethod("testMethod",server.baseURL + "/json/path/post/query","POST"); 90 | 91 | client.methods.testMethod(args, function(data, response){ 92 | data.should.not.equal(null); 93 | data.should.type("object"); 94 | data.url.should.equal("/json/path/post/query?testNumber=123&testString=test"); 95 | data.postData.should.equal('{"dataNumber":123,"dataString":"test"}'); 96 | done(); 97 | }); 98 | }); 99 | 100 | it("POST request with incompatible parameters URL", function(done){ 101 | var client = new Client(); 102 | var args ={ 103 | parameters:{testNumber:123, testString:"test"}, 104 | data:{dataNumber:123, dataString:"test"} 105 | }; 106 | 107 | client.on('error', function(err){ 108 | err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); 109 | done(); 110 | }); 111 | 112 | client.post(server.baseURL + "/json/path/post/query?testNumber=123&testString=test", args, function(data, response){ 113 | // noop 114 | }).should.throw(); 115 | 116 | }); 117 | 118 | it("POST request with invalid args type", function(done){ 119 | var client = new Client(); 120 | var args = "123"; 121 | 122 | client.on('error', function(err){ 123 | err.should.startWith("args should be and object"); 124 | done(); 125 | }); 126 | 127 | 128 | 129 | client.post(server.baseURL + "/json/path/post/query",args, function(data, response){ 130 | // noop 131 | }).should.throw(); 132 | 133 | }); 134 | 135 | 136 | it("POST request with invalid parameters type", function(done){ 137 | var client = new Client(); 138 | var args ={ 139 | parameters:"{test='123'}" 140 | }; 141 | 142 | client.on('error', function(err){ 143 | err.should.startWith("cannot serialize"); 144 | done(); 145 | }); 146 | 147 | 148 | 149 | client.post(server.baseURL + "/json/path/post/query",args, function(data, response){ 150 | // noop 151 | }).should.throw(); 152 | 153 | }); 154 | 155 | it("POST request with registered method and incompatible parameters URL", function(done){ 156 | var client = new Client(); 157 | var args ={ 158 | parameters:{testNumber:123, testString:"test"}, 159 | data:{dataNumber:123, dataString:"test"} 160 | }; 161 | 162 | client.on('error', function(err){ 163 | err.should.startWith("parameters argument cannot be used if parameters are already defined in URL"); 164 | done(); 165 | }); 166 | 167 | client.registerMethod("testMethod",server.baseURL + "/json/path/post/query?testNumber=123&testString=test","POST"); 168 | 169 | client.methods.testMethod(args, function(data, response){ 170 | // noop 171 | }).should.throw(); 172 | 173 | }); 174 | 175 | it("POST request with registered method and invalid args type", function(done){ 176 | var client = new Client(); 177 | var args ="123"; 178 | 179 | client.on('error', function(err){ 180 | err.should.startWith("args should be and object"); 181 | done(); 182 | }); 183 | 184 | client.registerMethod("testMethod",server.baseURL + "/json/path/post/query","POST"); 185 | 186 | client.methods.testMethod(args, function(data, response){ 187 | // noop 188 | }).should.throw(); 189 | 190 | }); 191 | 192 | 193 | it("POST request with registered method and invalid parameters type", function(done){ 194 | var client = new Client(); 195 | var args ={ 196 | parameters:"{test='123'}" 197 | }; 198 | 199 | client.on('error', function(err){ 200 | err.should.startWith("cannot serialize"); 201 | done(); 202 | }); 203 | 204 | client.registerMethod("testMethod",server.baseURL + "/json/path/post/query","POST"); 205 | 206 | client.methods.testMethod(args, function(data, response){ 207 | // noop 208 | }).should.throw(); 209 | 210 | }); 211 | }); 212 | 213 | describe("#XML",function(){ 214 | 215 | it("POST request with parameters", function(done){ 216 | var client = new Client(); 217 | var args ={ 218 | data:"123123" 219 | }; 220 | client.post(server.baseURL + "/xml/path/post/query",args, function(data, response){ 221 | data.should.type("object"); 222 | data.testData.should.be.ok; 223 | data.testData.testNumber.should.be.ok; 224 | data.testData.testString.should.be.ok; 225 | data.testData.testNumber.should.be.a.Number; 226 | data.testData.testString.should.be.a.String; 227 | data.testData.testNumber.should.be.equal("123"); 228 | data.testData.testString.should.be.equal("123"); 229 | 230 | 231 | done(); 232 | }); 233 | }); 234 | 235 | }); 236 | 237 | after(function () { 238 | server.close(); 239 | console.log("server stopped"); 240 | }); 241 | }); -------------------------------------------------------------------------------- /test/specs/TestRIOFacade.js: -------------------------------------------------------------------------------- 1 | var server =require("../server/mock-server"), 2 | Client=require("../../lib/node-rest-client").Client; 3 | 4 | describe('IO Facade', function () { 5 | 6 | this.timeout(150000); 7 | 8 | before(function () { 9 | server.listen(4444); 10 | console.log("server started on port 4444"); 11 | }); 12 | 13 | describe("#Parsers",function(){ 14 | 15 | var testParser = {"name":"test-parser", 16 | "isDefault":false, 17 | "match":function(response){ 18 | return response.headers["test-header"] === "test"; 19 | }, 20 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ 21 | var message = JSON.parse(byteBuffer.toString()); 22 | message.parsed = true; 23 | parsedCallback(message); 24 | } 25 | }, 26 | defaultTestParser = {"name":"default-test-parser", 27 | "isDefault":true, 28 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ 29 | var message = JSON.parse(byteBuffer.toString()); 30 | message.defaultParsed = true; 31 | parsedCallback(message); 32 | } 33 | }; 34 | 35 | it("add invalid parser to client", function(done){ 36 | var client = new Client(); 37 | 38 | client.on('error', function(err){ 39 | err.should.startWith("parser cannot be added: invalid parser definition"); 40 | done(); 41 | }); 42 | 43 | client.parsers.add({"invalid":123, "parser":456}).should.throw(); 44 | }); 45 | 46 | 47 | it("add parser to client", function(done){ 48 | var client = new Client(); 49 | client.parsers.add(testParser); 50 | var parser = client.parsers.find("test-parser"); 51 | 52 | parser.should.not.equal(null); 53 | parser.should.type("object"); 54 | done(); 55 | }); 56 | 57 | 58 | it("remove parser from client", function(done){ 59 | var client = new Client(); 60 | 61 | client.on('error', function(err){ 62 | err.should.startWith("cannot find parser: test-parser doesn't exists"); 63 | done(); 64 | }); 65 | 66 | client.parsers.add(testParser); 67 | var parser = client.parsers.find("test-parser"); 68 | 69 | parser.should.not.equal(null); 70 | parser.should.type("object"); 71 | 72 | client.parsers.remove("test-parser"); 73 | client.parsers.find("test-parser"); 74 | }); 75 | 76 | it("response match parser", function(done){ 77 | var client = new Client(); 78 | client.parsers.clean(); 79 | client.parsers.add(testParser); 80 | 81 | client.get(server.baseURL + "/json", function(data, response){ 82 | data.should.not.equal(null); 83 | data.should.type("object"); 84 | data.should.have.property("parsed"); 85 | data.parsed.should.be.a.Boolean; 86 | data.parsed.should.be.true; 87 | done(); 88 | }); 89 | 90 | }); 91 | 92 | 93 | it("add and use default parser", function(done){ 94 | var client = new Client(); 95 | client.parsers.clean(); 96 | 97 | client.parsers.add(testParser); 98 | client.parsers.add(defaultTestParser); 99 | // no parsers defined, default must be used 100 | 101 | client.get(server.baseURL + "/json/path?default-test", function(data, response){ 102 | data.should.not.equal(null); 103 | data.should.type("object"); 104 | data.should.have.property("defaultParsed"); 105 | data.defaultParsed.should.be.a.Boolean; 106 | data.defaultParsed.should.be.true; 107 | done(); 108 | }); 109 | 110 | }); 111 | 112 | 113 | it("add custom types to args in JSON parser", function(done){ 114 | var options={ 115 | // customize mime types for json or xml connections 116 | mimetypes: { 117 | json: ["test/json"] 118 | } 119 | }; 120 | var client = new Client(options); 121 | 122 | client.get(server.baseURL + "/json/test/content/type", function(data, response){ 123 | data.should.not.equal(null); 124 | data.should.type("object"); 125 | done(); 126 | }); 127 | 128 | }); 129 | 130 | 131 | it("add custom types to args in XML parser", function(done){ 132 | var options={ 133 | // customize mime types for json or xml connections 134 | mimetypes: { 135 | xml: ["test/xml"] 136 | } 137 | }; 138 | var client = new Client(options); 139 | 140 | client.get(server.baseURL + "/xml/test/content/type", function(data, response){ 141 | data.should.not.equal(null); 142 | data.should.type("object"); 143 | done(); 144 | }); 145 | 146 | }); 147 | 148 | 149 | it("get all regular parsers", function(done){ 150 | 151 | var client = new Client(); 152 | var parsers = client.parsers.getAll(); 153 | parsers.should.have.length(2); 154 | done(); 155 | 156 | }); 157 | 158 | it("emit custom event from parser to client", function(done){ 159 | var client = new Client(); 160 | client.on('customEvent',function(event){ 161 | event.should.be.equal("my custom event"); 162 | done(); 163 | }); 164 | 165 | 166 | client.parsers.clean(); 167 | client.parsers.add({ 168 | "name":"example-parser", 169 | "isDefault":false, 170 | "match":function(request){return true; }, 171 | "parse":function(byteBuffer,nrcEventEmitter,parsedCallback){ 172 | nrcEventEmitter('customEvent','my custom event'); 173 | // pass serialized data to client to be sent to remote API 174 | parsedCallback(byteBuffer.toString()); 175 | 176 | } 177 | }); 178 | 179 | var args ={data:"test data"} 180 | 181 | client.post(server.baseURL + "/json/path/post/query",args, function(data, response){}); 182 | 183 | }); 184 | 185 | }); 186 | 187 | 188 | describe("#Serializers",function(){ 189 | 190 | var testSerializer = {"name":"test-serializer", 191 | "isDefault":false, 192 | "match":function(request){ 193 | return request.headers["test-header"] === "test"; 194 | }, 195 | "serialize":function(data,nrcEventEmitter,serializedCallback){ 196 | if (typeof data === 'object') 197 | data.serialized = true; 198 | serializedCallback(JSON.stringify(data)); 199 | } 200 | }, 201 | defaultTestSerializer = {"name":"default-test-serializer", 202 | "isDefault":true, 203 | "serialize":function(data,nrcEventEmitter,serializedCallback){ 204 | if (typeof data === 'object') 205 | data.defaultParsed = true; 206 | serializedCallback(data); 207 | } 208 | }; 209 | 210 | 211 | it("add invalid serializer to client", function(done){ 212 | var client = new Client(); 213 | 214 | client.on('error', function(err){ 215 | err.should.startWith("serializer cannot be added: invalid serializer definition"); 216 | done(); 217 | }); 218 | 219 | client.serializers.add({"invalid":123, "serializer":456}).should.throw(); 220 | }); 221 | 222 | 223 | 224 | it("add serializer to client", function(done){ 225 | var client = new Client(); 226 | client.serializers.add(testSerializer); 227 | var serializer = client.serializers.find("test-serializer"); 228 | 229 | serializer.should.not.equal(null); 230 | serializer.should.type("object"); 231 | done(); 232 | }); 233 | 234 | 235 | it("remove serializer from client", function(done){ 236 | var client = new Client(); 237 | 238 | client.on('error', function(err){ 239 | err.should.startWith("cannot find serializer: test-serializer doesn't exists"); 240 | done(); 241 | }); 242 | 243 | client.serializers.add(testSerializer); 244 | var serializer = client.serializers.find("test-serializer"); 245 | 246 | serializer.should.not.equal(null); 247 | serializer.should.type("object"); 248 | 249 | client.serializers.remove("test-serializer"); 250 | client.serializers.find("test-serializer"); 251 | }); 252 | 253 | 254 | it("request match serializer", function(done){ 255 | var client = new Client(), 256 | args={ 257 | headers:{"test-header":"test"}, 258 | data:{"testNumber":123, "testString":"abc"} 259 | }; 260 | 261 | client.serializers.clean(); 262 | client.serializers.add(testSerializer); 263 | 264 | var request = client.post(server.baseURL + "/json/path/post",args, function(data, response){ 265 | data.postData.should.not.equal(null); 266 | data.postData.should.type("object"); 267 | data.postData.should.have.property("serialized"); 268 | data.postData.serialized.should.be.a.Boolean; 269 | data.postData.serialized.should.be.true; 270 | }); 271 | 272 | done(); 273 | 274 | }); 275 | 276 | it("get all regular serializers", function(done){ 277 | 278 | var client = new Client(); 279 | var serializers = client.serializers.getAll(); 280 | serializers.should.have.length(3); 281 | done(); 282 | 283 | }); 284 | 285 | it("emit custom event from serializer to client", function(done){ 286 | var client = new Client(); 287 | client.on('customEvent',function(event){ 288 | event.should.be.equal("my custom event"); 289 | done(); 290 | }); 291 | 292 | 293 | client.serializers.clean(); 294 | client.serializers.add({ 295 | "name":"example-serializer", 296 | "isDefault":false, 297 | "match":function(request){return true; }, 298 | "serialize":function(data,nrcEventEmitter,serializedCallback){ 299 | nrcEventEmitter('customEvent','my custom event'); 300 | // pass serialized data to client to be sent to remote API 301 | serializedCallback(data.toString()); 302 | 303 | } 304 | }); 305 | 306 | var args ={data:"test data"} 307 | 308 | client.post(server.baseURL + "/json/path/post/query",args, function(data, response){}); 309 | 310 | }); 311 | 312 | }); 313 | 314 | after(function () { 315 | server.close(); 316 | }); 317 | }); -------------------------------------------------------------------------------- /test/test-proxy.js: -------------------------------------------------------------------------------- 1 | var http = require('http'); 2 | var fs = require('fs'); 3 | 4 | var blacklist = []; 5 | var iplist = []; 6 | 7 | fs.watchFile('./blacklist', function(c,p) { update_blacklist(); }); 8 | fs.watchFile('./iplist', function(c,p) { update_iplist(); }); 9 | 10 | function update_blacklist() { 11 | console.log("Updating blacklist."); 12 | blacklist = fs.readFileSync('./blacklist', {encoding: 'utf-8'}).split('\n') 13 | .filter(function(rx) { return rx.length }) 14 | .map(function(rx) { return RegExp(rx) }); 15 | } 16 | 17 | function update_iplist() { 18 | console.log("Updating iplist."); 19 | iplist = fs.readFileSync('./iplist', {encoding: 'utf-8'}).split('\n') 20 | .filter(function(rx) { return rx.length }); 21 | } 22 | 23 | function ip_allowed(ip) { 24 | for (i in iplist) { 25 | if (iplist[i] == ip) { 26 | return true; 27 | } 28 | } 29 | return false; 30 | } 31 | 32 | function host_allowed(host) { 33 | for (i in blacklist) { 34 | if (blacklist[i].test(host)) { 35 | return false; 36 | } 37 | } 38 | return true; 39 | } 40 | 41 | function deny(response, msg) { 42 | response.writeHead(401); 43 | response.write(msg); 44 | response.end(); 45 | } 46 | 47 | http.createServer(function(request, response) { 48 | var ip = request.connection.remoteAddress; 49 | if (!ip_allowed(ip)) { 50 | msg = "IP " + ip + " is not allowed to use this proxy"; 51 | deny(response, msg); 52 | console.log(msg); 53 | return; 54 | } 55 | 56 | if (!host_allowed(request.url)) { 57 | msg = "Host " + request.url + " has been denied by proxy configuration"; 58 | deny(response, msg); 59 | console.log(msg); 60 | return; 61 | } 62 | 63 | console.log(ip + ": " + request.method + " " + request.url); 64 | var agent = new http.Agent({ host: request.headers['host'], port: 80, maxSockets: 1 }); 65 | var proxy_request = http.request({ 66 | host: request.headers['host'], 67 | port: 80, 68 | method: request.method, 69 | path: request.url, 70 | headers: request.headers, 71 | agent: agent 72 | }); 73 | proxy_request.addListener('response', function(proxy_response) { 74 | proxy_response.addListener('data', function(chunk) { 75 | response.write(chunk, 'binary'); 76 | }); 77 | proxy_response.addListener('end', function() { 78 | response.end(); 79 | }); 80 | response.writeHead(proxy_response.statusCode, proxy_response.headers); 81 | }); 82 | request.addListener('data', function(chunk) { 83 | proxy_request.write(chunk, 'binary'); 84 | }); 85 | request.addListener('end', function() { 86 | proxy_request.end(); 87 | }); 88 | }).listen(8080); 89 | 90 | update_blacklist(); 91 | update_iplist(); --------------------------------------------------------------------------------