-
Notifications
You must be signed in to change notification settings - Fork 121
/
StreamUtil.test.ts
241 lines (216 loc) · 9.06 KB
/
StreamUtil.test.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
import { PassThrough } from 'stream';
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import type { Logger } from '../../../src/logging/Logger';
import { getLoggerFor } from '../../../src/logging/LogUtil';
import { isHttpRequest } from '../../../src/server/HttpRequest';
import { guardedStreamFrom, pipeSafely, transformSafely, readableToString } from '../../../src/util/StreamUtil';
jest.mock('../../../src/logging/LogUtil', (): any => {
const logger: Logger = { warn: jest.fn(), log: jest.fn() } as any;
return { getLoggerFor: (): Logger => logger };
});
const logger: jest.Mocked<Logger> = getLoggerFor('StreamUtil') as any;
jest.mock('../../../src/server/HttpRequest', (): any => ({
isHttpRequest: jest.fn(),
}));
describe('StreamUtil', (): void => {
describe('#readableToString', (): void => {
it('concatenates all elements of a Readable.', async(): Promise<void> => {
const stream = streamifyArray([ 'a', 'b', 'c' ]);
await expect(readableToString(stream)).resolves.toEqual('abc');
});
});
describe('#pipeSafely', (): void => {
beforeEach(async(): Promise<void> => {
jest.clearAllMocks();
});
it('pipes data from one stream to the other.', async(): Promise<void> => {
const input = streamifyArray([ 'data' ]);
const output = new PassThrough();
const piped = pipeSafely(input, output);
await expect(readableToString(piped)).resolves.toEqual('data');
});
it('pipes errors from one stream to the other.', async(): Promise<void> => {
const input = new PassThrough();
input.read = (): any => {
input.emit('error', new Error('error'));
return null;
};
const output = new PassThrough();
const piped = pipeSafely(input, output);
await expect(readableToString(piped)).rejects.toThrow('error');
expect(logger.log).toHaveBeenCalledTimes(1);
expect(logger.log).toHaveBeenLastCalledWith('warn', 'Piped stream errored with error');
});
it('supports mapping errors to something else.', async(): Promise<void> => {
const input = streamifyArray([ 'data' ]);
input.read = (): any => {
input.emit('error', new Error('error'));
return null;
};
const output = new PassThrough();
const piped = pipeSafely(input, output, (): any => new Error('other error'));
await expect(readableToString(piped)).rejects.toThrow('other error');
});
it('logs specific safer errors as debug.', async(): Promise<void> => {
const input = streamifyArray([ 'data' ]);
input.read = (): any => {
input.emit('error', new Error('Cannot call write after a stream was destroyed'));
return null;
};
const output = new PassThrough();
const piped = pipeSafely(input, output);
await expect(readableToString(piped)).rejects.toThrow('Cannot call write after a stream was destroyed');
expect(logger.log).toHaveBeenCalledTimes(1);
expect(logger.log).toHaveBeenLastCalledWith(
'debug',
'Piped stream errored with Cannot call write after a stream was destroyed',
);
});
it('destroys the source stream in case the destinations becomes unpiped.', async(): Promise<void> => {
const input = new PassThrough();
const output = new PassThrough();
const piped = pipeSafely(input, output);
// Catch errors to prevent problems in test output
output.on('error', (): void => {
// Empty
});
piped.destroy(new Error('this causes an unpipe!'));
// Allow events to propagate
await new Promise(setImmediate);
expect(input.destroyed).toBe(true);
});
it('does not destroy the source stream if it is an HttpRequest.', async(): Promise<void> => {
(isHttpRequest as unknown as jest.Mock).mockReturnValueOnce(true);
const input = new PassThrough();
const output = new PassThrough();
const piped = pipeSafely(input, output);
// Catch errors to prevent problems in test output
output.on('error', (): void => {
// Empty
});
piped.destroy(new Error('error!'));
// Allow events to propagate
await new Promise(setImmediate);
expect(input.destroyed).toBe(false);
});
it('still sends errors downstream if the input is an HttpRequest.', async(): Promise<void> => {
(isHttpRequest as unknown as jest.Mock).mockReturnValueOnce(true);
const input = new PassThrough();
input.read = (): any => {
input.emit('error', new Error('error'));
return null;
};
const output = new PassThrough();
const piped = pipeSafely(input, output);
await expect(readableToString(piped)).rejects.toThrow('error');
});
it('can map errors if the input is an HttpRequest.', async(): Promise<void> => {
(isHttpRequest as unknown as jest.Mock).mockReturnValueOnce(true);
const input = streamifyArray([ 'data' ]);
input.read = (): any => {
input.emit('error', new Error('error'));
return null;
};
const output = new PassThrough();
const piped = pipeSafely(input, output, (): any => new Error('other error'));
await expect(readableToString(piped)).rejects.toThrow('other error');
});
});
describe('#transformSafely', (): void => {
it('can transform a stream without arguments.', async(): Promise<void> => {
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely(source);
transformed.setEncoding('utf8');
const result = await arrayifyStream(transformed);
expect(result).toEqual([ 'data' ]);
});
it('can transform a stream synchronously.', async(): Promise<void> => {
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely<string>(source, {
encoding: 'utf8',
transform(data: string): void {
this.push(`${data}1`);
this.push(`${data}2`);
},
flush(): void {
this.push(`data3`);
},
});
const result = await arrayifyStream(transformed);
expect(result).toEqual([ 'data1', 'data2', 'data3' ]);
});
it('can transform a stream asynchronously.', async(): Promise<void> => {
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely<string>(source, {
encoding: 'utf8',
async transform(data: string): Promise<void> {
await new Promise((resolve): any => setImmediate(resolve));
this.push(`${data}1`);
this.push(`${data}2`);
},
async flush(): Promise<void> {
await new Promise((resolve): any => setImmediate(resolve));
this.push(`data3`);
},
});
const result = await arrayifyStream(transformed);
expect(result).toEqual([ 'data1', 'data2', 'data3' ]);
});
it('catches source errors.', async(): Promise<void> => {
const error = new Error('stream error');
const source = new PassThrough();
const transformed = transformSafely<string>(source);
source.emit('error', error);
await expect(arrayifyStream(transformed)).rejects.toThrow(error);
});
it('catches synchronous errors on transform.', async(): Promise<void> => {
const error = new Error('stream error');
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely<string>(source, {
transform(): never {
throw error;
},
});
await expect(arrayifyStream(transformed)).rejects.toThrow(error);
});
it('catches synchronous errors on flush.', async(): Promise<void> => {
const error = new Error('stream error');
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely<string>(source, {
async flush(): Promise<never> {
await new Promise((resolve): any => setImmediate(resolve));
throw error;
},
});
await expect(arrayifyStream(transformed)).rejects.toThrow(error);
});
it('catches asynchronous errors on transform.', async(): Promise<void> => {
const error = new Error('stream error');
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely<string>(source, {
transform(): never {
throw error;
},
});
await expect(arrayifyStream(transformed)).rejects.toThrow(error);
});
it('catches asynchronous errors on flush.', async(): Promise<void> => {
const error = new Error('stream error');
const source = streamifyArray([ 'data' ]);
const transformed = transformSafely<string>(source, {
async flush(): Promise<never> {
await new Promise((resolve): any => setImmediate(resolve));
throw error;
},
});
await expect(arrayifyStream(transformed)).rejects.toThrow(error);
});
});
describe('#guardedStreamFrom', (): void => {
it('converts data to a guarded stream.', async(): Promise<void> => {
await expect(readableToString(guardedStreamFrom([ 'a', 'b' ]))).resolves.toBe('ab');
await expect(readableToString(guardedStreamFrom('ab'))).resolves.toBe('ab');
});
});
});