-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { MockClient } from './lib/MockClient'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { MockServer } from './lib/MockServer'; | ||
export * from './lib/MockServerAction'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
import WebSocket from 'ws'; | ||
import { MockPeer } from './MockPeer'; | ||
import { MockServerAction } from './MockServerAction'; | ||
|
||
export class MockServer extends MockPeer {} | ||
export class MockServer extends MockPeer { | ||
get mockClientWebSocket(): WebSocket { | ||
return this.peerWebSocket as any; | ||
} | ||
|
||
public async runActions(...actions: readonly MockServerAction[]): Promise<void> { | ||
for (const action of actions) { | ||
await action.run(this); | ||
} | ||
} | ||
|
||
public async acceptConnection(): Promise<void> { | ||
await new Promise((resolve) => { | ||
this.peerWebSocket.once('open', resolve); | ||
this.peerWebSocket.emit('open'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// tslint:disable:max-classes-per-file | ||
|
||
import { Data } from 'ws'; | ||
import { CloseFrame } from './CloseFrame'; | ||
import { MockServer } from './MockServer'; | ||
|
||
export abstract class MockServerAction { | ||
// tslint:disable-next-line:readonly-keyword | ||
protected _wasRun = false; | ||
|
||
get wasRun(): boolean { | ||
return this._wasRun; | ||
} | ||
|
||
public async run(_mockServer: MockServer): Promise<void> { | ||
// tslint:disable-next-line:no-object-mutation | ||
this._wasRun = true; | ||
} | ||
} | ||
|
||
export class AcceptConnectionAction extends MockServerAction { | ||
public async run(mockServer: MockServer): Promise<void> { | ||
await mockServer.acceptConnection(); | ||
await super.run(mockServer); | ||
} | ||
} | ||
|
||
export class CloseConnectionAction extends MockServerAction { | ||
constructor(protected readonly closeFrame?: CloseFrame) { | ||
super(); | ||
} | ||
|
||
public async run(mockServer: MockServer): Promise<void> { | ||
await mockServer.close(this.closeFrame?.code, this.closeFrame?.reason); | ||
await super.run(mockServer); | ||
} | ||
} | ||
|
||
export class SendMessageAction extends MockServerAction { | ||
constructor(protected readonly message: Data) { | ||
super(); | ||
} | ||
|
||
public async run(mockServer: MockServer): Promise<void> { | ||
await mockServer.send(this.message); | ||
await super.run(mockServer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters