diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index b127bc709..846219b6c 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -109,6 +109,15 @@ web_o = Object.keys(web_o).map(function(pass) { common.setupOutgoing(options.ssl || {}, options, req) ); + if(options.proxy_timeout) { + proxyReq.on('socket', function (socket) { + socket.setTimeout(options.proxy_timeout); + socket.on('timeout', function() { + proxyReq.abort(); + }); + }); + } + // Ensure we abort proxy if request is aborted req.on('aborted', function () { proxyReq.abort(); diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 49ffa9ef4..987b99ab8 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -155,4 +155,4 @@ describe('#createProxyServer.web() using own http server', function () { source.listen('8080'); http.request('http://127.0.0.1:8084', function() {}).end(); }); -}); \ No newline at end of file +}); diff --git a/test/lib-http-proxy-test.js b/test/lib-http-proxy-test.js index d6d5b31df..60c313686 100644 --- a/test/lib-http-proxy-test.js +++ b/test/lib-http-proxy-test.js @@ -188,7 +188,44 @@ describe('lib/http-proxy.js', function() { testReq.end(); }) - }) + }); + + describe('#createProxyServer setting the correct proxy timeout value', function () { + it('should hang up the socket at the timeout', function (done) { + this.timeout(30); + var ports = { source: gen.port, proxy: gen.port }; + var proxy = httpProxy.createProxyServer({ + target: 'http://127.0.0.1:' + ports.source, + proxy_timeout: 3 + }).listen(ports.proxy); + + proxy.on('error', function (err, req, res) { + res.writeHead(500, {'Content-Type': 'text/plain'}); + res.end('Something went wrong with the request: ' + err); + }); + + var source = http.createServer(function(req, res) { + setTimeout(function () { + res.end('At this point the socket should be closed'); + }, 5) + }); + + source.listen(ports.source); + + var testReq = http.request({ + hostname: '127.0.0.1', + port: ports.proxy, + method: 'GET', + }, function(res) { + expect(res.statusCode).to.be.eql(500); + proxy._server.close(); + source.close(); + done(); + }); + + testReq.end(); + }) + }); // describe('#createProxyServer using the web-incoming passes', function () { // it('should emit events correclty', function(done) {