mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
Added timeout option and test to test new timeout parameter, added requestFail assertion.
This commit is contained in:
parent
476cbe741f
commit
89d43c20dd
@ -550,6 +550,7 @@ If you have a suggestion for a feature currently not supported, feel free to ope
|
|||||||
xforward: true // enables X-Forwarded-For
|
xforward: true // enables X-Forwarded-For
|
||||||
},
|
},
|
||||||
changeOrigin: false, // changes the origin of the host header to the target URL
|
changeOrigin: false, // changes the origin of the host header to the target URL
|
||||||
|
timeout: 120000 // override the default 2 minute http socket timeout value in milliseconds
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -66,6 +66,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
|
|||||||
//
|
//
|
||||||
this.forward = options.forward;
|
this.forward = options.forward;
|
||||||
this.target = options.target;
|
this.target = options.target;
|
||||||
|
this.timeout = options.timeout;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Setup the necessary instances instance variables for
|
// Setup the necessary instances instance variables for
|
||||||
@ -163,6 +164,9 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
|
|||||||
req.headers['x-forwarded-proto'] = getProto(req);
|
req.headers['x-forwarded-proto'] = getProto(req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(this.timeout) {
|
||||||
|
req.socket.setTimeout(this.timeout);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Emit the `start` event indicating that we have begun the proxy operation.
|
// Emit the `start` event indicating that we have begun the proxy operation.
|
||||||
@ -350,10 +354,17 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
// Handle 'error' events from the `reverseProxy`.
|
// Handle 'error' events from the `reverseProxy`. Setup timeout override if needed
|
||||||
//
|
//
|
||||||
reverseProxy.once('error', proxyError);
|
reverseProxy.once('error', proxyError);
|
||||||
|
|
||||||
|
// Set a timeout on the socket if `this.timeout` is specified.
|
||||||
|
reverseProxy.once('socket', function (socket) {
|
||||||
|
if (self.timeout) {
|
||||||
|
socket.setTimeout(self.timeout);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
// Handle 'error' events from the `req` (e.g. `Parse Error`).
|
// Handle 'error' events from the `req` (e.g. `Parse Error`).
|
||||||
//
|
//
|
||||||
|
|||||||
@ -67,9 +67,11 @@ exports.createServer = function (options, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
setTimeout(function() {
|
||||||
res.write(options.output || 'hello proxy');
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||||
res.end();
|
res.write(options.output || 'hello proxy');
|
||||||
|
res.end();
|
||||||
|
}, options.latency || 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
var server = protocols.target === 'https'
|
var server = protocols.target === 'https'
|
||||||
|
|||||||
@ -76,7 +76,12 @@ vows.describe(helpers.describe()).addBatch({
|
|||||||
outputHeaders: { "x-testheader": "target" },
|
outputHeaders: { "x-testheader": "target" },
|
||||||
latency: 1000
|
latency: 1000
|
||||||
})
|
})
|
||||||
}
|
},
|
||||||
|
"and timeout set": macros.http.assertProxied({
|
||||||
|
shouldFail: true,
|
||||||
|
timeout: 2000,
|
||||||
|
requestLatency: 4000
|
||||||
|
})
|
||||||
},
|
},
|
||||||
"With a no valid target server": {
|
"With a no valid target server": {
|
||||||
"and no latency": macros.http.assertInvalidProxy(),
|
"and no latency": macros.http.assertInvalidProxy(),
|
||||||
|
|||||||
@ -49,6 +49,30 @@ exports.assertRequest = function (options) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// ### function assertFailedRequest (options)
|
||||||
|
// #### @options {Object} Options for this failed request assertion.
|
||||||
|
// #### @request {Object} Options to use for `request`.
|
||||||
|
// #### @assert {Object} Test assertions against the response.
|
||||||
|
//
|
||||||
|
// Makes a request using `options.request` and then asserts the response
|
||||||
|
// and body against anything in `options.assert`.
|
||||||
|
//
|
||||||
|
exports.assertFailedRequest = function (options) {
|
||||||
|
return {
|
||||||
|
topic: function () {
|
||||||
|
//
|
||||||
|
// Now make the HTTP request and assert.
|
||||||
|
//
|
||||||
|
options.request.rejectUnauthorized = false;
|
||||||
|
request(options.request, this.callback);
|
||||||
|
},
|
||||||
|
"should not succeed": function (err, res, body) {
|
||||||
|
assert.notStrictEqual(err,null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function assertProxied (options)
|
// ### function assertProxied (options)
|
||||||
// #### @options {Object} Options for this test
|
// #### @options {Object} Options for this test
|
||||||
@ -63,14 +87,17 @@ exports.assertRequest = function (options) {
|
|||||||
exports.assertProxied = function (options) {
|
exports.assertProxied = function (options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
var ports = options.ports || helpers.nextPortPair,
|
var ports = options.ports || helpers.nextPortPair,
|
||||||
output = options.output || 'hello world from ' + ports.target,
|
output = options.output || 'hello world from ' + ports.target,
|
||||||
outputHeaders = options.outputHeaders,
|
outputHeaders = options.outputHeaders,
|
||||||
targetHeaders = options.targetHeaders,
|
targetHeaders = options.targetHeaders,
|
||||||
proxyHeaders = options.proxyHeaders,
|
proxyHeaders = options.proxyHeaders,
|
||||||
protocol = helpers.protocols.proxy,
|
protocol = helpers.protocols.proxy,
|
||||||
req = options.request || {};
|
req = options.request || {},
|
||||||
|
timeout = options.timeout || null,
|
||||||
|
assertFn = options.shouldFail
|
||||||
|
? exports.assertFailedRequest
|
||||||
|
: exports.assertRequest;
|
||||||
|
|
||||||
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
|
req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;
|
||||||
|
|
||||||
@ -85,7 +112,8 @@ exports.assertProxied = function (options) {
|
|||||||
output: output,
|
output: output,
|
||||||
outputHeaders: targetHeaders,
|
outputHeaders: targetHeaders,
|
||||||
port: ports.target,
|
port: ports.target,
|
||||||
headers: req.headers
|
headers: req.headers,
|
||||||
|
latency: options.requestLatency
|
||||||
},
|
},
|
||||||
proxy: {
|
proxy: {
|
||||||
latency: options.latency,
|
latency: options.latency,
|
||||||
@ -97,12 +125,13 @@ exports.assertProxied = function (options) {
|
|||||||
https: helpers.protocols.target === 'https',
|
https: helpers.protocols.target === 'https',
|
||||||
host: '127.0.0.1',
|
host: '127.0.0.1',
|
||||||
port: ports.target
|
port: ports.target
|
||||||
}
|
},
|
||||||
|
timeout: timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, this.callback);
|
}, this.callback);
|
||||||
},
|
},
|
||||||
"the proxy request": exports.assertRequest({
|
"the proxy request": assertFn({
|
||||||
request: req,
|
request: req,
|
||||||
assert: {
|
assert: {
|
||||||
headers: outputHeaders,
|
headers: outputHeaders,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user