fix(har): favicon redirect handling (#8176)

This commit is contained in:
Chad Sheets 2021-09-02 08:31:25 -07:00 committed by GitHub
parent 0202cdf797
commit 23daf84cdd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 1 deletions

View file

@ -119,7 +119,7 @@ export class Request extends SdkObject {
this._headers = headers;
for (const { name, value } of this._headers)
this._headersMap.set(name.toLowerCase(), value);
this._isFavicon = url.endsWith('/favicon.ico');
this._isFavicon = url.endsWith('/favicon.ico') || !!redirectedFrom?._isFavicon;
}
_setFailureText(failureText: string) {

View file

@ -107,3 +107,46 @@ it('should fire events in proper order', async ({browser, server}) => {
'requestfinished',
]);
});
it('should not fire events for favicon or favicon redirects', async ({browser, server, browserName, channel, headless}) => {
it.skip(headless && browserName !== 'firefox', 'headless browsers, except firefox, do not request favicons');
it.skip(!headless && browserName === 'webkit' && !channel, 'headed webkit does not have a favicon feature');
const context = await browser.newContext();
const page = await context.newPage();
const favicon = `/favicon.ico`;
const hashedFaviconUrl = `/favicon-hashed.ico`;
const imagePath = `/fakeimage.png`;
const pagePath = `/page.html`;
server.setRedirect(favicon, hashedFaviconUrl);
server.setRoute(pagePath, (_, res) => {
res.end(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="icon" type="image/svg+xml" href="${favicon}">
<title>SVG Favicon Test</title>
</head>
<body>
<img src="${imagePath}" alt="my fake image">
</body>
</html>
`);
});
const events = [];
context.on('request', req => events.push(req.url()));
context.on('response', res => events.push(res.url()));
context.on('requestfinished', req => events.push(req.url()));
await Promise.all([
server.waitForRequest(favicon),
server.waitForRequest(hashedFaviconUrl),
page.goto(server.PREFIX + '/page.html'),
]);
expect(events).toEqual(expect.arrayContaining([expect.stringContaining(pagePath)]));
expect(events).toEqual(expect.arrayContaining([expect.stringContaining(imagePath)]));
expect(events).not.toEqual(expect.arrayContaining([expect.stringContaining(favicon)]));
expect(events).not.toEqual(expect.arrayContaining([expect.stringContaining(hashedFaviconUrl)]));
});

View file

@ -511,6 +511,54 @@ it('should contain http2 for http2 requests', async ({ contextFactory, browserNa
server.close();
});
it('should filter favicon and favicon redirects', async ({server, browserName, channel, headless, asset, contextFactory}, testInfo) => {
it.skip(headless && browserName !== 'firefox', 'headless browsers, except firefox, do not request favicons');
it.skip(!headless && browserName === 'webkit' && !channel, 'headed webkit does not have a favicon feature');
const { page, getLog } = await pageWithHar(contextFactory, testInfo);
// Browsers aggresively cache favicons, so force bust with the
// `d` parameter to make iterating on this test more predictable and isolated.
const favicon = `/favicon.ico`;
const hashedFaviconUrl = `/favicon-hashed.ico`;
server.setRedirect(favicon, hashedFaviconUrl);
server.setRoute(hashedFaviconUrl, (req, res) => {
server.serveFile(req, res, asset('media-query-prefers-color-scheme.svg'));
});
server.setRoute('/page.html', (_, res) => {
res.end(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="icon" type="image/svg+xml" href="${favicon}">
<title>SVG Favicon Test</title>
</head>
<body>
favicons
</body>
</html>
`);
});
await Promise.all([
server.waitForRequest(favicon),
server.waitForRequest(hashedFaviconUrl),
page.goto(server.PREFIX + '/page.html'),
]);
await page.waitForTimeout(500);
// Text still being around ensures we haven't actually lost our browser to a crash.
await page.waitForSelector('text=favicons');
// favicon and 302 redirects to favicons should be filtered out of request logs
const log = await getLog();
expect(log.entries.length).toBe(1);
const entry = log.entries[0];
expect(entry.request.url).toBe(server.PREFIX + '/page.html');
});
it('should have different hars for concurrent contexts', async ({ contextFactory }, testInfo) => {
const session0 = await pageWithHar(contextFactory, testInfo, 'test-0.har');
await session0.page.goto('data:text/html,<title>Zero</title>');