Skip to content

Commit

Permalink
Fixing proxy beforeRedirect regression
Browse files Browse the repository at this point in the history
  • Loading branch information
mbargiel committed May 10, 2022
1 parent 1e55acd commit e8b0b8c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/adapters/http.js
Expand Up @@ -20,6 +20,18 @@ var isHttps = /https:?/;

var supportedProtocols = [ 'http:', 'https:', 'file:' ];

function appendBeforeRedirect(options, beforeRedirect) {
var previousBeforeRedirect = options.beforeRedirect;
if (previousBeforeRedirect) {
options.beforeRedirect = function (redirectOptions) {
previousBeforeRedirect(redirectOptions);
beforeRedirect(redirectOptions);
}
} else {
options.beforeRedirect = beforeRedirect;
}
}

/**
*
* @param {http.ClientRequestArgs} options
Expand Down Expand Up @@ -59,11 +71,11 @@ function setProxy(options, configProxy, location) {
}
}

options.beforeRedirect = function beforeRedirect(redirectOptions) {
appendBeforeRedirect(options, function beforeRedirect(redirectOptions) {
// Configure proxy for redirected request, passing the original config proxy to apply
// the exact same logic as if the redirected request was performed by axios directly.
setProxy(redirectOptions, configProxy, redirectOptions.href);
};
});
}

/*eslint consistent-return:0*/
Expand Down Expand Up @@ -213,7 +225,7 @@ module.exports = function httpAdapter(config) {
options.maxRedirects = config.maxRedirects;
}
if (config.beforeRedirect) {
options.beforeRedirect = config.beforeRedirect;
appendBeforeRedirect(options, config.beforeRedirect);
}
transport = isHttpsRequest ? httpsFollow : httpFollow;
}
Expand Down
49 changes: 49 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -265,6 +265,55 @@ describe('supports http with nodejs', function () {
});
});

it('should support beforeRedirect and proxy with redirect', function (done) {
var requestCount = 0;
server = http.createServer(function (req, res) {
requestCount += 1;
if (requestCount === 1) {
res.setHeader('Location', '/redirected');
res.writeHead(302);
}
res.end();
}).listen(4444, function () {
var proxyUseCount = 0;
proxy = http.createServer(function (request, response) {
proxyUseCount += 1;
var parsed = url.parse(request.url);
var opts = {
host: parsed.hostname,
port: parsed.port,
path: parsed.path
};

http.get(opts, function (res) {
response.writeHead(res.statusCode, res.headers);
res.on('data', function (data) {
response.write(data)
});
res.on('end', function () {
response.end();
});
});
}).listen(4000, function () {
var redirectCalled = false;
axios.get('http://localhost:4444/', {
proxy: {
host: 'localhost',
port: 4000
},
maxRedirects: 3,
beforeRedirect: function (options) {
redirectCalled = true;
}
}).then(function (res) {
assert.ok(redirectCalled, 'should invoke beforeRedirect option');
assert.equal(2, proxyUseCount, 'should go through proxy after redirect');
done();
}).catch(done);
});
});
});

it('should preserve the HTTP verb on redirect', function (done) {
server = http.createServer(function (req, res) {
if (req.method.toLowerCase() !== "head") {
Expand Down

0 comments on commit e8b0b8c

Please sign in to comment.