Skip to content

Commit c217121

Browse files
authored
test: add a test for request interception with redirects (#3994)
1 parent 81c1dae commit c217121

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,8 @@ Routing provides the capability to modify network requests that are made by a pa
16541654

16551655
Once routing is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted.
16561656

1657+
> **NOTE** The handler will only be called for the first url if the response is a redirect.
1658+
16571659
An example of a naïve handler that aborts all image requests:
16581660

16591661
```js

test/network-request.spec.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,40 @@ it('should work for subframe navigation request', async ({page, server}) => {
3737

3838
it('should work for fetch requests', async ({page, server}) => {
3939
await page.goto(server.EMPTY_PAGE);
40-
let requests = [];
40+
const requests = [];
4141
page.on('request', request => requests.push(request));
4242
await page.evaluate(() => fetch('/digits/1.png'));
43-
requests = requests.filter(request => !request.url().includes('favicon'));
4443
expect(requests.length).toBe(1);
4544
expect(requests[0].frame()).toBe(page.mainFrame());
4645
});
4746

47+
it('should work for a redirect', async ({page, server}) => {
48+
server.setRedirect('/foo.html', '/empty.html');
49+
const requests = [];
50+
page.on('request', request => requests.push(request));
51+
await page.goto(server.PREFIX + '/foo.html');
52+
53+
expect(requests.length).toBe(2);
54+
expect(requests[0].url()).toBe(server.PREFIX + '/foo.html');
55+
expect(requests[1].url()).toBe(server.PREFIX + '/empty.html');
56+
});
57+
58+
// https://github.com/microsoft/playwright/issues/3993
59+
it('should not work for a redirect and interception', async ({page, server}) => {
60+
server.setRedirect('/foo.html', '/empty.html');
61+
const requests = [];
62+
await page.route('**', route => {
63+
requests.push(route.request());
64+
route.continue();
65+
});
66+
await page.goto(server.PREFIX + '/foo.html');
67+
68+
expect(page.url()).toBe(server.PREFIX + '/empty.html');
69+
70+
expect(requests.length).toBe(1);
71+
expect(requests[0].url()).toBe(server.PREFIX + '/foo.html');
72+
});
73+
4874
it('should return headers', async ({page, server, isChromium, isFirefox, isWebKit}) => {
4975
const response = await page.goto(server.EMPTY_PAGE);
5076
if (isChromium)

0 commit comments

Comments
 (0)