Skip to content

Commit

Permalink
fix(fetch): update host header on redirect (#26750)
Browse files Browse the repository at this point in the history
Fixes #26743
  • Loading branch information
yury-s authored Aug 28, 2023
1 parent 5c72cbd commit 501ed32
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -308,6 +308,7 @@ export abstract class APIRequestContext extends SdkObject {
removeHeader(headers, `content-type`);
}


const redirectOptions: SendRequestOptions = {
method,
headers,
Expand All @@ -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();
Expand Down
31 changes: 30 additions & 1 deletion tests/library/browsercontext-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,4 +1123,33 @@ it('should support set-cookie with SameSite and without Secure attribute over HT
expect(cookie.sameSite).toBe(value);
});
}
});
});

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);
});

0 comments on commit 501ed32

Please sign in to comment.