Merge pull request #642 from bruce-one/proxyRes-req-res

`proxyRes` event, provide access to the req and res objects
This commit is contained in:
Jarrett Cruger 2014-05-11 19:01:05 -04:00
commit 5ebf9833c8
2 changed files with 29 additions and 1 deletions

View File

@ -131,7 +131,7 @@ web_o = Object.keys(web_o).map(function(pass) {
(options.buffer || req).pipe(proxyReq);
proxyReq.on('response', function(proxyRes) {
if(server) { server.emit('proxyRes', proxyRes); }
if(server) { server.emit('proxyRes', proxyRes, req, res); }
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes)) { break; }
}

View File

@ -127,4 +127,32 @@ describe('#createProxyServer.web() using own http server', function () {
method: 'GET',
}, function() {}).end();
});
it('should proxy the request and provide a proxyRes event with the request and response parameters', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080'
});
function requestHandler(req, res) {
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
source.close();
proxyServer.close();
expect(pReq).to.be.equal(req);
expect(pRes).to.be.equal(res);
done();
});
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
res.end('Response');
});
proxyServer.listen('8084');
source.listen('8080');
http.request('http://127.0.0.1:8084', function() {}).end();
});
});