test: link preload interception (#16908)

This commit is contained in:
Yury Semikhatsky 2022-08-30 10:35:55 -07:00 committed by GitHub
parent 71f061ea9a
commit 077b8a9289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,5 @@
<script>
let preloadedStyles = false;
</script>
<link rel="preload" href="./one-style.css" as="style" onload="window.preloadedStyles=true;this.onload=null;this.rel='stylesheet'" />
<div>hello, world!</div>

View file

@ -349,3 +349,27 @@ it('should delete the origin header', async ({ page, server, isAndroid, browserN
expect(interceptedRequest.headers()['origin']).toEqual(undefined);
expect(serverRequest.headers.origin).toBeFalsy();
});
it('should continue preload link requests', async ({ page, server, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16745' });
it.fixme(browserName === 'webkit', 'Preload requests are aborted in WebKit when interception is enabled');
let intercepted = false;
await page.route('**/one-style.css', route => {
intercepted = true;
route.continue({
headers: {
...route.request().headers(),
'custom': 'value'
}
});
});
const [serverRequest] = await Promise.all([
server.waitForRequest('/one-style.css'),
page.goto(server.PREFIX + '/preload.html')
]);
expect(serverRequest.headers['custom']).toBe('value');
await page.waitForFunction(() => (window as any).preloadedStyles);
expect(intercepted).toBe(true);
const color = await page.evaluate(() => window.getComputedStyle(document.body).backgroundColor);
expect(color).toBe('rgb(255, 192, 203)');
});

View file

@ -353,3 +353,31 @@ function findResponse(har: har.HARFile, url: string): har.Response {
expect(entry, originalUrl).toBeTruthy();
return entry?.response;
}
it('should fulfill preload link requests', async ({ page, server, browserName }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/16745' });
it.fixme(browserName === 'webkit', 'Preload requests are aborted in WebKit when interception is enabled');
let intercepted = false;
await page.route('**/one-style.css', route => {
intercepted = true;
route.fulfill({
status: 200,
headers: {
'content-type': 'text/css; charset=utf-8',
'cache-control': 'no-cache, no-store',
'custom': 'value'
},
body: 'body { background-color: green; }'
});
});
const [response] = await Promise.all([
page.waitForResponse('**/one-style.css'),
page.goto(server.PREFIX + '/preload.html')
]);
expect(await response.headerValue('custom')).toBe('value');
await page.waitForFunction(() => (window as any).preloadedStyles);
expect(intercepted).toBe(true);
const color = await page.evaluate(() => window.getComputedStyle(document.body).backgroundColor);
expect(color).toBe('rgb(0, 128, 0)');
});