@relaycorp/ws-mock
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -5,4 +5,3 @@ export { MockClient } from './lib/MockClient'; | ||
export { PingOrPong } from './lib/PingOrPong'; | ||
export * from './lib/MockServerAction'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -20,5 +10,4 @@ exports.createMockWebSocketStream = exports.MockServer = exports.MockPeer = exports.MockClient = void 0; | ||
Object.defineProperty(exports, "MockServer", { enumerable: true, get: function () { return MockServer_1.MockServer; } }); | ||
__exportStar(require("./lib/MockServerAction"), exports); | ||
var stream_1 = require("./lib/stream"); | ||
Object.defineProperty(exports, "createMockWebSocketStream", { enumerable: true, get: function () { return stream_1.createMockWebSocketStream; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,6 @@ /// <reference types="node" /> | ||
protected readonly peerWebSocket: MockWebSocket; | ||
get wasConnectionClosed(): boolean; | ||
protected ownCloseFrame: CloseFrame | null; | ||
protected wasAborted: boolean; | ||
get didPeerCloseConnection(): boolean; | ||
protected get didICloseConnection(): boolean; | ||
close(code?: number, reason?: string): void; | ||
@@ -11,0 +14,0 @@ abort(error: Error): void; |
@@ -12,10 +12,21 @@ "use strict"; | ||
this.peerWebSocket = new MockWebSocket_1.MockWebSocket(); | ||
// tslint:disable-next-line:readonly-keyword | ||
this.ownCloseFrame = null; | ||
// tslint:disable-next-line:readonly-keyword | ||
this.wasAborted = false; | ||
} | ||
get wasConnectionClosed() { | ||
return this.peerWebSocket.closeFrame !== null; | ||
get didPeerCloseConnection() { | ||
return this.peerWebSocket.closeFrame !== null || this.peerWebSocket.wasTerminated; | ||
} | ||
close(code, reason) { | ||
get didICloseConnection() { | ||
return this.ownCloseFrame !== null || this.wasAborted; | ||
} | ||
close(code = 1005, reason) { | ||
// tslint:disable-next-line:readonly-keyword no-object-mutation | ||
this.ownCloseFrame = { code, reason }; | ||
this.peerWebSocket.emit('close', code, reason); | ||
} | ||
abort(error) { | ||
// tslint:disable-next-line:no-object-mutation | ||
this.wasAborted = true; | ||
this.peerWebSocket.emit('error', error); | ||
@@ -96,3 +107,3 @@ } | ||
requireConnectionStillOpen() { | ||
if (this.wasConnectionClosed) { | ||
if (this.didPeerCloseConnection) { | ||
throw new Error('Connection was already closed'); | ||
@@ -99,0 +110,0 @@ } |
import { MockPeer } from './MockPeer'; | ||
import { MockServerAction } from './MockServerAction'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export declare class MockServer extends MockPeer { | ||
get client(): MockWebSocket; | ||
runActions(...actions: readonly MockServerAction[]): Promise<void>; | ||
acceptConnection(): Promise<void>; | ||
use<T>(clientPromise: Promise<T>, serverImplementation?: () => Promise<void>): Promise<T>; | ||
} |
@@ -9,18 +9,22 @@ "use strict"; | ||
} | ||
async runActions(...actions) { | ||
for (const action of actions) { | ||
if (this.wasConnectionClosed) { | ||
break; | ||
} | ||
await action.run(this); | ||
} | ||
async use(clientPromise, serverImplementation) { | ||
const [clientResult] = await Promise.all([ | ||
clientPromise, | ||
new Promise(async (resolve) => { | ||
this.peerWebSocket.emit('open'); | ||
// Give the client enough time to connect | ||
await new Promise(setImmediate); | ||
await (serverImplementation === null || serverImplementation === void 0 ? void 0 : serverImplementation()); | ||
// Allow more time for the client to process the latest actions from the server | ||
await new Promise(setImmediate); | ||
if (!this.didICloseConnection) { | ||
this.close(); | ||
} | ||
resolve(undefined); | ||
}), | ||
]); | ||
return clientResult; | ||
} | ||
async acceptConnection() { | ||
await new Promise((resolve) => { | ||
this.peerWebSocket.once('open', resolve); | ||
this.peerWebSocket.emit('open'); | ||
}); | ||
} | ||
} | ||
exports.MockServer = MockServer; | ||
//# sourceMappingURL=MockServer.js.map |
@@ -5,4 +5,3 @@ export { MockClient } from './lib/MockClient'; | ||
export { PingOrPong } from './lib/PingOrPong'; | ||
export * from './lib/MockServerAction'; | ||
export { CloseFrame } from './lib/CloseFrame'; | ||
export { createMockWebSocketStream } from './lib/stream'; |
export { MockClient } from './lib/MockClient'; | ||
export { MockPeer } from './lib/MockPeer'; | ||
export { MockServer } from './lib/MockServer'; | ||
export * from './lib/MockServerAction'; | ||
export { createMockWebSocketStream } from './lib/stream'; | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,6 @@ /// <reference types="node" /> | ||
protected readonly peerWebSocket: MockWebSocket; | ||
get wasConnectionClosed(): boolean; | ||
protected ownCloseFrame: CloseFrame | null; | ||
protected wasAborted: boolean; | ||
get didPeerCloseConnection(): boolean; | ||
protected get didICloseConnection(): boolean; | ||
close(code?: number, reason?: string): void; | ||
@@ -11,0 +14,0 @@ abort(error: Error): void; |
@@ -5,9 +5,20 @@ import bufferToArray from 'buffer-to-arraybuffer'; | ||
peerWebSocket = new MockWebSocket(); | ||
get wasConnectionClosed() { | ||
return this.peerWebSocket.closeFrame !== null; | ||
// tslint:disable-next-line:readonly-keyword | ||
ownCloseFrame = null; | ||
// tslint:disable-next-line:readonly-keyword | ||
wasAborted = false; | ||
get didPeerCloseConnection() { | ||
return this.peerWebSocket.closeFrame !== null || this.peerWebSocket.wasTerminated; | ||
} | ||
close(code, reason) { | ||
get didICloseConnection() { | ||
return this.ownCloseFrame !== null || this.wasAborted; | ||
} | ||
close(code = 1005, reason) { | ||
// tslint:disable-next-line:readonly-keyword no-object-mutation | ||
this.ownCloseFrame = { code, reason }; | ||
this.peerWebSocket.emit('close', code, reason); | ||
} | ||
abort(error) { | ||
// tslint:disable-next-line:no-object-mutation | ||
this.wasAborted = true; | ||
this.peerWebSocket.emit('error', error); | ||
@@ -88,3 +99,3 @@ } | ||
requireConnectionStillOpen() { | ||
if (this.wasConnectionClosed) { | ||
if (this.didPeerCloseConnection) { | ||
throw new Error('Connection was already closed'); | ||
@@ -91,0 +102,0 @@ } |
import { MockPeer } from './MockPeer'; | ||
import { MockServerAction } from './MockServerAction'; | ||
import { MockWebSocket } from './MockWebSocket'; | ||
export declare class MockServer extends MockPeer { | ||
get client(): MockWebSocket; | ||
runActions(...actions: readonly MockServerAction[]): Promise<void>; | ||
acceptConnection(): Promise<void>; | ||
use<T>(clientPromise: Promise<T>, serverImplementation?: () => Promise<void>): Promise<T>; | ||
} |
@@ -6,17 +6,21 @@ import { MockPeer } from './MockPeer'; | ||
} | ||
async runActions(...actions) { | ||
for (const action of actions) { | ||
if (this.wasConnectionClosed) { | ||
break; | ||
} | ||
await action.run(this); | ||
} | ||
async use(clientPromise, serverImplementation) { | ||
const [clientResult] = await Promise.all([ | ||
clientPromise, | ||
new Promise(async (resolve) => { | ||
this.peerWebSocket.emit('open'); | ||
// Give the client enough time to connect | ||
await new Promise(setImmediate); | ||
await serverImplementation?.(); | ||
// Allow more time for the client to process the latest actions from the server | ||
await new Promise(setImmediate); | ||
if (!this.didICloseConnection) { | ||
this.close(); | ||
} | ||
resolve(undefined); | ||
}), | ||
]); | ||
return clientResult; | ||
} | ||
async acceptConnection() { | ||
await new Promise((resolve) => { | ||
this.peerWebSocket.once('open', resolve); | ||
this.peerWebSocket.emit('open'); | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=MockServer.js.map |
{ | ||
"name": "@relaycorp/ws-mock", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "email": "no-reply@relaycorp.tech", |
@@ -15,3 +15,3 @@ # @relaycorp/ws-mock | ||
```javascript | ||
```typescript | ||
test('Challenge should be sent as soon as client connects', async () => { | ||
@@ -32,3 +32,3 @@ const client = new MockClient(wsServer); | ||
```javascript | ||
```typescript | ||
let mockServer: MockServer; | ||
@@ -47,14 +47,12 @@ beforeEach(() => { | ||
await Promise.all([ | ||
await mockServer.use( | ||
clientUnderTest.connectToServerAndInteractWithIt(), | ||
// Configure the mock server to accept the incoming connection and return a message straightaway | ||
mockServer.runActions( | ||
new AcceptConnectionAction(), | ||
new SendMessageAction(messageToEcho), | ||
), | ||
]); | ||
async () => { | ||
await mockServer.send(messageToEcho); | ||
// Check that the client sent the message back to the server: | ||
const clientResponse = await mockServer.receive(); | ||
expect(clientResponse).toEqual(messageToEcho); | ||
// Check that the client sent the message back to the server: | ||
const clientResponse = await mockServer.receive(); | ||
expect(clientResponse).toEqual(messageToEcho); | ||
}, | ||
); | ||
}); | ||
@@ -61,0 +59,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54678
51
897
72