docs: make sure to await route.* methods (#29489)

Fixes https://github.com/microsoft/playwright/issues/29471
Follow-up on https://github.com/microsoft/playwright/pull/28745.
This commit is contained in:
Max Schmitt 2024-02-14 17:57:12 +01:00 committed by GitHub
parent 918c26fa02
commit ce5d970929
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 39 additions and 39 deletions

View file

@ -331,7 +331,7 @@ test('context request will share cookie storage with its browser context', async
[name, value])
)).toEqual(responseCookies);
route.fulfill({
await route.fulfill({
response,
headers: { ...responseHeaders, foo: 'bar' },
});
@ -375,7 +375,7 @@ test('global context request has isolated cookie storage', async ({
new Map(contextCookies2.map(({ name, value }) => [name, value]))
).toEqual(responseCookies);
route.fulfill({
await route.fulfill({
response,
headers: { ...responseHeaders, foo: 'bar' },
});

View file

@ -1120,11 +1120,11 @@ await browser.CloseAsync();
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some post data, and leaving all other requests as is:
```js
await context.route('/api/**', route => {
await context.route('/api/**', async route => {
if (route.request().postData().includes('my-string'))
route.fulfill({ body: 'mocked-data' });
await route.fulfill({ body: 'mocked-data' });
else
route.continue();
await route.continue();
});
```
@ -1138,16 +1138,16 @@ context.route("/api/**", route -> {
```
```python async
def handle_route(route):
async def handle_route(route: Route):
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
await route.fulfill(body="mocked-data")
else:
route.continue_()
await route.continue_()
await context.route("/api/**", handle_route)
```
```python sync
def handle_route(route):
def handle_route(route: Route):
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
else:

View file

@ -3456,11 +3456,11 @@ await page.GotoAsync("https://www.microsoft.com");
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some post data, and leaving all other requests as is:
```js
await page.route('/api/**', route => {
await page.route('/api/**', async route => {
if (route.request().postData().includes('my-string'))
route.fulfill({ body: 'mocked-data' });
await route.fulfill({ body: 'mocked-data' });
else
route.continue();
await route.continue();
});
```
@ -3474,16 +3474,16 @@ page.route("/api/**", route -> {
```
```python async
def handle_route(route):
async def handle_route(route: Route):
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
await route.fulfill(body="mocked-data")
else:
route.continue_()
await route.continue_()
await page.route("/api/**", handle_route)
```
```python sync
def handle_route(route):
def handle_route(route: Route):
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
else:

View file

@ -92,11 +92,11 @@ page.route("**/*", handle)
```
```csharp
await page.RouteAsync("**/*", route =>
await page.RouteAsync("**/*", async route =>
{
var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "bar" } };
headers.Remove("origin");
route.ContinueAsync(headers);
await route.ContinueAsync(new() { Headers = headers });
});
```
@ -264,17 +264,17 @@ page.route("**/*", route -> {
```python async
# Handle GET requests.
def handle_get(route):
async def handle_get(route):
if route.request.method != "GET":
route.fallback()
await route.fallback()
return
# Handling GET only.
# ...
# Handle POST requests.
def handle_post(route):
async def handle_post(route):
if route.request.method != "POST":
route.fallback()
await route.fallback()
return
# Handling POST only.
# ...
@ -378,11 +378,11 @@ page.route("**/*", handle)
```
```csharp
await page.RouteAsync("**/*", route =>
await page.RouteAsync("**/*", async route =>
{
var headers = new Dictionary<string, string>(route.Request.Headers) { { "foo", "foo-value" } };
headers.Remove("bar");
route.FallbackAsync(headers);
await route.FallbackAsync(new() { Headers = headers });
});
```

View file

@ -495,10 +495,10 @@ await page.GotoAsync("https://example.com");
```js
// Delete header
await page.route('**/*', route => {
await page.route('**/*', async route => {
const headers = route.request().headers();
delete headers['X-Secret'];
route.continue({ headers });
await route.continue({ headers });
});
// Continue requests as POST.
@ -522,7 +522,7 @@ page.route("**/*", route -> route.resume(new Route.ResumeOptions().setMethod("PO
async def handle_route(route):
headers = route.request.headers
del headers["x-secret"]
route.continue_(headers=headers)
await route.continue_(headers=headers)
await page.route("**/*", handle_route)
# Continue requests as POST.
@ -617,7 +617,7 @@ await page.route('**/title.html', async route => {
// Add a prefix to the title.
let body = await response.text();
body = body.replace('<title>', '<title>My prefix:');
route.fulfill({
await route.fulfill({
// Pass all fields from the response.
response,
// Override response body.

View file

@ -1233,16 +1233,16 @@ test.beforeEach(async ({ page }) => {
await page.route('**/*', async route => {
const headers = await route.request().allHeaders();
delete headers['if-none-match'];
route.fallback({ headers });
await route.fallback({ headers });
});
});
test('should work', async ({ page }) => {
await page.route('**/*', route => {
await page.route('**/*', async route => {
if (route.request().resourceType() === 'image')
route.abort();
await route.abort();
else
route.fallback();
await route.fallback();
});
});
```
@ -1813,7 +1813,7 @@ test('response interception', async ({ page }) => {
const response = await page._request.fetch(route.request());
const image = await jimp.read(await response.body());
await image.blur(5);
route.fulfill({
await route.fulfill({
response,
body: await image.getBufferAsync('image/jpeg'),
});

View file

@ -3665,11 +3665,11 @@ export interface Page {
* some post data, and leaving all other requests as is:
*
* ```js
* await page.route('/api/**', route => {
* await page.route('/api/**', async route => {
* if (route.request().postData().includes('my-string'))
* route.fulfill({ body: 'mocked-data' });
* await route.fulfill({ body: 'mocked-data' });
* else
* route.continue();
* await route.continue();
* });
* ```
*
@ -8464,11 +8464,11 @@ export interface BrowserContext {
* some post data, and leaving all other requests as is:
*
* ```js
* await context.route('/api/**', route => {
* await context.route('/api/**', async route => {
* if (route.request().postData().includes('my-string'))
* route.fulfill({ body: 'mocked-data' });
* await route.fulfill({ body: 'mocked-data' });
* else
* route.continue();
* await route.continue();
* });
* ```
*