test: unflake two tests (#32879)

- the test that closes a context must not be a "page test";
- account for stray browser requests in the proxy test.
This commit is contained in:
Dmitry Gozman 2024-09-30 07:49:18 -07:00 committed by GitHub
parent c67a7335ab
commit 6f99d48a12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 28 deletions

View File

@ -144,3 +144,23 @@ it('should not fire events for favicon or favicon redirects', async ({ context,
expect(events).not.toEqual(expect.arrayContaining([expect.stringContaining(favicon)])); expect(events).not.toEqual(expect.arrayContaining([expect.stringContaining(favicon)]));
expect(events).not.toEqual(expect.arrayContaining([expect.stringContaining(hashedFaviconUrl)])); expect(events).not.toEqual(expect.arrayContaining([expect.stringContaining(hashedFaviconUrl)]));
}); });
it('should reject response.finished if context closes', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
server.setRoute('/get', (req, res) => {
// In Firefox, |fetch| will be hanging until it receives |Content-Type| header
// from server.
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.write('hello ');
});
// send request and wait for server response
const [pageResponse] = await Promise.all([
page.waitForEvent('response'),
page.evaluate(() => fetch('./get', { method: 'GET' })),
]);
const finishPromise = pageResponse.finished().catch(e => e);
await page.context().close();
const error = await finishPromise;
expect(error.message).toContain('closed');
});

View File

@ -20,14 +20,15 @@ it.skip(({ mode }) => mode !== 'default');
it('context request should pick up proxy credentials', async ({ browserType, server, proxyServer }) => { it('context request should pick up proxy credentials', async ({ browserType, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT, { allowConnectRequests: true }); proxyServer.forwardTo(server.PORT, { allowConnectRequests: true });
let auth; const authPromise = new Promise<string>(resolve => {
proxyServer.setAuthHandler(req => { proxyServer.setAuthHandler(req => {
const header = req.headers['proxy-authorization']; const header = req.headers['proxy-authorization'];
// Browser can issue various unrelated requests over the proxy, // Browser can issue various unrelated requests over the proxy,
// but we are only interested in our own request. // but we are only interested in our own request.
if (proxyServer.connectHosts.includes('non-existent.com:80')) if (proxyServer.connectHosts.includes('non-existent.com:80'))
auth = header; resolve(header);
return !!header; return !!header;
});
}); });
const browser = await browserType.launch({ const browser = await browserType.launch({
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' } proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }
@ -35,6 +36,7 @@ it('context request should pick up proxy credentials', async ({ browserType, ser
const context = await browser.newContext(); const context = await browser.newContext();
const response = await context.request.get('http://non-existent.com/simple.json'); const response = await context.request.get('http://non-existent.com/simple.json');
expect(proxyServer.connectHosts).toContain('non-existent.com:80'); expect(proxyServer.connectHosts).toContain('non-existent.com:80');
const auth = await authPromise;
expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64')); expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));
expect(await response.json()).toEqual({ foo: 'bar' }); expect(await response.json()).toEqual({ foo: 'bar' });
await browser.close(); await browser.close();

View File

@ -129,26 +129,6 @@ it('should reject response.finished if page closes', async ({ page, server }) =>
expect(error.message).toContain('closed'); expect(error.message).toContain('closed');
}); });
it('should reject response.finished if context closes', async ({ page, server }) => {
await page.goto(server.EMPTY_PAGE);
server.setRoute('/get', (req, res) => {
// In Firefox, |fetch| will be hanging until it receives |Content-Type| header
// from server.
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.write('hello ');
});
// send request and wait for server response
const [pageResponse] = await Promise.all([
page.waitForEvent('response'),
page.evaluate(() => fetch('./get', { method: 'GET' })),
]);
const finishPromise = pageResponse.finished().catch(e => e);
await page.context().close();
const error = await finishPromise;
expect(error.message).toContain('closed');
});
it('should return json', async ({ page, server }) => { it('should return json', async ({ page, server }) => {
const response = await page.goto(server.PREFIX + '/simple.json'); const response = await page.goto(server.PREFIX + '/simple.json');
expect(await response.json()).toEqual({ foo: 'bar' }); expect(await response.json()).toEqual({ foo: 'bar' });