Skip to content

Commit

Permalink
Fixed issue where error callback would not invoke, including new test…
Browse files Browse the repository at this point in the history
… cases. Added req/res values to error events.

Conflicts:
	lib/http-proxy/passes/web-incoming.js
  • Loading branch information
mmoulton authored and cronopio committed Oct 21, 2013
1 parent 5d66ce1 commit 0bfb9be
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
92 changes: 92 additions & 0 deletions test/lib-http-proxy-integration-test.js
Original file line number Diff line number Diff line change
@@ -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();
});

});

});

0 comments on commit 0bfb9be

Please sign in to comment.