-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
web-sockets-controller.spec.ts
386 lines (357 loc) 路 13 KB
/
web-sockets-controller.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { ApplicationConfig } from '@nestjs/core/application-config';
import { expect } from 'chai';
import { fromEvent, lastValueFrom, Observable, of } from 'rxjs';
import * as sinon from 'sinon';
import { MetadataScanner } from '../../core/metadata-scanner';
import { AbstractWsAdapter } from '../adapters/ws-adapter';
import { PORT_METADATA } from '../constants';
import { WsContextCreator } from '../context/ws-context-creator';
import { WebSocketGateway } from '../decorators/socket-gateway.decorator';
import { InvalidSocketPortException } from '../errors/invalid-socket-port.exception';
import { GatewayMetadataExplorer } from '../gateway-metadata-explorer';
import { SocketServerProvider } from '../socket-server-provider';
import { WebSocketsController } from '../web-sockets-controller';
class NoopAdapter extends AbstractWsAdapter {
public create(port: number, options?: any) {}
public bindMessageHandlers(
client: any,
handlers,
transform: (data: any) => Observable<any>,
) {
handlers.forEach(({ message, callback }) => {
const source$ = fromEvent(client, message);
source$.subscribe(data => null);
});
}
}
describe('WebSocketsController', () => {
let instance: WebSocketsController;
let provider: SocketServerProvider,
config: ApplicationConfig,
mockProvider: sinon.SinonMock;
const messageHandlerCallback = () => Promise.resolve();
const port = 90,
namespace = '/';
@WebSocketGateway(port, { namespace })
class Test {}
beforeEach(() => {
config = new ApplicationConfig(new NoopAdapter());
provider = new SocketServerProvider(null, config);
mockProvider = sinon.mock(provider);
const contextCreator = sinon.createStubInstance(WsContextCreator);
contextCreator.create.returns(messageHandlerCallback);
instance = new WebSocketsController(
provider,
config,
contextCreator as any,
);
});
describe('connectGatewayToServer', () => {
let subscribeToServerEvents: sinon.SinonSpy;
@WebSocketGateway('test' as any)
class InvalidGateway {}
@WebSocketGateway()
class DefaultGateway {}
beforeEach(() => {
subscribeToServerEvents = sinon.spy();
(instance as any).subscribeToServerEvents = subscribeToServerEvents;
});
it('should throws "InvalidSocketPortException" when port is not a number', () => {
Reflect.defineMetadata(PORT_METADATA, 'test', InvalidGateway);
expect(() =>
instance.connectGatewayToServer(
new InvalidGateway(),
InvalidGateway,
'',
),
).throws(InvalidSocketPortException);
});
it('should call "subscribeToServerEvents" with default values when metadata is empty', () => {
const gateway = new DefaultGateway();
instance.connectGatewayToServer(gateway, DefaultGateway, '');
expect(subscribeToServerEvents.calledWith(gateway, {}, 0, '')).to.be.true;
});
it('should call "subscribeToServerEvents" when metadata is valid', () => {
const gateway = new Test();
instance.connectGatewayToServer(gateway, Test, '');
expect(
subscribeToServerEvents.calledWith(gateway, { namespace }, port, ''),
).to.be.true;
});
});
describe('subscribeToServerEvents', () => {
let explorer: GatewayMetadataExplorer,
mockExplorer: sinon.SinonMock,
gateway,
handlers,
server,
assignServerToProperties: sinon.SinonSpy,
subscribeEvents: sinon.SinonSpy;
const handlerCallback = () => {};
beforeEach(() => {
gateway = new Test();
explorer = new GatewayMetadataExplorer(new MetadataScanner());
mockExplorer = sinon.mock(explorer);
(instance as any).metadataExplorer = explorer;
handlers = [
{
message: 'message',
methodName: 'methodName',
callback: handlerCallback,
},
];
server = { server: 'test' };
mockExplorer.expects('explore').returns(handlers);
mockProvider.expects('scanForSocketServer').returns(server);
assignServerToProperties = sinon.spy();
subscribeEvents = sinon.spy();
(instance as any).assignServerToProperties = assignServerToProperties;
(instance as any).subscribeEvents = subscribeEvents;
});
it('should call "assignServerToProperties" with expected arguments', () => {
instance.subscribeToServerEvents(gateway, { namespace }, port, '');
expect(assignServerToProperties.calledWith(gateway, server.server)).to.be
.true;
});
it('should call "subscribeEvents" with expected arguments', () => {
instance.subscribeToServerEvents(gateway, { namespace }, port, '');
expect(subscribeEvents.firstCall.args[0]).to.be.equal(gateway);
expect(subscribeEvents.firstCall.args[2]).to.be.equal(server);
expect(subscribeEvents.firstCall.args[1]).to.be.eql([
{
message: 'message',
methodName: 'methodName',
callback: messageHandlerCallback,
},
]);
});
});
describe('subscribeEvents', () => {
const gateway = new Test();
let handlers;
let server,
subscribeConnectionEvent: sinon.SinonSpy,
subscribeDisconnectEvent: sinon.SinonSpy,
nextSpy: sinon.SinonSpy,
onSpy: sinon.SinonSpy,
subscribeInitEvent: sinon.SinonSpy,
getConnectionHandler: sinon.SinonSpy;
beforeEach(() => {
nextSpy = sinon.spy();
onSpy = sinon.spy();
subscribeInitEvent = sinon.spy();
getConnectionHandler = sinon.spy();
subscribeConnectionEvent = sinon.spy();
subscribeDisconnectEvent = sinon.spy();
handlers = ['test'];
server = {
server: {
on: onSpy,
},
init: {
next: nextSpy,
},
disconnect: {},
connection: {},
};
(instance as any).subscribeInitEvent = subscribeInitEvent;
(instance as any).getConnectionHandler = getConnectionHandler;
(instance as any).subscribeConnectionEvent = subscribeConnectionEvent;
(instance as any).subscribeDisconnectEvent = subscribeDisconnectEvent;
});
it('should call "subscribeConnectionEvent" with expected arguments', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(subscribeConnectionEvent.calledWith(gateway, server.connection)).to
.be.true;
});
it('should call "subscribeDisconnectEvent" with expected arguments', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(subscribeDisconnectEvent.calledWith(gateway, server.disconnect)).to
.be.true;
});
it('should call "subscribeInitEvent" with expected arguments', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(subscribeInitEvent.calledWith(gateway, server.init)).to.be.true;
});
it('should bind connection handler to server', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(onSpy.calledWith('connection', getConnectionHandler())).to.be.true;
});
it('should call "getConnectionHandler" with expected arguments', () => {
instance.subscribeEvents(gateway, handlers, server as any);
expect(
getConnectionHandler.calledWith(
instance,
gateway,
handlers,
server.disconnect,
server.connection,
),
).to.be.true;
});
});
describe('getConnectionHandler', () => {
const gateway = new Test();
let handlers, fn;
let connection,
client,
nextSpy: sinon.SinonSpy,
onSpy: sinon.SinonSpy,
subscribeMessages: sinon.SinonSpy,
subscribeDisconnectEvent: sinon.SinonSpy,
subscribeConnectionEvent: sinon.SinonSpy;
beforeEach(() => {
nextSpy = sinon.spy();
onSpy = sinon.spy();
subscribeMessages = sinon.spy();
subscribeDisconnectEvent = sinon.spy();
subscribeConnectionEvent = sinon.spy();
handlers = ['test'];
connection = {
next: nextSpy,
};
client = {
on: onSpy,
};
(instance as any).subscribeDisconnectEvent = subscribeDisconnectEvent;
(instance as any).subscribeConnectionEvent = subscribeConnectionEvent;
(instance as any).subscribeMessages = subscribeMessages;
fn = instance.getConnectionHandler(
instance,
gateway,
handlers,
null,
connection,
);
fn(client);
});
it('should return function', () => {
expect(
instance.getConnectionHandler(null, null, null, null, null),
).to.be.a('function');
});
it('should call "next" method of connection object with expected argument', () => {
expect(nextSpy.calledWith([client])).to.be.true;
});
it('should call "subscribeMessages" with expected arguments', () => {
expect(subscribeMessages.calledWith(handlers, client, gateway)).to.be
.true;
});
it('should call "on" method of client object with expected arguments', () => {
expect(onSpy.called).to.be.true;
});
});
describe('subscribeInitEvent', () => {
const gateway = new Test();
let event, subscribe: sinon.SinonSpy;
beforeEach(() => {
subscribe = sinon.spy();
event = { subscribe, pipe: sinon.stub().returnsThis() };
});
it('should not call subscribe method when "afterInit" method not exists', () => {
instance.subscribeInitEvent(gateway, event);
expect(subscribe.called).to.be.false;
});
it('should call subscribe method of event object with expected arguments when "afterInit" exists', () => {
(gateway as any).afterInit = () => {};
instance.subscribeInitEvent(gateway, event);
expect(subscribe.called).to.be.true;
});
});
describe('subscribeConnectionEvent', () => {
const gateway = new Test();
let event, subscribe: sinon.SinonSpy;
beforeEach(() => {
subscribe = sinon.spy();
event = { subscribe, pipe: sinon.stub().returnsThis() };
});
it('should not call subscribe method when "handleConnection" method not exists', () => {
instance.subscribeConnectionEvent(gateway, event);
expect(subscribe.called).to.be.false;
});
it('should call subscribe method of event object with expected arguments when "handleConnection" exists', () => {
(gateway as any).handleConnection = () => {};
instance.subscribeConnectionEvent(gateway, event);
expect(subscribe.called).to.be.true;
});
});
describe('subscribeDisconnectEvent', () => {
const gateway = new Test();
let event, subscribe: sinon.SinonSpy;
beforeEach(() => {
subscribe = sinon.spy();
event = { subscribe, pipe: sinon.stub().returnsThis() };
});
it('should not call subscribe method when "handleDisconnect" method not exists', () => {
instance.subscribeDisconnectEvent(gateway, event);
expect(subscribe.called).to.be.false;
});
it('should call subscribe method of event object with expected arguments when "handleDisconnect" exists', () => {
(gateway as any).handleDisconnect = () => {};
instance.subscribeDisconnectEvent(gateway, event);
expect(subscribe.called).to.be.true;
});
});
describe('subscribeMessages', () => {
const gateway = new Test();
let client, handlers, onSpy: sinon.SinonSpy;
beforeEach(() => {
onSpy = sinon.spy();
client = { on: onSpy, off: onSpy };
handlers = [
{ message: 'test', callback: { bind: () => 'testCallback' } },
{ message: 'test2', callback: { bind: () => 'testCallback2' } },
];
});
it('should bind each handler to client', () => {
instance.subscribeMessages(handlers, client, gateway);
expect(onSpy.calledTwice).to.be.true;
});
});
describe('pickResult', () => {
describe('when deferredResult contains value which', () => {
describe('is a Promise', () => {
it('should return Promise<Observable>', async () => {
const value = 100;
expect(
await lastValueFrom(
await instance.pickResult(
Promise.resolve(Promise.resolve(value)),
),
),
).to.be.eq(value);
});
});
describe('is an Observable', () => {
it('should return Promise<Observable>', async () => {
const value = 100;
expect(
await lastValueFrom(
await instance.pickResult(Promise.resolve(of(value))),
),
).to.be.eq(value);
});
});
describe('is an object that has the method `subscribe`', () => {
it('should return Promise<Observable>', async () => {
const value = { subscribe() {} };
expect(
await lastValueFrom(
await instance.pickResult(Promise.resolve(value)),
),
).to.equal(value);
});
});
describe('is an ordinary value', () => {
it('should return Promise<Observable>', async () => {
const value = 100;
expect(
await lastValueFrom(
await instance.pickResult(Promise.resolve(value)),
),
).to.be.eq(value);
});
});
});
});
});