Skip to content

Commit

Permalink
feat(MockClient): Prevent connecting to the server more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Jul 28, 2021
1 parent 94a725a commit 979bd77
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib/MockClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { MockPeer } from './MockPeer';
export class MockClient extends MockPeer {
protected readonly socket: Socket;

// tslint:disable-next-line:readonly-keyword
protected connected = false;

constructor(
private wsServer: WSServer,
private headers: { readonly [key: string]: string } = {},
Expand All @@ -22,6 +25,10 @@ export class MockClient extends MockPeer {
}

public async connect(): Promise<void> {
if (this.connected) {
throw new Error('Cannot connect to server again');
}

const incomingMessage = new IncomingMessage(this.socket);
// tslint:disable-next-line:no-object-mutation
incomingMessage.headers = {
Expand All @@ -31,6 +38,10 @@ export class MockClient extends MockPeer {
// tslint:disable-next-line:no-object-mutation
incomingMessage.url = this.url;

// tslint:disable-next-line:no-object-mutation
this.connected = true;

// Only return once the server's own `connection` event handler has been been executed
return new Promise((resolve) => {
this.wsServer.once('connection', resolve);
this.wsServer.emit('connection', this.peerWebSocket, incomingMessage);
Expand Down

0 comments on commit 979bd77

Please sign in to comment.