update header name

This commit is contained in:
Simon Knott 2025-01-27 13:19:44 +01:00
parent 303531a4d7
commit a0ce23d105
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
4 changed files with 14 additions and 14 deletions

View file

@ -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<Headers>();
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);
},
])

View file

@ -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**

View file

@ -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";
}

View file

@ -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";
}