diff --git a/lib/caronte/common.js b/lib/caronte/common.js index 59a1cf9..78b4586 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -13,15 +13,16 @@ var common = exports; * @param {Object} Outgoing Base object to be filled with required properties * @param {Object} Options Config object passed to the proxy * @param {ClientRequest} Req Request Object + * @param {String} Forward String to select forward or target *  * @return {Object} Outgoing Object with all required properties set * * @api private */ -common.setupOutgoing = function(outgoing, options, req) { +common.setupOutgoing = function(outgoing, options, req, forward) { ['host', 'hostname', 'port', 'socketPath'/*, 'agent'*/].forEach( - function(e) { outgoing[e] = options.target[e]; } + function(e) { outgoing[e] = options[forward || 'target'][e]; } ); ['method', 'path', 'headers'].forEach( diff --git a/lib/caronte/index.js b/lib/caronte/index.js index bf61609..6700a43 100644 --- a/lib/caronte/index.js +++ b/lib/caronte/index.js @@ -33,7 +33,6 @@ function createRightProxy(type) { return function(req, res) { var self = this, ev = 'caronte:' + type + ':'; - //self.emit(ev + 'begin', req, res); passes.forEach(function(pass) { diff --git a/lib/caronte/streams/forward.js b/lib/caronte/streams/forward.js index 665a919..b1bb58f 100644 --- a/lib/caronte/streams/forward.js +++ b/lib/caronte/streams/forward.js @@ -50,8 +50,8 @@ require('util').inherits(ForwardStream, Writable); */ ForwardStream.prototype.onPipe = function(request) { - this.forwardReq = (options.ssl ? https : http).request( - common.setupOutgoing(options.ssl || {}, options, request) + this.forwardReq = (this.options.ssl ? https : http).request( + common.setupOutgoing(this.options.ssl || {}, this.options, request, 'forward') ); this.forwardReq.on('error', function() {}); /** Fire and forget */ @@ -85,4 +85,4 @@ ForwardStream.prototype.onFinish = function() { ForwardStream.prototype._write = function(chunk, encoding, clb) { this.forwardReq.write(chunk, encoding, clb); -}; \ No newline at end of file +}; diff --git a/test/lib-caronte-streams-forward-test.js b/test/lib-caronte-streams-forward-test.js index 5e74a1e..8de2afe 100644 --- a/test/lib-caronte-streams-forward-test.js +++ b/test/lib-caronte-streams-forward-test.js @@ -1,4 +1,5 @@ -var ForwardStream = require('../lib/caronte/streams/forward'), +var caronte = require('../'), + ForwardStream = require('../lib/caronte/streams/forward'); expect = require('expect.js'), Writable = require('stream').Writable, http = require('http'); @@ -22,16 +23,23 @@ describe('lib/caronte/passes/web.js', function () { }); describe('should pipe the request and finish it', function () { - it('should make the request on pipe and finish it'); - var stubOptions = { - target: { - hostname : 'www.google.com', - port : '80', - path : '/' - } - }; + it('should make the request on pipe and finish it', function(done) { + var result; + + var p = caronte.createProxyServer({ + forward: 'http://127.0.0.1:8080' + }).listen('8081') - var forwardProxy = new ForwardStream({}); + var s = http.createServer(function(req, res) { + expect(req.method).to.eql('GET'); + s.close(); + p.close(); + done(); + }); + s.listen('8080'); + + http.request('http://127.0.0.1:8081', function() {}).end(); + }); }); -}); \ No newline at end of file +});