test: failing test for websockets + offline context (#4912)
This commit is contained in:
parent
fdb3c1f153
commit
061f9ea68a
|
|
@ -17,6 +17,7 @@
|
||||||
import { folio as base } from 'folio';
|
import { folio as base } from 'folio';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import socks from 'socksv5';
|
import socks from 'socksv5';
|
||||||
|
import { Server as WebSocketServer } from 'ws';
|
||||||
import { TestServer } from '../utils/testserver';
|
import { TestServer } from '../utils/testserver';
|
||||||
|
|
||||||
type HttpWorkerFixtures = {
|
type HttpWorkerFixtures = {
|
||||||
|
|
@ -28,6 +29,7 @@ type HttpWorkerFixtures = {
|
||||||
type HttpTestFixtures = {
|
type HttpTestFixtures = {
|
||||||
server: TestServer;
|
server: TestServer;
|
||||||
httpsServer: TestServer;
|
httpsServer: TestServer;
|
||||||
|
webSocketServer: WebSocketServer;
|
||||||
};
|
};
|
||||||
|
|
||||||
const fixtures = base.extend<HttpTestFixtures, HttpWorkerFixtures>();
|
const fixtures = base.extend<HttpTestFixtures, HttpWorkerFixtures>();
|
||||||
|
|
@ -35,7 +37,7 @@ fixtures.httpService.init(async ({ testWorkerIndex }, test) => {
|
||||||
const assetsPath = path.join(__dirname, 'assets');
|
const assetsPath = path.join(__dirname, 'assets');
|
||||||
const cachedPath = path.join(__dirname, 'assets', 'cached');
|
const cachedPath = path.join(__dirname, 'assets', 'cached');
|
||||||
|
|
||||||
const port = 8907 + testWorkerIndex * 2;
|
const port = 8907 + testWorkerIndex * 3;
|
||||||
const server = await TestServer.create(assetsPath, port);
|
const server = await TestServer.create(assetsPath, port);
|
||||||
server.enableHTTPCache(cachedPath);
|
server.enableHTTPCache(cachedPath);
|
||||||
|
|
||||||
|
|
@ -65,6 +67,14 @@ fixtures.httpsServer.init(async ({ httpService }, test) => {
|
||||||
await test(httpService.httpsServer);
|
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) => {
|
fixtures.socksPort.init(async ({ testWorkerIndex }, run) => {
|
||||||
const server = socks.createServer((info, accept, deny) => {
|
const server = socks.createServer((info, accept, deny) => {
|
||||||
let socket;
|
let socket;
|
||||||
|
|
|
||||||
|
|
@ -169,3 +169,30 @@ it('should reject waitForEvent on page close', async ({page, server}) => {
|
||||||
await page.close();
|
await page.close();
|
||||||
expect((await error).message).toContain('Page closed');
|
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');
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue