mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
Added targetTimeout option and two tests for timeout
This commit is contained in:
parent
7cb98a4e41
commit
0f243516e1
@ -109,6 +109,14 @@ web_o = Object.keys(web_o).map(function(pass) {
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
);
|
||||
|
||||
// allow outgoing socket to timeout so that we could
|
||||
// show an error page at the initial request
|
||||
if(options.targetTimeout) {
|
||||
proxyReq.setTimeout(options.targetTimeout, function() {
|
||||
proxyReq.abort();
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure we abort proxy if request is aborted
|
||||
req.on('aborted', function () {
|
||||
proxyReq.abort();
|
||||
|
||||
@ -128,6 +128,87 @@ describe('#createProxyServer.web() using own http server', function () {
|
||||
}, function() {}).end();
|
||||
});
|
||||
|
||||
it('should proxy the request and handle timeout error (targetTimeout)', function(done) {
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:45000',
|
||||
targetTimeout: 100
|
||||
});
|
||||
|
||||
require('net').createServer().listen(45000);
|
||||
|
||||
var proxyServer = http.createServer(requestHandler);
|
||||
|
||||
var started = new Date().getTime();
|
||||
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(new Date().getTime() - started).to.be.greaterThan(99);
|
||||
expect(err.code).to.be('ECONNRESET');
|
||||
done();
|
||||
});
|
||||
|
||||
proxy.web(req, res);
|
||||
}
|
||||
|
||||
proxyServer.listen('8084');
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: '8084',
|
||||
method: 'GET',
|
||||
}, function() {}).end();
|
||||
});
|
||||
|
||||
it('should proxy the request and handle timeout error', function(done) {
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:45001',
|
||||
timeout: 100
|
||||
});
|
||||
|
||||
require('net').createServer().listen(45001);
|
||||
|
||||
var proxyServer = http.createServer(requestHandler);
|
||||
|
||||
var cnt = 0;
|
||||
var doneOne = function() {
|
||||
cnt += 1;
|
||||
if(cnt === 2) done();
|
||||
}
|
||||
|
||||
var started = new Date().getTime();
|
||||
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('ECONNRESET');
|
||||
doneOne();
|
||||
});
|
||||
|
||||
proxy.web(req, res);
|
||||
}
|
||||
|
||||
proxyServer.listen('8085');
|
||||
|
||||
var req = http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: '8085',
|
||||
method: 'GET',
|
||||
}, function() {});
|
||||
|
||||
req.on('error', function(err) {
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.code).to.be('ECONNRESET');
|
||||
expect(new Date().getTime() - started).to.be.greaterThan(99);
|
||||
doneOne();
|
||||
});
|
||||
req.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'
|
||||
@ -151,8 +232,8 @@ describe('#createProxyServer.web() using own http server', function () {
|
||||
res.end('Response');
|
||||
});
|
||||
|
||||
proxyServer.listen('8084');
|
||||
proxyServer.listen('8086');
|
||||
source.listen('8080');
|
||||
http.request('http://127.0.0.1:8084', function() {}).end();
|
||||
http.request('http://127.0.0.1:8086', function() {}).end();
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user