Skip to content

Commit 6fd75f7

Browse files
Add test coverage to fixRequestBody and responseInterceptor (chimurai#608)
* test(responseInterceptor): add unit test * test(fixRequestBody): add unit test
1 parent a6f8e0d commit 6fd75f7

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

test/unit/fix-request-body.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { ClientRequest } from 'http';
2+
import * as querystring from 'querystring';
3+
4+
import { fixRequestBody } from '../../src/handlers/fix-request-body';
5+
import type { Request } from '../../src/types';
6+
7+
const fakeProxyRequest = () => {
8+
const proxyRequest = new ClientRequest('http://some-host');
9+
proxyRequest.emit = jest.fn();
10+
11+
return proxyRequest;
12+
};
13+
14+
describe('fixRequestBody', () => {
15+
it('should not write when body is undefined', () => {
16+
const proxyRequest = fakeProxyRequest();
17+
18+
jest.spyOn(proxyRequest, 'setHeader');
19+
jest.spyOn(proxyRequest, 'write');
20+
21+
fixRequestBody(proxyRequest, { body: undefined } as Request);
22+
23+
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
24+
expect(proxyRequest.write).not.toHaveBeenCalled();
25+
});
26+
27+
it('should not write when body is empty', () => {
28+
const proxyRequest = fakeProxyRequest();
29+
30+
jest.spyOn(proxyRequest, 'setHeader');
31+
jest.spyOn(proxyRequest, 'write');
32+
33+
fixRequestBody(proxyRequest, { body: {} } as Request);
34+
35+
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
36+
expect(proxyRequest.write).not.toHaveBeenCalled();
37+
});
38+
39+
it('should write when body is not empty and Content-Type is application/json', () => {
40+
const proxyRequest = fakeProxyRequest();
41+
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
42+
43+
jest.spyOn(proxyRequest, 'setHeader');
44+
jest.spyOn(proxyRequest, 'write');
45+
46+
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
47+
48+
const expectedBody = JSON.stringify({ someField: 'some value' });
49+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
50+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
51+
});
52+
53+
it('should write when body is not empty and Content-Type is application/x-www-form-urlencoded', () => {
54+
const proxyRequest = fakeProxyRequest();
55+
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded');
56+
57+
jest.spyOn(proxyRequest, 'setHeader');
58+
jest.spyOn(proxyRequest, 'write');
59+
60+
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
61+
62+
const expectedBody = querystring.stringify({ someField: 'some value' });
63+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
64+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
65+
});
66+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { IncomingMessage, ServerResponse } from 'http';
2+
3+
import { responseInterceptor } from '../../src/handlers/response-interceptor';
4+
5+
const fakeProxyResponse = () => {
6+
const httpIncomingMessage = new IncomingMessage(null);
7+
httpIncomingMessage._read = () => ({});
8+
return httpIncomingMessage;
9+
};
10+
11+
const fakeResponse = () => {
12+
const httpIncomingMessage = fakeProxyResponse();
13+
14+
const response = new ServerResponse(httpIncomingMessage);
15+
response.setHeader = jest.fn();
16+
response.write = jest.fn();
17+
response.end = jest.fn();
18+
19+
return response;
20+
};
21+
22+
const waitInterceptorHandler = (ms = 1): Promise<void> =>
23+
new Promise((resolve) => setTimeout(resolve, ms));
24+
25+
describe('responseInterceptor', () => {
26+
it('should write body on end proxy event', async () => {
27+
const httpIncomingMessage = fakeProxyResponse();
28+
const response = fakeResponse();
29+
30+
responseInterceptor(async () => JSON.stringify({ someField: '' }))(
31+
httpIncomingMessage,
32+
null,
33+
response
34+
);
35+
36+
httpIncomingMessage.emit('end');
37+
await waitInterceptorHandler();
38+
39+
const expectedBody = JSON.stringify({ someField: '' });
40+
expect(response.setHeader).toHaveBeenCalledWith('content-length', expectedBody.length);
41+
expect(response.write).toHaveBeenCalledWith(Buffer.from(expectedBody));
42+
expect(response.end).toHaveBeenCalledWith();
43+
});
44+
45+
it('should end with error when receive a proxy error event', async () => {
46+
const httpIncomingMessage = fakeProxyResponse();
47+
const response = fakeResponse();
48+
49+
responseInterceptor(async () => JSON.stringify({ someField: '' }))(
50+
httpIncomingMessage,
51+
null,
52+
response
53+
);
54+
55+
httpIncomingMessage.emit('error', new Error('some error meessage'));
56+
57+
expect(response.setHeader).not.toHaveBeenCalled();
58+
expect(response.write).not.toHaveBeenCalled();
59+
expect(response.end).toHaveBeenCalledWith(
60+
'Error fetching proxied request: some error meessage'
61+
);
62+
});
63+
});

0 commit comments

Comments
 (0)