2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-17 19:21:29 +03:00

fix: missing beforeRedirect requestDetails argument (#6241)

This commit is contained in:
Dave Cardwell
2026-04-26 12:14:23 -04:00
committed by GitHub
parent 80324886b1
commit caa3497913
4 changed files with 41 additions and 4 deletions
+2 -1
View File
@@ -495,7 +495,8 @@ declare namespace axios {
maxRate?: number | [MaxUploadRate, MaxDownloadRate];
beforeRedirect?: (
options: Record<string, any>,
responseDetails: { headers: Record<string, string>; statusCode: HttpStatusCode }
responseDetails: { headers: Record<string, string>; statusCode: HttpStatusCode },
requestDetails: { headers: Record<string, string>; url: string; method: string },
) => void;
socketPath?: string | null;
allowedSocketPaths?: string | string[] | null;
Vendored
+6 -1
View File
@@ -394,7 +394,12 @@ export interface AxiosRequestConfig<D = any> {
responseDetails: {
headers: Record<string, string>;
statusCode: HttpStatusCode;
}
},
requestDetails: {
headers: Record<string, string>;
url: string;
method: string;
},
) => void;
socketPath?: string | null;
allowedSocketPaths?: string | string[] | null;
+2 -2
View File
@@ -176,12 +176,12 @@ const http2Sessions = new Http2Sessions();
*
* @returns {Object<string, any>}
*/
function dispatchBeforeRedirect(options, responseDetails) {
function dispatchBeforeRedirect(options, responseDetails, requestDetails) {
if (options.beforeRedirects.proxy) {
options.beforeRedirects.proxy(options);
}
if (options.beforeRedirects.config) {
options.beforeRedirects.config(options, responseDetails);
options.beforeRedirects.config(options, responseDetails, requestDetails);
}
}
+31
View File
@@ -385,6 +385,37 @@ describe('supports http with nodejs', () => {
}
});
it('should pass requestDetails to beforeRedirect with the original URL', async () => {
const server = await startHTTPServer(
(req, res) => {
res.setHeader('Location', '/foo');
res.statusCode = 302;
res.end();
},
{ port: SERVER_PORT }
);
const originalUrl = `http://localhost:${server.address().port}/bar`;
let capturedUrl;
try {
await axios.get(originalUrl, {
maxRedirects: 3,
beforeRedirect: (options, responseDetails, requestDetails) => {
if (options.path === '/foo' && responseDetails.headers.location === '/foo') {
capturedUrl = requestDetails.url;
throw new Error('Provided path is not allowed');
}
},
});
} catch (error) {
assert.strictEqual(error.message, 'Redirected request failed: Provided path is not allowed');
assert.strictEqual(capturedUrl, originalUrl);
} finally {
await stopHTTPServer(server);
}
});
it('should support beforeRedirect and proxy with redirect', async () => {
let requestCount = 0;
let proxyUseCount = 0;