Skip to content

Commit

Permalink
test: unflake two tests (#32879)
Browse files Browse the repository at this point in the history
- the test that closes a context must not be a "page test";
- account for stray browser requests in the proxy test.
  • Loading branch information
dgozman authored Sep 30, 2024
1 parent c67a733 commit 6f99d48
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
20 changes: 20 additions & 0 deletions tests/library/browsercontext-network-event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(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');
});
18 changes: 10 additions & 8 deletions tests/library/fetch-proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ it.skip(({ mode }) => mode !== 'default');

it('context request should pick up proxy credentials', async ({ browserType, server, proxyServer }) => {
proxyServer.forwardTo(server.PORT, { allowConnectRequests: true });
let auth;
proxyServer.setAuthHandler(req => {
const header = req.headers['proxy-authorization'];
// Browser can issue various unrelated requests over the proxy,
// but we are only interested in our own request.
if (proxyServer.connectHosts.includes('non-existent.com:80'))
auth = header;
return !!header;
const authPromise = new Promise<string>(resolve => {
proxyServer.setAuthHandler(req => {
const header = req.headers['proxy-authorization'];
// Browser can issue various unrelated requests over the proxy,
// but we are only interested in our own request.
if (proxyServer.connectHosts.includes('non-existent.com:80'))
resolve(header);
return !!header;
});
});
const browser = await browserType.launch({
proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }
});
const context = await browser.newContext();
const response = await context.request.get('http://non-existent.com/simple.json');
expect(proxyServer.connectHosts).toContain('non-existent.com:80');
const auth = await authPromise;
expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));
expect(await response.json()).toEqual({ foo: 'bar' });
await browser.close();
Expand Down
20 changes: 0 additions & 20 deletions tests/page/page-network-response.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,6 @@ it('should reject response.finished if page closes', async ({ page, server }) =>
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 }) => {
const response = await page.goto(server.PREFIX + '/simple.json');
expect(await response.json()).toEqual({ foo: 'bar' });
Expand Down

0 comments on commit 6f99d48

Please sign in to comment.