-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathfix-request-body.spec.ts
96 lines (69 loc) · 3.8 KB
/
fix-request-body.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
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
import { ClientRequest } from 'http';
import * as querystring from 'querystring';
import { fixRequestBody } from '../../src/handlers/fix-request-body';
import type { Request } from '../../src/types';
const fakeProxyRequest = () => {
const proxyRequest = new ClientRequest('http://some-host');
proxyRequest.emit = jest.fn();
return proxyRequest;
};
describe('fixRequestBody', () => {
it('should not write when body is undefined', () => {
const proxyRequest = fakeProxyRequest();
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');
fixRequestBody(proxyRequest, { body: undefined } as Request);
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
expect(proxyRequest.write).not.toHaveBeenCalled();
});
it('should write when body is an empty JSON object', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');
fixRequestBody(proxyRequest, { body: {} } as Request);
expect(proxyRequest.setHeader).toHaveBeenCalled();
expect(proxyRequest.write).toHaveBeenCalled();
});
it('should write when body is not empty and Content-Type is application/json', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
const expectedBody = JSON.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});
it('should write when body is not empty and Content-Type is application/x-www-form-urlencoded', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded');
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
const expectedBody = querystring.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});
it('should write when body is not empty and Content-Type includes application/x-www-form-urlencoded', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded; charset=UTF-8');
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
const expectedBody = querystring.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});
it('should parse json and call write() once with incorrect content-type application/x-www-form-urlencoded+application/json', () => {
const proxyRequest = fakeProxyRequest();
proxyRequest.setHeader('content-type', 'application/x-www-form-urlencoded+application/json');
jest.spyOn(proxyRequest, 'setHeader');
jest.spyOn(proxyRequest, 'write');
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as Request);
const expectedBody = JSON.stringify({ someField: 'some value' });
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
expect(proxyRequest.write).toHaveBeenCalledTimes(1);
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
});
});