Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(fetch): update host header on redirect (#26750)
Yury Semikhatsky501ed3285638
diff --git a/packages/playwright-core/src/server/fetch.ts b/packages/playwright-core/src/server/fetch.tsindex 58921d11f4..d439d393a3 100644--- a/packages/playwright-core/src/server/fetch.ts+++ b/packages/playwright-core/src/server/fetch.ts@@ -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();diff --git a/tests/library/browsercontext-fetch.spec.ts b/tests/library/browsercontext-fetch.spec.tsindex 913c21c664..de0da3a118 100644--- a/tests/library/browsercontext-fetch.spec.ts+++ b/tests/library/browsercontext-fetch.spec.ts@@ -1123,4 +1123,33 @@ it('should support set-cookie with SameSite and without Secure attribute over HTexpect(cookie.sameSite).toBe(value);});}-});\ No newline at end of file+});++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);+});