From 113cfef1b37f7a95d2974f40da3912d29832edae Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 11 Jan 2021 14:25:37 -0800 Subject: [PATCH] fix(connect): provide an error message when ws endpoint is incorrect --- src/browserServerImpl.ts | 6 +----- src/client/browserType.ts | 7 ++++++- test/browsertype-launch-server.spec.ts | 7 +++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/browserServerImpl.ts b/src/browserServerImpl.ts index dd82ab4b7ce3d..e690072257aee 100644 --- a/src/browserServerImpl.ts +++ b/src/browserServerImpl.ts @@ -76,17 +76,13 @@ export class BrowserServerImpl extends EventEmitter implements BrowserServer { this._ready = new Promise(f => readyCallback = f); const token = createGuid(); - this._server = new ws.Server({ port }, () => { + this._server = new ws.Server({ port, path: '/' + token }, () => { const address = this._server.address(); this._wsEndpoint = typeof address === 'string' ? `${address}/${token}` : `ws://127.0.0.1:${address.port}/${token}`; readyCallback(); }); this._server.on('connection', (socket: ws, req) => { - if (req.url !== '/' + token) { - socket.close(); - return; - } this._clientAttached(socket); }); diff --git a/src/client/browserType.ts b/src/client/browserType.ts index 4966101e66332..1ed28ea08ac43 100644 --- a/src/client/browserType.ts +++ b/src/client/browserType.ts @@ -147,6 +147,10 @@ export class BrowserType extends ChannelOwner { + const prematureCloseListener = (event: { reason: string }) => { + reject(new Error('Server disconnected: ' + event.reason)); + }; + ws.addEventListener('close', prematureCloseListener); const remoteBrowser = await connection.waitForObjectWithKnownName('remoteBrowser') as RemoteBrowser; // Inherit shared selectors for connected browser. @@ -165,6 +169,7 @@ export class BrowserType extends ChannelOwner { sharedSelectors._removeChannel(selectorsOwner); @@ -175,7 +180,7 @@ export class BrowserType extends ChannelOwner { ws.close(); - reject(new Error('WebSocket error: ' + event.message)); + reject(new Error(event.message + '. Most likely ws endpoint is incorrect')); }); }); }, logger); diff --git a/test/browsertype-launch-server.spec.ts b/test/browsertype-launch-server.spec.ts index d5a23f3af400d..1a6f86d325ff1 100644 --- a/test/browsertype-launch-server.spec.ts +++ b/test/browsertype-launch-server.spec.ts @@ -33,6 +33,13 @@ describe('lauch server', (suite, { mode }) => { await browserServer.close(); }); + it('should provide an error when ws endpoint is incorrect', async ({browserType, browserOptions}) => { + const browserServer = await browserType.launchServer(browserOptions); + const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint() + '-foo' }).catch(e => e); + await browserServer.close(); + expect(error.message).toContain('Most likely ws endpoint is incorrect'); + }); + it('should fire "close" event during kill', async ({browserType, browserOptions}) => { const order = []; const browserServer = await browserType.launchServer(browserOptions);