Skip to content

Commit 3da41f6

Browse files
committed
Refactoring
1 parent dd49b56 commit 3da41f6

7 files changed

+64
-74
lines changed

src/fixRequestBody.test.ts

+6-22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { fixRequestBody } from './fixRequestBody';
55

66
const fakeProxyRequest = () => {
77
const proxyRequest = new http.ClientRequest('http://some-host');
8-
proxyRequest.emit = jest.fn();
9-
8+
proxyRequest.emit = () => false; // Otherwise we get "Error: getaddrinfo ENOTFOUND some-host"
109
return proxyRequest;
1110
};
1211

@@ -16,12 +15,7 @@ test('should not write when body is undefined', () => {
1615
jest.spyOn(proxyRequest, 'setHeader');
1716
jest.spyOn(proxyRequest, 'write');
1817

19-
fixRequestBody(
20-
proxyRequest,
21-
{ body: undefined } as express.Request,
22-
{} as http.ServerResponse,
23-
{}
24-
);
18+
fixRequestBody(proxyRequest, { body: undefined } as express.Request);
2519

2620
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
2721
expect(proxyRequest.write).not.toHaveBeenCalled();
@@ -33,7 +27,7 @@ test('should not write when body is empty', () => {
3327
jest.spyOn(proxyRequest, 'setHeader');
3428
jest.spyOn(proxyRequest, 'write');
3529

36-
fixRequestBody(proxyRequest, { body: {} } as express.Request, {} as http.ServerResponse, {});
30+
fixRequestBody(proxyRequest, { body: {} } as express.Request);
3731

3832
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
3933
expect(proxyRequest.write).not.toHaveBeenCalled();
@@ -46,12 +40,7 @@ test('should write when body is not empty and Content-Type is application/json',
4640
jest.spyOn(proxyRequest, 'setHeader');
4741
jest.spyOn(proxyRequest, 'write');
4842

49-
fixRequestBody(
50-
proxyRequest,
51-
{ body: { someField: 'some value' } } as express.Request,
52-
{} as http.ServerResponse,
53-
{}
54-
);
43+
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as express.Request);
5544

5645
const expectedBody = JSON.stringify({ someField: 'some value' });
5746
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
@@ -65,14 +54,9 @@ test('should write when body is not empty and Content-Type is application/x-www-
6554
jest.spyOn(proxyRequest, 'setHeader');
6655
jest.spyOn(proxyRequest, 'write');
6756

68-
fixRequestBody(
69-
proxyRequest,
70-
{ body: { someField: 'some value' } } as express.Request,
71-
{} as http.ServerResponse,
72-
{}
73-
);
57+
fixRequestBody(proxyRequest, { body: { someField: 'some value' } } as express.Request);
7458

75-
const expectedBody = new URLSearchParams({ someField: 'some value' }).toString();
59+
const expectedBody = 'someField=some+value';
7660
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
7761
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
7862
});

src/fixRequestBody.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { ProxyReqCallback } from 'http-proxy';
1+
import http from 'http';
22

33
/**
44
* Fix proxied body if bodyParser is involved.
55
*
6-
* https://github.com/chimurai/http-proxy-middleware/blob/v2.0.1/src/handlers/fix-request-body.ts
7-
* https://github.com/chimurai/http-proxy-middleware/issues/320
8-
* https://github.com/chimurai/http-proxy-middleware/pull/492
6+
* @see https://github.com/chimurai/http-proxy-middleware/blob/v2.0.1/src/handlers/fix-request-body.ts
7+
* @see https://github.com/chimurai/http-proxy-middleware/issues/320
8+
* @see https://github.com/chimurai/http-proxy-middleware/pull/492
99
*/
10-
const fixRequestBody: ProxyReqCallback = (proxyReq, req) => {
10+
const fixRequestBody = (proxyReq: http.ClientRequest, req: http.IncomingMessage) => {
1111
const requestBody = (req as unknown as Request).body;
1212

1313
if (!requestBody || Object.keys(requestBody).length === 0) {

src/proxy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express from 'express';
22
import { createProxyServer } from 'http-proxy';
33

44
import { fixRequestBody } from './fixRequestBody';
5-
import { removeSecurityFromCookie } from './removeSecurityFromCookie';
5+
import { removeSecureFromSetCookie } from './removeSecureFromSetCookie';
66
import { ProxyParams } from './stubServer';
77

88
const send = (
@@ -31,7 +31,7 @@ const send = (
3131
});
3232

3333
proxy.on('proxyReq', fixRequestBody);
34-
proxy.on('proxyRes', removeSecurityFromCookie);
34+
proxy.on('proxyRes', removeSecureFromSetCookie);
3535
proxy.web(req, res, { target }, next);
3636
};
3737

src/removeSecureFromSetCookie.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { removeSecureFromSetCookie } from './removeSecureFromSetCookie';
2+
3+
test('should remove Secure attribute from Set-Cookie header', () => {
4+
const proxyRes = {
5+
headers: {
6+
'set-cookie': [
7+
'cookie1=ZYADVSQYTDZA1; Secure; SameSite',
8+
'cookie2=ZYADVSQYTDZA2; Secure',
9+
'cookie3=ZYADVSQYTDZA3'
10+
]
11+
}
12+
};
13+
14+
removeSecureFromSetCookie(proxyRes);
15+
16+
expect(proxyRes.headers['set-cookie']).toEqual([
17+
'cookie1=ZYADVSQYTDZA1; SameSite',
18+
'cookie2=ZYADVSQYTDZA2',
19+
'cookie3=ZYADVSQYTDZA3'
20+
]);
21+
});
22+
23+
test('do nothing if no Set-Cookie header', () => {
24+
const proxyRes = {
25+
headers: {}
26+
};
27+
28+
removeSecureFromSetCookie(proxyRes);
29+
30+
expect(proxyRes.headers).toEqual({});
31+
});

src/removeSecureFromSetCookie.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import http from 'http';
2+
3+
/**
4+
* http-proxy does not remove 'Secure' attribute from Set-Cookie header.
5+
*
6+
* @see https://github.com/http-party/node-http-proxy/issues/1165
7+
* @see https://github.com/http-party/node-http-proxy/pull/1166
8+
*/
9+
const removeSecureFromSetCookie = (proxyRes: { headers: http.IncomingHttpHeaders }) => {
10+
// ["Header names are lower-cased"](https://nodejs.org/dist/latest-v16.x/docs/api/http.html#messageheaders)
11+
12+
if (proxyRes.headers['set-cookie']) {
13+
const cookies = proxyRes.headers['set-cookie'].map(cookie => cookie.replace(/; secure/gi, ''));
14+
/* eslint-disable no-param-reassign */
15+
proxyRes.headers['set-cookie'] = cookies;
16+
/* eslint-enable no-param-reassign */
17+
}
18+
};
19+
20+
export { removeSecureFromSetCookie };

src/removeSecurityFromCookie.test.ts

-28
This file was deleted.

src/removeSecurityFromCookie.ts

-17
This file was deleted.

0 commit comments

Comments
 (0)