Skip to content

Commit 24f22b2

Browse files
authored
Fixing proxy beforeRedirect regression (axios#4708)
1 parent 13dd93f commit 24f22b2

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

lib/adapters/http.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ var isHttps = /https:?/;
2020

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

23+
function dispatchBeforeRedirect(options) {
24+
if (options.beforeRedirects.proxy) {
25+
options.beforeRedirects.proxy(options);
26+
}
27+
if (options.beforeRedirects.config) {
28+
options.beforeRedirects.config(options);
29+
}
30+
}
31+
2332
/**
2433
*
2534
* @param {http.ClientRequestArgs} options
@@ -59,7 +68,7 @@ function setProxy(options, configProxy, location) {
5968
}
6069
}
6170

62-
options.beforeRedirect = function beforeRedirect(redirectOptions) {
71+
options.beforeRedirects.proxy = function beforeRedirect(redirectOptions) {
6372
// Configure proxy for redirected request, passing the original config proxy to apply
6473
// the exact same logic as if the redirected request was performed by axios directly.
6574
setProxy(redirectOptions, configProxy, redirectOptions.href);
@@ -190,7 +199,9 @@ module.exports = function httpAdapter(config) {
190199
headers: headers,
191200
agents: { http: config.httpAgent, https: config.httpsAgent },
192201
auth: auth,
193-
protocol: protocol
202+
protocol: protocol,
203+
beforeRedirect: dispatchBeforeRedirect,
204+
beforeRedirects: {}
194205
};
195206

196207
if (config.socketPath) {
@@ -213,7 +224,7 @@ module.exports = function httpAdapter(config) {
213224
options.maxRedirects = config.maxRedirects;
214225
}
215226
if (config.beforeRedirect) {
216-
options.beforeRedirect = config.beforeRedirect;
227+
options.beforeRedirects.config = config.beforeRedirect;
217228
}
218229
transport = isHttpsRequest ? httpsFollow : httpFollow;
219230
}

test/unit/adapters/http.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe('supports http with nodejs', function () {
204204
assert.equal(res.data, str);
205205
assert.equal(res.request.path, '/two');
206206
done();
207-
}).catch(done);;
207+
}).catch(done);
208208
});
209209
});
210210

@@ -223,7 +223,7 @@ describe('supports http with nodejs', function () {
223223
assert.equal(res.status, 302);
224224
assert.equal(res.headers['location'], '/foo');
225225
done();
226-
}).catch(done);;
226+
}).catch(done);
227227
});
228228
});
229229

@@ -241,7 +241,7 @@ describe('supports http with nodejs', function () {
241241
assert.equal(error.code, AxiosError.ERR_FR_TOO_MANY_REDIRECTS);
242242
assert.equal(error.message, 'Maximum number of redirects exceeded');
243243
done();
244-
});
244+
}).catch(done);
245245
});
246246
});
247247

@@ -263,6 +263,56 @@ describe('supports http with nodejs', function () {
263263
}).catch(function (error) {
264264
assert.equal(error.message, 'Provided path is not allowed');
265265
done();
266+
}).catch(done);
267+
});
268+
});
269+
270+
it('should support beforeRedirect and proxy with redirect', function (done) {
271+
var requestCount = 0;
272+
var totalRedirectCount = 5;
273+
server = http.createServer(function (req, res) {
274+
requestCount += 1;
275+
if (requestCount <= totalRedirectCount) {
276+
res.setHeader('Location', 'http://localhost:4444');
277+
res.writeHead(302);
278+
}
279+
res.end();
280+
}).listen(4444, function () {
281+
var proxyUseCount = 0;
282+
proxy = http.createServer(function (request, response) {
283+
proxyUseCount += 1;
284+
var parsed = url.parse(request.url);
285+
var opts = {
286+
host: parsed.hostname,
287+
port: parsed.port,
288+
path: parsed.path
289+
};
290+
291+
http.get(opts, function (res) {
292+
response.writeHead(res.statusCode, res.headers);
293+
res.on('data', function (data) {
294+
response.write(data)
295+
});
296+
res.on('end', function () {
297+
response.end();
298+
});
299+
});
300+
}).listen(4000, function () {
301+
var configBeforeRedirectCount = 0;
302+
axios.get('http://localhost:4444/', {
303+
proxy: {
304+
host: 'localhost',
305+
port: 4000
306+
},
307+
maxRedirects: totalRedirectCount,
308+
beforeRedirect: function (options) {
309+
configBeforeRedirectCount += 1;
310+
}
311+
}).then(function (res) {
312+
assert.equal(totalRedirectCount, configBeforeRedirectCount, 'should invoke config.beforeRedirect option on every redirect');
313+
assert.equal(totalRedirectCount + 1, proxyUseCount, 'should go through proxy on every redirect');
314+
done();
315+
}).catch(done);
266316
});
267317
});
268318
});

0 commit comments

Comments
 (0)