-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathRSocketWebSocketClient-test.js
More file actions
317 lines (281 loc) · 10.5 KB
/
RSocketWebSocketClient-test.js
File metadata and controls
317 lines (281 loc) · 10.5 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
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
/** 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';
/* eslint-env browser */
jest.useFakeTimers();
import {FRAME_TYPES, deserializeFrame, serializeFrame} from 'rsocket-core';
import {genMockPublisher} from 'MockFlowableSubscription';
import {genMockSubscriber} from 'MockFlowableSubscriber';
import RSocketWebSocketClient from '../RSocketWebSocketClient';
describe('RSocketWebSocketClient', () => {
beforeEach(() => {
jest.clearAllTimers();
});
describe('connectionStatus() and connect()', () => {
let client;
let status;
beforeEach(() => {
client = new RSocketWebSocketClient({url: 'wss://...'});
client.connectionStatus().subscribe({
onNext: _status => (status = _status),
onSubscribe: subscription =>
subscription.request(Number.MAX_SAFE_INTEGER),
});
});
it('initially returns NOT_CONNECTED', () => {
expect(status.kind).toBe('NOT_CONNECTED');
});
it('returns CONNECTING while connecting', () => {
client.connect();
expect(status.kind).toBe('CONNECTING');
});
it('returns CONNECTED once connected', () => {
client.connect();
WebSocket.socket.mock.open();
expect(status.kind).toBe('CONNECTED');
});
it('returns ERROR if the socket errors', () => {
client.connect();
WebSocket.socket.mock.error(new Error('oopsie'));
expect(status.kind).toBe('ERROR');
expect(status.error.message).toBe('oopsie');
});
it('returns CLOSED if explicitly closed', () => {
client.connect();
client.close();
expect(status.kind).toBe('CLOSED');
});
});
describe('post-connect() APIs', () => {
let client;
let socket;
const frame = {
data: null,
flags: 0,
lastReceivedPosition: 0,
streamId: 0,
type: FRAME_TYPES.KEEPALIVE,
};
beforeEach(() => {
client = new RSocketWebSocketClient({url: 'wss://...'});
client.connect();
jest.runAllTimers();
socket = WebSocket.socket;
socket.mock.open();
jest.runAllTimers();
});
describe('close()', () => {
describe('given an error', () => {
it('closes the socket', () => {
client.close(new Error());
expect(socket.close.mock.calls.length).toBe(1);
});
it('sets the status to ERROR with the given error', () => {
let status;
client.connectionStatus().subscribe({
onNext: _status => (status = _status),
onSubscribe: subscription =>
subscription.request(Number.MAX_SAFE_INTEGER),
});
const error = new Error();
client.close(error);
expect(status.kind).toBe('ERROR');
expect(status.error).toBe(error);
});
it('calls receive.onError with the given error', () => {
const onError = jest.fn();
const onSubscribe = subscription =>
subscription.request(Number.MAX_SAFE_INTEGER);
client.receive().subscribe({onError, onSubscribe});
const error = new Error();
client.close(error);
expect(onError.mock.calls.length).toBe(1);
expect(onError.mock.calls[0][0]).toBe(error);
});
});
describe('not given an error', () => {
it('closes the socket', () => {
client.close();
expect(socket.close.mock.calls.length).toBe(1);
});
it('sets the status to CLOSED', () => {
let status;
client.connectionStatus().subscribe({
onNext: _status => (status = _status),
onSubscribe: subscription =>
subscription.request(Number.MAX_SAFE_INTEGER),
});
client.close();
expect(status.kind).toBe('CLOSED');
});
it('calls receive.onComplete', () => {
const onComplete = jest.fn();
const onSubscribe = subscription =>
subscription.request(Number.MAX_SAFE_INTEGER);
client.receive().subscribe({onComplete, onSubscribe});
client.close();
expect(onComplete.mock.calls.length).toBe(1);
});
});
});
describe('sendOne()', () => {
it('sends a frame', () => {
client.sendOne(frame);
expect(socket.send.mock.calls.length).toBe(1);
const buffer = socket.send.mock.calls[0][0];
expect(deserializeFrame(buffer)).toEqual({
...frame,
length: buffer.length,
});
});
it('calls receive.onError if the frame cannot be sent', () => {
const onError = jest.fn();
const onSubscribe = subscription =>
subscription.request(Number.MAX_SAFE_INTEGER);
client.receive().subscribe({onError, onSubscribe});
socket.send = () => {
throw new Error('wtf');
};
client.sendOne(frame);
expect(onError.mock.calls.length).toBe(1);
});
});
describe('send()', () => {
it('sends frames', () => {
const frame2 = {...frame, flags: 1};
const publisher = genMockPublisher();
client.send(publisher);
publisher.onNext(frame);
publisher.onNext(frame2);
expect(socket.send.mock.calls.length).toBe(2);
const buffer = socket.send.mock.calls[0][0];
expect(deserializeFrame(buffer)).toEqual({
...frame,
length: buffer.length,
});
const buffer2 = socket.send.mock.calls[1][0];
expect(deserializeFrame(buffer2)).toEqual({
...frame2,
length: buffer2.length,
});
});
it('calls receive.onError if frames cannot be sent', () => {
const onError = jest.fn();
const onSubscribe = subscription =>
subscription.request(Number.MAX_SAFE_INTEGER);
client.receive().subscribe({onError, onSubscribe});
socket.send = () => {
throw new Error('wtf');
};
const publisher = genMockPublisher();
client.send(publisher);
publisher.onNext(frame);
expect(onError.mock.calls.length).toBe(1);
});
it('unsubscribes when closed', () => {
const publisher = genMockPublisher();
client.send(publisher);
client.close();
expect(publisher.cancel).toBeCalled();
});
});
describe('receive()', () => {
it('calls onNext with deserialized frames', () => {
const subscriber = genMockSubscriber({
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
});
client.receive().subscribe(subscriber);
expect(subscriber.onNext.mock.calls.length).toBe(0);
socket.mock.message(serializeFrame(frame));
expect(subscriber.onNext.mock.calls.length).toBe(1);
const nextFrame = subscriber.onNext.mock.calls[0][0];
expect(nextFrame).toEqual({...frame, length: nextFrame.length});
expect(subscriber.onComplete.mock.calls.length).toBe(0);
expect(subscriber.onError.mock.calls.length).toBe(0);
});
it('calls onError when partial frames are received', () => {
const subscriber = genMockSubscriber({
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
});
client.receive().subscribe(subscriber);
const buffer = serializeFrame(frame);
socket.mock.message(buffer.slice(0, 2));
expect(subscriber.onComplete.mock.calls.length).toBe(0);
expect(subscriber.onNext.mock.calls.length).toBe(0);
expect(subscriber.onError.mock.calls.length).toBe(1);
});
it('calls onComplete when intentionally close()-ed', () => {
const subscriber = genMockSubscriber({
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
});
client.receive().subscribe(subscriber);
client.close();
expect(subscriber.onComplete.mock.calls.length).toBe(1);
expect(subscriber.onError.mock.calls.length).toBe(0);
expect(subscriber.onNext.mock.calls.length).toBe(0);
});
it('calls onError when the socket is closed by the peer', () => {
const subscriber = genMockSubscriber({
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
});
client.receive().subscribe(subscriber);
socket.mock.close();
expect(subscriber.onComplete.mock.calls.length).toBe(0);
expect(subscriber.onError.mock.calls.length).toBe(1);
const error = subscriber.onError.mock.calls[0][0];
expect(error.message).toBe(
'RSocketWebSocketClient: Socket closed unexpectedly.',
);
expect(subscriber.onNext.mock.calls.length).toBe(0);
});
it('calls onError when invalid frames are received', () => {
const subscriber = genMockSubscriber({
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
});
client.receive().subscribe(subscriber);
// Emit a frame of length one, which is shorter than the smallest
// possible frame (3 bytes of length, 1 byte of payload).
const buffer = new Buffer([0x00, 0x00, 0x01, 0x00]);
socket.mock.message(buffer);
expect(subscriber.onComplete.mock.calls.length).toBe(0);
expect(subscriber.onError.mock.calls.length).toBe(1);
expect(subscriber.onNext.mock.calls.length).toBe(0);
});
it('calls onError when a socket error occurs', () => {
const subscriber = genMockSubscriber({
onSubscribe(subscription) {
subscription.request(Number.MAX_SAFE_INTEGER);
},
});
client.receive().subscribe(subscriber);
socket.mock.error(new Error('oops'));
expect(subscriber.onComplete.mock.calls.length).toBe(0);
expect(subscriber.onError.mock.calls.length).toBe(1);
expect(subscriber.onError.mock.calls[0][0].message).toBe('oops');
expect(subscriber.onNext.mock.calls.length).toBe(0);
});
});
});
});