fix(fetch): update host header on redirect (#26750)

Fixes https://github.com/microsoft/playwright/issues/26743
This commit is contained in:
Yury Semikhatsky 2023-08-28 12:42:50 -07:00 committed by GitHub
parent 5c72cbdb23
commit 501ed32856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -291,7 +291,7 @@ export abstract class APIRequestContext extends SdkObject {
request.destroy();
return;
}
const headers: HeadersObject = { ...options.headers };
const headers = { ...options.headers };
removeHeader(headers, `cookie`);
// HTTP-redirect fetch step 13 (https://fetch.spec.whatwg.org/#http-redirect-fetch)
@ -308,6 +308,7 @@ export abstract class APIRequestContext extends SdkObject {
removeHeader(headers, `content-type`);
}
const redirectOptions: SendRequestOptions = {
method,
headers,
@ -331,6 +332,10 @@ export abstract class APIRequestContext extends SdkObject {
request.destroy();
return;
}
if (headers['host'])
headers['host'] = locationURL.host;
notifyRequestFinished();
fulfill(this._sendRequest(progress, locationURL, redirectOptions, postData));
request.destroy();

View File

@ -1124,3 +1124,32 @@ it('should support set-cookie with SameSite and without Secure attribute over HT
});
}
});
it('should update host header on redirect', async ({ context, server }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/26743' });
let redirectCount = 0;
server.setRoute('/redirect', (req, res) => {
redirectCount++;
const path = (req.headers.host === new URL(server.PREFIX).host) ? '/redirect' : '/test';
res.writeHead(302, {
host: new URL(server.CROSS_PROCESS_PREFIX).host,
location: server.CROSS_PROCESS_PREFIX + path,
});
res.end();
});
server.setRoute('/test', (req, res) => {
res.writeHead(200, {
'content-type': 'text/plain',
});
res.end('Hello!');
});
const reqPromise = server.waitForRequest('/test');
const response = await context.request.get(server.PREFIX + '/redirect', {
headers: { host: new URL(server.PREFIX).host }
});
expect(redirectCount).toBe(2);
await expect(response).toBeOK();
expect(await response.text()).toBe('Hello!');
expect((await reqPromise).headers.host).toBe(new URL(server.CROSS_PROCESS_PREFIX).host);
});