update header name
This commit is contained in:
parent
303531a4d7
commit
a0ce23d105
|
|
@ -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:
|
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.
|
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.
|
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.
|
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
|
```js
|
||||||
const proxyUrl = `http://localhost:8123/`; // 1: Disable Parallelism + hardcode port OR
|
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:
|
And this is the Playwright config to go with it:
|
||||||
|
|
@ -681,11 +681,11 @@ export function register() {
|
||||||
if (process.env.NODE_ENV === 'test') {
|
if (process.env.NODE_ENV === 'test') {
|
||||||
const originalFetch = globalThis.fetch;
|
const originalFetch = globalThis.fetch;
|
||||||
globalThis.fetch = async (input, init) => {
|
globalThis.fetch = async (input, init) => {
|
||||||
const proxyPort = (await headers()).get('x-playwright-proxy-port');
|
const proxy = (await headers()).get('x-playwright-proxy');
|
||||||
if (!proxyPort)
|
if (!proxy)
|
||||||
return originalFetch(input, init);
|
return originalFetch(input, init);
|
||||||
const request = new Request(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") {
|
if (process.env.NODE_ENV === "test") {
|
||||||
const originalFetch = globalThis.fetch;
|
const originalFetch = globalThis.fetch;
|
||||||
globalThis.fetch = async (input, init) => {
|
globalThis.fetch = async (input, init) => {
|
||||||
const proxyPort = headersStore.getStore()?.get('x-playwright-proxy-port');
|
const proxy = headersStore.getStore()?.get('x-playwright-proxy');
|
||||||
if (!proxyPort)
|
if (!proxy)
|
||||||
return originalFetch(input, init);
|
return originalFetch(input, init);
|
||||||
const request = new Request(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([
|
withInterceptors([
|
||||||
(req, next) => {
|
(req, next) => {
|
||||||
const proxyPort = inject(REQUEST)?.headers.get('x-playwright-proxy-port');
|
const proxy = inject(REQUEST)?.headers.get('x-playwright-proxy');
|
||||||
if (proxyPort)
|
if (proxy)
|
||||||
req = req.clone({ url: `http://localhost:${proxyPort}/${req.url}` })
|
req = req.clone({ url: decodeURIComponent(proxy) + req.url })
|
||||||
return next(req);
|
return next(req);
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
|
||||||
|
|
@ -680,7 +680,7 @@ export default defineConfig({
|
||||||
## property: TestOptions.mockingProxy
|
## property: TestOptions.mockingProxy
|
||||||
* since: v1.51
|
* since: v1.51
|
||||||
- type: <[Object]>
|
- 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**
|
**Usage**
|
||||||
|
|
||||||
|
|
|
||||||
2
packages/playwright/types/test.d.ts
vendored
2
packages/playwright/types/test.d.ts
vendored
|
|
@ -5894,7 +5894,7 @@ type ConnectOptions = {
|
||||||
};
|
};
|
||||||
type MockingProxyOptions = {
|
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";
|
port: number | "inject";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
utils/generate_types/overrides-test.d.ts
vendored
2
utils/generate_types/overrides-test.d.ts
vendored
|
|
@ -227,7 +227,7 @@ type ConnectOptions = {
|
||||||
};
|
};
|
||||||
type MockingProxyOptions = {
|
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";
|
port: number | "inject";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue