Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connect): provide an error message when ws endpoint is incorrect #4978

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/browserServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,13 @@ export class BrowserServerImpl extends EventEmitter implements BrowserServer {
this._ready = new Promise<void>(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);
});

Expand Down
7 changes: 6 additions & 1 deletion src/client/browserType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
}
}
ws.addEventListener('open', async () => {
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.
Expand All @@ -165,6 +169,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
}
browser._didClose();
};
ws.removeEventListener('close', prematureCloseListener);
ws.addEventListener('close', closeListener);
browser.on(Events.Browser.Disconnected, () => {
sharedSelectors._removeChannel(selectorsOwner);
Expand All @@ -175,7 +180,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
});
ws.addEventListener('error', event => {
ws.close();
reject(new Error('WebSocket error: ' + event.message));
reject(new Error(event.message + '. Most likely ws endpoint is incorrect'));
});
});
}, logger);
Expand Down
7 changes: 7 additions & 0 deletions test/browsertype-launch-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down