Skip to content

Commit 0f24351

Browse files
committed
Added targetTimeout option and two tests for timeout
1 parent 7cb98a4 commit 0f24351

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

lib/http-proxy/passes/web-incoming.js

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ web_o = Object.keys(web_o).map(function(pass) {
109109
common.setupOutgoing(options.ssl || {}, options, req)
110110
);
111111

112+
// allow outgoing socket to timeout so that we could
113+
// show an error page at the initial request
114+
if(options.targetTimeout) {
115+
proxyReq.setTimeout(options.targetTimeout, function() {
116+
proxyReq.abort();
117+
});
118+
}
119+
112120
// Ensure we abort proxy if request is aborted
113121
req.on('aborted', function () {
114122
proxyReq.abort();

test/lib-http-proxy-passes-web-incoming-test.js

+83-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,87 @@ describe('#createProxyServer.web() using own http server', function () {
128128
}, function() {}).end();
129129
});
130130

131+
it('should proxy the request and handle timeout error (targetTimeout)', function(done) {
132+
var proxy = httpProxy.createProxyServer({
133+
target: 'http://127.0.0.1:45000',
134+
targetTimeout: 100
135+
});
136+
137+
require('net').createServer().listen(45000);
138+
139+
var proxyServer = http.createServer(requestHandler);
140+
141+
var started = new Date().getTime();
142+
function requestHandler(req, res) {
143+
proxy.once('error', function (err, errReq, errRes) {
144+
proxyServer.close();
145+
expect(err).to.be.an(Error);
146+
expect(errReq).to.be.equal(req);
147+
expect(errRes).to.be.equal(res);
148+
expect(new Date().getTime() - started).to.be.greaterThan(99);
149+
expect(err.code).to.be('ECONNRESET');
150+
done();
151+
});
152+
153+
proxy.web(req, res);
154+
}
155+
156+
proxyServer.listen('8084');
157+
158+
http.request({
159+
hostname: '127.0.0.1',
160+
port: '8084',
161+
method: 'GET',
162+
}, function() {}).end();
163+
});
164+
165+
it('should proxy the request and handle timeout error', function(done) {
166+
var proxy = httpProxy.createProxyServer({
167+
target: 'http://127.0.0.1:45001',
168+
timeout: 100
169+
});
170+
171+
require('net').createServer().listen(45001);
172+
173+
var proxyServer = http.createServer(requestHandler);
174+
175+
var cnt = 0;
176+
var doneOne = function() {
177+
cnt += 1;
178+
if(cnt === 2) done();
179+
}
180+
181+
var started = new Date().getTime();
182+
function requestHandler(req, res) {
183+
proxy.once('error', function (err, errReq, errRes) {
184+
proxyServer.close();
185+
expect(err).to.be.an(Error);
186+
expect(errReq).to.be.equal(req);
187+
expect(errRes).to.be.equal(res);
188+
expect(err.code).to.be('ECONNRESET');
189+
doneOne();
190+
});
191+
192+
proxy.web(req, res);
193+
}
194+
195+
proxyServer.listen('8085');
196+
197+
var req = http.request({
198+
hostname: '127.0.0.1',
199+
port: '8085',
200+
method: 'GET',
201+
}, function() {});
202+
203+
req.on('error', function(err) {
204+
expect(err).to.be.an(Error);
205+
expect(err.code).to.be('ECONNRESET');
206+
expect(new Date().getTime() - started).to.be.greaterThan(99);
207+
doneOne();
208+
});
209+
req.end();
210+
});
211+
131212
it('should proxy the request and provide a proxyRes event with the request and response parameters', function(done) {
132213
var proxy = httpProxy.createProxyServer({
133214
target: 'http://127.0.0.1:8080'
@@ -151,8 +232,8 @@ describe('#createProxyServer.web() using own http server', function () {
151232
res.end('Response');
152233
});
153234

154-
proxyServer.listen('8084');
235+
proxyServer.listen('8086');
155236
source.listen('8080');
156-
http.request('http://127.0.0.1:8084', function() {}).end();
237+
http.request('http://127.0.0.1:8086', function() {}).end();
157238
});
158239
});

0 commit comments

Comments
 (0)