Add followRedirects option

This commit is contained in:
n30n0v 2017-08-17 18:22:44 +03:00 committed by Charlie Robbins
parent 6f88caf6e4
commit c9a556cfa5
4 changed files with 50 additions and 4 deletions

View File

@ -375,6 +375,7 @@ proxyServer.listen(8015);
* **headers**: object with extra headers to be added to target requests.
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
* **selfHandleRequest** true/false, if set to true, none of the webOutgoing passes are called and its your responsibility ro appropriately return the response by listening and acting on the `proxyRes` event
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:

View File

@ -1,12 +1,15 @@
var http = require('http'),
https = require('https'),
var httpNative = require('http'),
httpsNative = require('https'),
web_o = require('./web-outgoing'),
common = require('../common');
common = require('../common'),
followRedirects = require('follow-redirects');
web_o = Object.keys(web_o).map(function(pass) {
return web_o[pass];
});
var nativeAgents = { http: httpNative, https: httpsNative };
/*!
* Array of passes.
*
@ -99,6 +102,10 @@ module.exports = {
// And we begin!
server.emit('start', req, res, options.target || options.forward);
var agents = options.followRedirects ? followRedirects : nativeAgents;
var http = agents.http;
var https = agents.https;
if(options.forward) {
// If forward enable, so just pipe the request
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(

View File

@ -13,7 +13,8 @@
"main": "index.js",
"dependencies": {
"eventemitter3": "^3.0.0",
"requires-port": "^1.0.0"
"requires-port": "^1.0.0",
"follow-redirects": "^1.0.0"
},
"devDependencies": {
"async": "^2.0.0",

View File

@ -1,6 +1,7 @@
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
httpProxy = require('../lib/http-proxy'),
expect = require('expect.js'),
url = require('url'),
http = require('http');
describe('lib/http-proxy/passes/web.js', function() {
@ -413,3 +414,39 @@ describe('#createProxyServer.web() using own http server', function () {
http.request('http://127.0.0.1:8080/test2', function() {}).end();
});
});
describe('#followRedirects', function () {
it('should proxy the request follow redirects', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
followRedirects: true
});
function requestHandler(req, res) {
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
if (url.parse(req.url).pathname === '/redirect') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('ok');
}
res.writeHead(301, { 'Location': '/redirect' });
res.end();
});
proxyServer.listen('8081');
source.listen('8080');
http.request('http://127.0.0.1:8081', function(res) {
source.close();
proxyServer.close();
expect(res.statusCode).to.eql(200);
done();
}).end();
});
});