-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathRSocketWebSocketServer-test.js
More file actions
91 lines (80 loc) · 2.87 KB
/
RSocketWebSocketServer-test.js
File metadata and controls
91 lines (80 loc) · 2.87 KB
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
/** Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
jest.mock('net').useFakeTimers();
describe('RSocketWebSocketServer', () => {
// `import` and jest mocks don't seem to play well together
const ws = require('ws');
const {
FRAME_TYPES,
deserializeFrameWithLength,
serializeFrameWithLength,
} = require('rsocket-core');
const RSocketWebSocketServer = require('../RSocketWebSocketServer').default;
const EventEmitter = require('events');
beforeEach(() => {
jest.clearAllTimers();
});
describe('connectionStatus() and connect()', () => {
let emitter;
let connection;
let server;
let status;
beforeEach(() => {
emitter = new EventEmitter();
emitter.close = () => {};
emitter.error = () => {};
server = new RSocketWebSocketServer({port: 8080});
server.start().subscribe(_connection => (connection = _connection));
ws.servers[0].emit('connection', emitter);
connection.connectionStatus().subscribe({
onNext: _status => (status = _status),
onSubscribe: subscription =>
subscription.request(Number.MAX_SAFE_INTEGER),
});
});
it('initially returns CONNECTED', () => {
expect(status.kind).toBe('CONNECTED');
});
it('initially returns NOT_CONNECTED if socket is null', () => {
ws.servers[0].emit('connection', null);
connection.connectionStatus().subscribe({
onNext: _status => (status = _status),
onSubscribe: subscription =>
subscription.request(Number.MAX_SAFE_INTEGER),
});
expect(status.kind).toBe('NOT_CONNECTED');
});
it('returns ERROR if the socket errors', () => {
connection.receive().subscribe(() => {});
const error = new Error('wtf');
emitter.emit('error', error);
expect(status.kind).toBe('ERROR');
expect(status.error).toBe(error);
});
it('returns CLOSED if explicitly closed with no error', () => {
connection.receive().subscribe(() => {});
connection.close();
expect(status.kind).toBe('CLOSED');
});
it('returns ERROR if explicitly closed with an error', () => {
connection.receive().subscribe(() => {});
const error = new Error();
connection.close(error);
expect(status.kind).toBe('ERROR');
expect(status.error).toBe(error);
});
});
});