test: failing test for websockets + offline context (#4912)

This commit is contained in:
Joel Einbinder 2021-03-29 20:24:12 -07:00 committed by GitHub
parent fdb3c1f153
commit 061f9ea68a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -17,6 +17,7 @@
import { folio as base } from 'folio';
import path from 'path';
import socks from 'socksv5';
import { Server as WebSocketServer } from 'ws';
import { TestServer } from '../utils/testserver';
type HttpWorkerFixtures = {
@ -28,6 +29,7 @@ type HttpWorkerFixtures = {
type HttpTestFixtures = {
server: TestServer;
httpsServer: TestServer;
webSocketServer: WebSocketServer;
};
const fixtures = base.extend<HttpTestFixtures, HttpWorkerFixtures>();
@ -35,7 +37,7 @@ fixtures.httpService.init(async ({ testWorkerIndex }, test) => {
const assetsPath = path.join(__dirname, 'assets');
const cachedPath = path.join(__dirname, 'assets', 'cached');
const port = 8907 + testWorkerIndex * 2;
const port = 8907 + testWorkerIndex * 3;
const server = await TestServer.create(assetsPath, port);
server.enableHTTPCache(cachedPath);
@ -65,6 +67,14 @@ fixtures.httpsServer.init(async ({ httpService }, test) => {
await test(httpService.httpsServer);
});
fixtures.webSocketServer.init(async ({ testWorkerIndex }, run) => {
const webSocketServer = new WebSocketServer({
port: 8907 + testWorkerIndex * 3 + 2,
});
await run(webSocketServer);
await new Promise(x => webSocketServer.close(x));
});
fixtures.socksPort.init(async ({ testWorkerIndex }, run) => {
const server = socks.createServer((info, accept, deny) => {
let socket;

View File

@ -169,3 +169,30 @@ it('should reject waitForEvent on page close', async ({page, server}) => {
await page.close();
expect((await error).message).toContain('Page closed');
});
it('should turn off when offline', test => {
test.fixme();
}, async ({page, webSocketServer}) => {
const address = webSocketServer.address();
const [socket, wsHandle] = await Promise.all([
new Promise<import('ws')>(x => webSocketServer.once('connection', x)),
page.evaluateHandle(async address => {
const ws = new WebSocket(`ws://${address}/`);
await new Promise(x => ws.onopen = x);
return ws;
}, typeof address === 'string' ? address : 'localhost:' + address.port),
]);
const failurePromise = new Promise(x => socket.on('message', data => x(data)));
const closePromise = wsHandle.evaluate(async ws => {
if (ws.readyState !== WebSocket.CLOSED)
await new Promise(x => ws.onclose = x);
return 'successfully closed';
});
const result = Promise.race([
failurePromise,
closePromise
]);
await page.context().setOffline(true);
await wsHandle.evaluate(ws => ws.send('if this arrives it failed'));
expect(await result).toBe('successfully closed');
});