From 061f9ea68a349169899494bc8e287c91580f04a1 Mon Sep 17 00:00:00 2001 From: Joel Einbinder Date: Mon, 29 Mar 2021 20:24:12 -0700 Subject: [PATCH] test: failing test for websockets + offline context (#4912) --- test/http.fixtures.ts | 12 +++++++++++- test/web-socket.spec.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/test/http.fixtures.ts b/test/http.fixtures.ts index 6e04ed5e89..27e20d1a92 100644 --- a/test/http.fixtures.ts +++ b/test/http.fixtures.ts @@ -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(); @@ -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; diff --git a/test/web-socket.spec.ts b/test/web-socket.spec.ts index df57374d27..d7940f8f54 100644 --- a/test/web-socket.spec.ts +++ b/test/web-socket.spec.ts @@ -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(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'); +});