Skip to content

Commit f6bac7b

Browse files
committed
Merge pull request #658 from RushPL/master
Added proxyTimeout option and two tests for timeout
2 parents 7cb98a4 + 48d46e6 commit f6bac7b

File tree

3 files changed

+100
-7
lines changed

3 files changed

+100
-7
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.proxyTimeout) {
115+
proxyReq.setTimeout(options.proxyTimeout, 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 (proxyTimeout)', function(done) {
132+
var proxy = httpProxy.createProxyServer({
133+
target: 'http://127.0.0.1:45000',
134+
proxyTimeout: 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
});

test/lib-http-proxy-test.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,11 @@ describe('lib/http-proxy.js', function() {
294294
var proxy = httpProxy.createProxyServer({
295295
target: 'ws://127.0.0.1:' + ports.source,
296296
ws: true
297-
}),
298-
proxyServer = proxy.listen(ports.proxy),
299-
destiny = io.listen(ports.source, function () {
297+
});
298+
proxyServer = proxy.listen(ports.proxy);
299+
var server = http.createServer();
300+
destiny = io.listen(server);
301+
function startSocketIo() {
300302
var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy);
301303

302304
client.on('connect', function () {
@@ -306,10 +308,12 @@ describe('lib/http-proxy.js', function() {
306308
client.on('outgoing', function (data) {
307309
expect(data).to.be('Hello over websockets');
308310
proxyServer._server.close();
309-
destiny.server.close();
311+
server.close();
310312
done();
311313
});
312-
});
314+
}
315+
server.listen(ports.source);
316+
server.on('listening', startSocketIo);
313317

314318
destiny.sockets.on('connection', function (socket) {
315319
socket.on('incoming', function (msg) {

0 commit comments

Comments
 (0)