test: add a test for request interception with redirects (#3994)

This commit is contained in:
Tom Jenkinson 2020-10-02 03:55:39 +01:00 committed by GitHub
parent 81c1daed73
commit c2171218fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -1654,6 +1654,8 @@ Routing provides the capability to modify network requests that are made by a pa
Once routing is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted. Once routing is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted.
> **NOTE** The handler will only be called for the first url if the response is a redirect.
An example of a naïve handler that aborts all image requests: An example of a naïve handler that aborts all image requests:
```js ```js

View File

@ -37,14 +37,40 @@ it('should work for subframe navigation request', async ({page, server}) => {
it('should work for fetch requests', async ({page, server}) => { it('should work for fetch requests', async ({page, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
let requests = []; const requests = [];
page.on('request', request => requests.push(request)); page.on('request', request => requests.push(request));
await page.evaluate(() => fetch('/digits/1.png')); await page.evaluate(() => fetch('/digits/1.png'));
requests = requests.filter(request => !request.url().includes('favicon'));
expect(requests.length).toBe(1); expect(requests.length).toBe(1);
expect(requests[0].frame()).toBe(page.mainFrame()); expect(requests[0].frame()).toBe(page.mainFrame());
}); });
it('should work for a redirect', async ({page, server}) => {
server.setRedirect('/foo.html', '/empty.html');
const requests = [];
page.on('request', request => requests.push(request));
await page.goto(server.PREFIX + '/foo.html');
expect(requests.length).toBe(2);
expect(requests[0].url()).toBe(server.PREFIX + '/foo.html');
expect(requests[1].url()).toBe(server.PREFIX + '/empty.html');
});
// https://github.com/microsoft/playwright/issues/3993
it('should not work for a redirect and interception', async ({page, server}) => {
server.setRedirect('/foo.html', '/empty.html');
const requests = [];
await page.route('**', route => {
requests.push(route.request());
route.continue();
});
await page.goto(server.PREFIX + '/foo.html');
expect(page.url()).toBe(server.PREFIX + '/empty.html');
expect(requests.length).toBe(1);
expect(requests[0].url()).toBe(server.PREFIX + '/foo.html');
});
it('should return headers', async ({page, server, isChromium, isFirefox, isWebKit}) => { it('should return headers', async ({page, server, isChromium, isFirefox, isWebKit}) => {
const response = await page.goto(server.EMPTY_PAGE); const response = await page.goto(server.EMPTY_PAGE);
if (isChromium) if (isChromium)