Skip to content

Commit 89d43c2

Browse files
pdoranindexzero
authored andcommitted
Added timeout option and test to test new timeout parameter, added requestFail assertion.
1 parent 476cbe7 commit 89d43c2

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ If you have a suggestion for a feature currently not supported, feel free to ope
550550
xforward: true // enables X-Forwarded-For
551551
},
552552
changeOrigin: false, // changes the origin of the host header to the target URL
553+
timeout: 120000 // override the default 2 minute http socket timeout value in milliseconds
553554
}
554555
```
555556

lib/node-http-proxy/http-proxy.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
6666
//
6767
this.forward = options.forward;
6868
this.target = options.target;
69+
this.timeout = options.timeout;
6970

7071
//
7172
// Setup the necessary instances instance variables for
@@ -163,6 +164,9 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
163164
req.headers['x-forwarded-proto'] = getProto(req);
164165
}
165166
}
167+
if(this.timeout) {
168+
req.socket.setTimeout(this.timeout);
169+
}
166170

167171
//
168172
// Emit the `start` event indicating that we have begun the proxy operation.
@@ -350,10 +354,17 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
350354
});
351355

352356
//
353-
// Handle 'error' events from the `reverseProxy`.
357+
// Handle 'error' events from the `reverseProxy`. Setup timeout override if needed
354358
//
355359
reverseProxy.once('error', proxyError);
356360

361+
// Set a timeout on the socket if `this.timeout` is specified.
362+
reverseProxy.once('socket', function (socket) {
363+
if (self.timeout) {
364+
socket.setTimeout(self.timeout);
365+
}
366+
});
367+
357368
//
358369
// Handle 'error' events from the `req` (e.g. `Parse Error`).
359370
//
@@ -899,4 +910,4 @@ HttpProxy.prototype._forwardRequest = function (req) {
899910

900911
function getProto(req) {
901912
return req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http');
902-
}
913+
}

test/helpers/http.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ exports.createServer = function (options, callback) {
6767
});
6868
}
6969

70-
res.writeHead(200, { 'Content-Type': 'text/plain' });
71-
res.write(options.output || 'hello proxy');
72-
res.end();
70+
setTimeout(function() {
71+
res.writeHead(200, { 'Content-Type': 'text/plain' });
72+
res.write(options.output || 'hello proxy');
73+
res.end();
74+
}, options.latency || 1);
7375
}
7476

7577
var server = protocols.target === 'https'

test/http/http-test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ vows.describe(helpers.describe()).addBatch({
7676
outputHeaders: { "x-testheader": "target" },
7777
latency: 1000
7878
})
79-
}
79+
},
80+
"and timeout set": macros.http.assertProxied({
81+
shouldFail: true,
82+
timeout: 2000,
83+
requestLatency: 4000
84+
})
8085
},
8186
"With a no valid target server": {
8287
"and no latency": macros.http.assertInvalidProxy(),

test/macros/http.js

+35-6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ exports.assertRequest = function (options) {
4949
};
5050
};
5151

52+
//
53+
// ### function assertFailedRequest (options)
54+
// #### @options {Object} Options for this failed request assertion.
55+
// #### @request {Object} Options to use for `request`.
56+
// #### @assert {Object} Test assertions against the response.
57+
//
58+
// Makes a request using `options.request` and then asserts the response
59+
// and body against anything in `options.assert`.
60+
//
61+
exports.assertFailedRequest = function (options) {
62+
return {
63+
topic: function () {
64+
//
65+
// Now make the HTTP request and assert.
66+
//
67+
options.request.rejectUnauthorized = false;
68+
request(options.request, this.callback);
69+
},
70+
"should not succeed": function (err, res, body) {
71+
assert.notStrictEqual(err,null);
72+
}
73+
};
74+
};
75+
5276
//
5377
// ### function assertProxied (options)
5478
// #### @options {Object} Options for this test
@@ -63,14 +87,17 @@ exports.assertRequest = function (options) {
6387
exports.assertProxied = function (options) {
6488
options = options || {};
6589

66-
var ports = options.ports || helpers.nextPortPair,
90+
var ports = options.ports || helpers.nextPortPair,
6791
output = options.output || 'hello world from ' + ports.target,
6892
outputHeaders = options.outputHeaders,
6993
targetHeaders = options.targetHeaders,
7094
proxyHeaders = options.proxyHeaders,
7195
protocol = helpers.protocols.proxy,
72-
req = options.request || {};
73-
96+
req = options.request || {},
97+
timeout = options.timeout || null,
98+
assertFn = options.shouldFail
99+
? exports.assertFailedRequest
100+
: exports.assertRequest;
74101

75102
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
76103

@@ -85,7 +112,8 @@ exports.assertProxied = function (options) {
85112
output: output,
86113
outputHeaders: targetHeaders,
87114
port: ports.target,
88-
headers: req.headers
115+
headers: req.headers,
116+
latency: options.requestLatency
89117
},
90118
proxy: {
91119
latency: options.latency,
@@ -97,12 +125,13 @@ exports.assertProxied = function (options) {
97125
https: helpers.protocols.target === 'https',
98126
host: '127.0.0.1',
99127
port: ports.target
100-
}
128+
},
129+
timeout: timeout
101130
}
102131
}
103132
}, this.callback);
104133
},
105-
"the proxy request": exports.assertRequest({
134+
"the proxy request": assertFn({
106135
request: req,
107136
assert: {
108137
headers: outputHeaders,

0 commit comments

Comments
 (0)