-
Notifications
You must be signed in to change notification settings - Fork 823
/
response-interceptor.spec.ts
56 lines (42 loc) · 1.85 KB
/
response-interceptor.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
import { IncomingMessage, ServerResponse } from 'http';
import { Socket } from 'net';
import { responseInterceptor } from '../../src/handlers/response-interceptor';
const fakeProxyResponse = () => {
const httpIncomingMessage = new IncomingMessage(new Socket());
httpIncomingMessage._read = () => ({});
return httpIncomingMessage;
};
const fakeResponse = () => {
const httpIncomingMessage = fakeProxyResponse();
const response = new ServerResponse(httpIncomingMessage);
response.setHeader = jest.fn();
response.write = jest.fn();
response.end = jest.fn();
return response;
};
const waitInterceptorHandler = (ms = 1): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
describe('responseInterceptor', () => {
it('should write body on end proxy event', async () => {
const proxyRes = fakeProxyResponse();
const req = fakeProxyResponse();
const res = fakeResponse();
responseInterceptor(async () => JSON.stringify({ someField: '' }))(proxyRes, req, res);
proxyRes.emit('end');
await waitInterceptorHandler();
const expectedBody = JSON.stringify({ someField: '' });
expect(res.setHeader).toHaveBeenCalledWith('content-length', expectedBody.length);
expect(res.write).toHaveBeenCalledWith(Buffer.from(expectedBody));
expect(res.end).toHaveBeenCalledWith();
});
it('should end with error when receive a proxy error event', async () => {
const proxyRes = fakeProxyResponse();
const req = fakeProxyResponse();
const res = fakeResponse();
responseInterceptor(async () => JSON.stringify({ someField: '' }))(proxyRes, req, res);
proxyRes.emit('error', new Error('some error message'));
expect(res.setHeader).not.toHaveBeenCalled();
expect(res.write).not.toHaveBeenCalled();
expect(res.end).toHaveBeenCalledWith('Error fetching proxied request: some error message');
});
});