diff --git a/docs/src/mock.md b/docs/src/mock.md index c6f2717dbe..100e988ce0 100644 --- a/docs/src/mock.md +++ b/docs/src/mock.md @@ -567,7 +567,7 @@ Playwright's mocking proxy is an HTTP proxy server that's connected to the curre For browser network mocking, Playwright always knows what browser context and page a request is coming from. But because there's only a single application server shared by multiple concurrent test runs, it cannot know this for server requests! To resolve this, pick one of these two strategies: 1. [Disable parallelism](./test-parallel.md#disable-parallelism), so that there's only a single test at a time. -2. On the server, read the `x-playwright-proxy-port` header of incoming requests. When the mocking proxy is configured, Playwright adds this header to all browser requests. +2. On the server, read the `x-playwright-proxy` header of incoming requests. When the mocking proxy is configured, Playwright adds this header to all browser requests. The second strategy can be hard to integrate for some applications, because it requires access to the current request from where you're making your API requests. If this is possible in your application, this is the recommended approach. @@ -577,7 +577,7 @@ Putting this together, figuring out what proxy to funnel a request should look s ```js const proxyUrl = `http://localhost:8123/`; // 1: Disable Parallelism + hardcode port OR -const proxyUrl = `http://localhost:${$currentHeaders.get('x-playwright-proxy-port')}/`; // 2: Inject proxy port +const proxyUrl = decodeURIComponent(currentHeaders.get('x-playwright-proxy') ?? ''); // 2: Inject proxy port ``` And this is the Playwright config to go with it: @@ -681,11 +681,11 @@ export function register() { if (process.env.NODE_ENV === 'test') { const originalFetch = globalThis.fetch; globalThis.fetch = async (input, init) => { - const proxyPort = (await headers()).get('x-playwright-proxy-port'); - if (!proxyPort) + const proxy = (await headers()).get('x-playwright-proxy'); + if (!proxy) return originalFetch(input, init); const request = new Request(input, init); - return originalFetch(`http://localhost:${proxyPort}/${request.url}`, request); + return originalFetch(decodeURIComponent(proxy) + request.url, request); }; } } @@ -705,11 +705,11 @@ const headersStore = new AsyncLocalStorage(); if (process.env.NODE_ENV === "test") { const originalFetch = globalThis.fetch; globalThis.fetch = async (input, init) => { - const proxyPort = headersStore.getStore()?.get('x-playwright-proxy-port'); - if (!proxyPort) + const proxy = headersStore.getStore()?.get('x-playwright-proxy'); + if (!proxy) return originalFetch(input, init); const request = new Request(input, init); - return originalFetch(`http://localhost:${proxyPort}/${request.url}`, request); + return originalFetch(decodeURIComponent(proxy) + request.url, request); }; } @@ -739,9 +739,9 @@ const serverConfig = { ..., withInterceptors([ (req, next) => { - const proxyPort = inject(REQUEST)?.headers.get('x-playwright-proxy-port'); - if (proxyPort) - req = req.clone({ url: `http://localhost:${proxyPort}/${req.url}` }) + const proxy = inject(REQUEST)?.headers.get('x-playwright-proxy'); + if (proxy) + req = req.clone({ url: decodeURIComponent(proxy) + req.url }) return next(req); }, ]) diff --git a/docs/src/test-api/class-testoptions.md b/docs/src/test-api/class-testoptions.md index e392704e72..91fc733adc 100644 --- a/docs/src/test-api/class-testoptions.md +++ b/docs/src/test-api/class-testoptions.md @@ -680,7 +680,7 @@ export default defineConfig({ ## property: TestOptions.mockingProxy * since: v1.51 - type: <[Object]> - - `port` <[int]|"inject"> What port to start the mocking proxy on. If set to `"inject"`, Playwright will use a free port and inject it into all outgoing requests under the `x-playwright-proxy-port` parameter. + - `port` <[int]|"inject"> What port to start the mocking proxy on. If set to `"inject"`, Playwright will use a free port and inject the proxy URL into all outgoing requests under the `x-playwright-proxy` header. **Usage** diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 3dfe99210e..2283c58bde 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -5894,7 +5894,7 @@ type ConnectOptions = { }; type MockingProxyOptions = { /** - * What port to start the mocking proxy on. If set to `"inject"`, Playwright will use a free port and inject it into all outgoing requests under the `x-playwright-proxy-port` parameter. + * What port to start the mocking proxy on. If set to `"inject"`, Playwright will use a free port and inject the proxy URL it into all outgoing requests under the `x-playwright-proxy` header. */ port: number | "inject"; } diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 1cf650c15b..cbefc4b5b1 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -227,7 +227,7 @@ type ConnectOptions = { }; type MockingProxyOptions = { /** - * What port to start the mocking proxy on. If set to `"inject"`, Playwright will use a free port and inject it into all outgoing requests under the `x-playwright-proxy-port` parameter. + * What port to start the mocking proxy on. If set to `"inject"`, Playwright will use a free port and inject the proxy URL it into all outgoing requests under the `x-playwright-proxy` header. */ port: number | "inject"; }