From 0bfb9be418926f2113489e92504038127d4c04bb Mon Sep 17 00:00:00 2001 From: mmoulton Date: Mon, 21 Oct 2013 10:05:06 -0700 Subject: [PATCH] Fixed issue where error callback would not invoke, including new test cases. Added req/res values to error events. Conflicts: lib/http-proxy/passes/web-incoming.js --- lib/http-proxy/passes/web-incoming.js | 9 ++- test/lib-http-proxy-integration-test.js | 92 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 test/lib-http-proxy-integration-test.js diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index b8f936d51..eceb1636e 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -106,11 +106,10 @@ web_o = Object.keys(web_o).map(function(pass) { // Error Handler proxyReq.on('error', function(err){ - if(server) { - server.emit('error', err); - } - else {  - clb(err); + if (clb) { + clb(err); + } else { + server.emit('error', err, req, res); } }); diff --git a/test/lib-http-proxy-integration-test.js b/test/lib-http-proxy-integration-test.js new file mode 100644 index 000000000..f6ea70693 --- /dev/null +++ b/test/lib-http-proxy-integration-test.js @@ -0,0 +1,92 @@ +var httpProxy = require('../lib/http-proxy'), + expect = require('expect.js'), + http = require('http'); + + +describe('lib/http-proxy.js', function() { + + describe('#createProxyServer with target options for integration into existing server', function () { + it('should proxy the request using the web proxy handler', function (done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:8080' + }); + + function requestHandler(req, res) { + proxy.web(req, res); + } + + var proxyServer = http.createServer(requestHandler); + + var source = http.createServer(function(req, res) { + source.close(); + proxyServer.close(); + expect(req.method).to.eql('GET'); + expect(req.headers.host.split(':')[1]).to.eql('8082'); + done(); + }); + + proxyServer.listen('8082'); + source.listen('8080'); + + http.request('http://127.0.0.1:8082', function() {}).end(); + }); + }); + + describe('#createProxyServer() for integration into existing server with error response', function () { + it('should proxy the request and handle error via callback', function(done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:8080' + }); + + var proxyServer = http.createServer(requestHandler); + + function requestHandler(req, res) { + proxy.web(req, res, function (err) { + proxyServer.close(); + expect(err).to.be.an(Error); + expect(err.code).to.be('ECONNREFUSED'); + done(); + }); + } + + proxyServer.listen('8082'); + + http.request({ + hostname: '127.0.0.1', + port: '8082', + method: 'GET', + }, function() {}).end(); + }); + + it('should proxy the request and handle error event listener', function(done) { + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:8080' + }); + + var proxyServer = http.createServer(requestHandler); + + function requestHandler(req, res) { + proxy.once('error', function (err, errReq, errRes) { + proxyServer.close(); + expect(err).to.be.an(Error); + expect(errReq).to.be.equal(req); + expect(errRes).to.be.equal(res); + expect(err.code).to.be('ECONNREFUSED'); + done(); + }); + + proxy.web(req, res); + } + + proxyServer.listen('8082'); + + http.request({ + hostname: '127.0.0.1', + port: '8082', + method: 'GET', + }, function() {}).end(); + }); + + }); + +}); \ No newline at end of file