-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathtest-utils.ts
415 lines (353 loc) · 13.2 KB
/
test-utils.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import * as sourceMapSupport from 'source-map-support'
sourceMapSupport.install({ handleUncaughtExceptions: false });
import * as _ from 'lodash';
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import * as https from 'https';
import * as http2 from 'http2';
import * as http2Wrapper from 'http2-wrapper';
import * as streams from 'stream';
import * as URL from 'url';
import * as CrossFetch from "cross-fetch";
import {
FormData as FormDataPolyfill,
File as FilePolyfill
} from "formdata-node";
import { RequestPromise } from 'request-promise-native';
import chai = require("chai");
import chaiAsPromised = require("chai-as-promised");
import chaiFetch = require("chai-fetch");
import * as dns2 from 'dns2'; // Imported here just for types
import { Mockttp } from "..";
export { getDeferred, Deferred } from '../src/util/promise';
import { makeDestroyable, DestroyableServer } from "destroyable-server";
import { isNode, isWeb, delay } from '../src/util/util';
export { isNode, isWeb, delay, makeDestroyable, DestroyableServer };
if (isNode) {
// Run a target websocket server in the background. In browsers, this is
// launched by from the Karma script. Eventually this should be replaced
// by a Mockttp-spawned WS server, once we have one.
require('./fixtures/websocket-test-server');
}
chai.use(chaiAsPromised);
chai.use(chaiFetch);
export const AssertionError = chai.AssertionError;
function getGlobalFetch() {
return {
fetch: globalThis.fetch.bind(globalThis),
Headers: globalThis.Headers,
Request: globalThis.Request,
Response: globalThis.Response
};
}
let fetchImplementation = isNode ? CrossFetch : getGlobalFetch();
export const fetch = fetchImplementation.fetch;
// All a bit convoluted, so we don't shadow the global vars,
// and we can still use those to define these in the browser
const headersImplementation = fetchImplementation.Headers;
const requestImplementation = fetchImplementation.Request;
const responseImplementation = fetchImplementation.Response;
export { headersImplementation as Headers };
export { requestImplementation as Request };
export { responseImplementation as Response };
export const FormData = globalThis.FormData ?? FormDataPolyfill;
export const File = globalThis.File ?? FilePolyfill;
// Quick helper to convert Fetch response headers back into an object. Very dumb,
// doesn't deal with multiple header values or anything correctly, but ok for tests.
export function headersToObject(fetchHeaders: Headers) {
const headers: _.Dictionary<string> = {};
fetchHeaders.forEach((value, key) => {
headers[key] = value;
});
return headers;
}
export const URLSearchParams: typeof window.URLSearchParams = (isNode || !window.URLSearchParams) ?
require('url').URLSearchParams : window.URLSearchParams;
export const expect = chai.expect;
export function browserOnly(body: Function) {
if (!isNode) body();
}
export function nodeOnly(body: Function) {
if (isNode) body();
}
// Wrap a test promise that might fail due to irrelevant remote network issues, and it'll skip the test
// if there's a timeout or 502 response (but still throw any other errors). This allows us to write tests
// that will fail if a remote server explicitly rejects something, but make them resilient to the remote
// server simply being entirely unavailable.
export async function ignoreNetworkError<T extends RequestPromise | Promise<Response>>(request: T, options: {
context: Mocha.Context,
timeout?: number
}): Promise<T> {
const TimeoutError = new Error('timeout');
const result = await Promise.race([
request.catch(e => e),
delay(options.timeout ?? 1000).then(() => { throw TimeoutError })
]).catch(error => {
console.log(error);
if (error === TimeoutError) {
console.warn(`Skipping test due to network error: ${error.message || error}`);
if ('abort' in request) request.abort();
throw options.context.skip();
} else {
throw error;
}
});
if ((result as any).status === 502) {
console.warn('Skipping test due to remote 502 response');
throw options.context.skip();
}
return result;
}
const TOO_LONG_HEADER_SIZE = 1024 * (isNode ? 16 : 160) + 1;
export const TOO_LONG_HEADER_VALUE = _.range(TOO_LONG_HEADER_SIZE).map(() => "X").join("");
export async function openRawSocket(server: Mockttp) {
const socket = new net.Socket();
return new Promise<net.Socket>((resolve, reject) => {
socket.connect({
port: server.port,
host: '127.0.0.1'
});
socket.on('connect', () => resolve(socket));
socket.on('error', reject);
});
}
export async function sendRawRequest(server: Mockttp, requestContent: string): Promise<string> {
const client = new net.Socket();
await new Promise<void>((resolve) => client.connect(server.port, '127.0.0.1', resolve));
const dataPromise = new Promise<string>((resolve) => {
client.on('data', function(data) {
resolve(data.toString());
client.destroy();
});
});
client.write(requestContent);
client.end();
return dataPromise;
}
export async function openRawTlsSocket(
target: Mockttp | net.Socket,
options: tls.ConnectionOptions = {}
): Promise<tls.TLSSocket> {
return await new Promise<tls.TLSSocket>((resolve, reject) => {
const socket: tls.TLSSocket = tls.connect({
host: 'localhost',
...(target instanceof net.Socket
? { socket: target }
: { port: target.port }
),
...options
});
socket.once('secureConnect', () => resolve(socket));
socket.once('error', reject);
});
}
// Write a message to a socket that will trigger a respnse, but kill the socket
// before the response is received, so a real response triggers a reset.
export async function writeAndReset(socket: net.Socket, content: string) {
socket.write(content);
setTimeout(() => socket.destroy(), 0);
}
export function watchForEvent(event: string, ...servers: Mockttp[]) {
let eventResult: any;
beforeEach(async () => {
eventResult = undefined;
await Promise.all(servers.map((server) =>
server.on(event as any, (result: any) => {
eventResult = result || true;
})
));
});
return async () => {
await delay(100);
expect(eventResult).to.equal(undefined, `Unexpected ${event} event`);
}
}
// An extremely simple & dumb DNS server for quick testing:
export async function startDnsServer(callback: (question: dns2.DnsQuestion) => string | undefined) {
// We import the implementation async, because it fails in the browser
const dns2 = await import('dns2');
const server = makeDestroyable(dns2.createServer(async (request, sendResponse) => {
const response = dns2.Packet.createResponseFromRequest(request);
// Multiple questions are allowed in theory, but apparently nobody
// supports it, so we don't either.
const [question] = request.questions;
const answer = callback(question);
if (answer) response.answers.push({
name: question.name,
type: dns2.Packet.TYPE.A,
class: dns2.Packet.CLASS.IN,
ttl: 0,
address: answer
});
sendResponse(response);
}));
return new Promise<DestroyableServer<net.Server>>((resolve, reject) => {
server.listen(5333, '127.0.0.1');
server.on('listening', () => resolve(server));
server.on('error', reject);
});
}
export const H2_TLS_ON_TLS_SUPPORTED = ">=12.17";
export const HTTP_ABORTSIGNAL_SUPPORTED = ">=14.17";
export const OLD_TLS_SUPPORTED = "<17"; // In 17+ TLS < v1.2 is only available with legacy OpenSSL flag
export const SOCKET_RESET_SUPPORTED = "^16.17 || >=18.3";
export const BROKEN_H2_TUNNELLING = "^18.8"; // Some H1-over-H2 tests fail in Node 18 (but not 19)
type Http2ResponseHeaders = http2.IncomingHttpHeaders & http2.IncomingHttpStatusHeader;
type Http2TestRequestResult = {
alpnProtocol: string | undefined,
headers: http2.IncomingHttpHeaders,
body: Buffer
};
export function getHttp2Response(req: http2.ClientHttp2Stream) {
return new Promise<Http2ResponseHeaders>((resolve, reject) => {
req.on('response', resolve);
req.on('error', reject);
});
}
export function getHttp2Body(req: http2.ClientHttp2Stream) {
return new Promise<Buffer>((resolve, reject) => {
if (req.closed) {
resolve(Buffer.from([]));
return;
}
const body: Buffer[] = [];
req.on('data', (d: Buffer | string) => {
body.push(Buffer.from(d));
});
req.on('end', () => req.close());
req.on('close', () => resolve(Buffer.concat(body)));
req.on('error', reject);
});
}
export async function http2Request(
url: string,
headers: {},
requestBody = '',
createConnection?: (() => streams.Duplex) | undefined
) {
const client = http2.connect(url, { createConnection });
return new Promise<Http2TestRequestResult>(async (resolve, reject) => {
try {
const req = client.request(headers, {
endStream: !requestBody
});
req.on('error', reject);
if (requestBody) req.end(requestBody);
const [
responseHeaders,
responseBody
] = await Promise.all([
getHttp2Response(req),
getHttp2Body(req)
]);
const alpnProtocol = client.alpnProtocol;
resolve({
alpnProtocol,
headers: responseHeaders,
body: responseBody
});
} catch (e) {
reject(e);
}
}).finally(() => cleanup(client));
}
export function http2DirectRequest(
server: Mockttp,
path: string,
headers: {} = {}
) {
return http2Request(server.url, {
':path': path,
...headers
});
}
export async function http2ProxyRequest(
proxyServer: Mockttp,
url: string,
options: {
headers?: {},
requestBody?: string,
http1Within?: boolean
} = {}
) {
const { headers, requestBody, http1Within } = options;
const parsedUrl = URL.parse(url);
const isTLS = parsedUrl.protocol === 'https:';
const targetHost = parsedUrl.hostname!;
const targetPort = parsedUrl.port! ?? (isTLS ? 443 : 80);
const proxyClient = http2.connect(proxyServer.url);
return await new Promise<Http2TestRequestResult>(async (resolve, reject) => {
try {
const proxyReq = proxyClient.request({
':method': 'CONNECT',
':authority': `${targetHost}:${targetPort}`
});
proxyReq.on('error', reject);
const proxyResponse = await getHttp2Response(proxyReq);
expect(proxyResponse[':status']).to.equal(200);
const createConnection = () => isTLS
? tls.connect({
host: targetHost,
servername: targetHost,
socket: proxyReq as any,
ALPNProtocols: http1Within ? ['http/1.1'] : ['h2']
})
: proxyReq as unknown as net.Socket
if (!http1Within) {
resolve(http2Request(
url,
{
':path': parsedUrl.path,
...headers
},
requestBody,
createConnection
));
} else {
const req = (isTLS ? https : http).request(url, {
headers: { host: `${targetHost}:${targetPort}` },
createConnection
});
req.end(requestBody);
req.on('response', resolve);
req.on('error', reject);
}
} catch (e) {
reject(e);
}
}).finally(() => cleanup(proxyClient));
}
export async function cleanup(
...streams: (streams.Duplex | http2.Http2Session | http2.Http2Stream)[]
) {
return new Promise<void>((resolve, reject) => {
if (streams.length === 0) resolve();
else {
const nextStream = streams[0];
nextStream.on('error', reject);
if ('resume' in nextStream) {
// Drain the stream, to ensure it closes OK
nextStream.resume();
}
if ('close' in nextStream) {
nextStream.close();
nextStream.on('close', () => {
cleanup(...streams.slice(1))
.then(resolve).catch(reject);
});
} else {
nextStream.destroy();
cleanup(...streams.slice(1))
.then(resolve).catch(reject);
}
}
});
}
beforeEach(() => {
if (isNode) {
// Http2-wrapper has a hostname -> H1/H2 cache, which can cause problems
// when our tests reuse ports with servers of different protocols.
(http2Wrapper.auto as any).protocolCache.clear();
}
});