Skip to content

Commit 014a198

Browse files
authored
test(fix-request-body): add tests (#1289)
1 parent 087db31 commit 014a198

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ describe('fixRequestBody', () => {
3838
expect(proxyRequest.write).not.toHaveBeenCalled();
3939
});
4040

41+
it('should not write when Content-Type is missing', () => {
42+
const proxyRequest = fakeProxyRequest();
43+
44+
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));
45+
46+
expect(proxyRequest.setHeader).not.toHaveBeenCalled();
47+
expect(proxyRequest.write).not.toHaveBeenCalled();
48+
});
49+
50+
it('should not write when Content-Type is unsupported', () => {
51+
const proxyRequest = fakeProxyRequest();
52+
proxyRequest.setHeader('content-type', 'application/octet-stream');
53+
54+
fixRequestBody(proxyRequest, createRequestWithBody({ someField: 'some value' }));
55+
56+
expect(proxyRequest.setHeader).not.toHaveBeenCalledWith('Content-Length', expect.anything());
57+
expect(proxyRequest.write).not.toHaveBeenCalled();
58+
});
59+
4160
it('should write when body is an empty JSON object', () => {
4261
const proxyRequest = fakeProxyRequest();
4362
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
@@ -274,6 +293,32 @@ describe('fixRequestBody', () => {
274293
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
275294
});
276295

296+
it('should re-encode body when the source was Brotli encoded', () => {
297+
const proxyRequest = fakeProxyRequest();
298+
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
299+
proxyRequest.setHeader('content-encoding', 'br');
300+
301+
const data = { someField: 'some value' };
302+
fixRequestBody(proxyRequest, createRequestWithBody(data));
303+
304+
const expectedBody = zlib.brotliCompressSync(JSON.stringify(data));
305+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
306+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
307+
});
308+
309+
it('should re-encode body when the source was deflate encoded', () => {
310+
const proxyRequest = fakeProxyRequest();
311+
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
312+
proxyRequest.setHeader('content-encoding', 'deflate');
313+
314+
const data = { someField: 'some value' };
315+
fixRequestBody(proxyRequest, createRequestWithBody(data));
316+
317+
const expectedBody = zlib.deflateSync(JSON.stringify(data));
318+
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
319+
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
320+
});
321+
277322
it('should re-encode body when the source was zstd encoded', () => {
278323
const proxyRequest = fakeProxyRequest();
279324
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
@@ -286,4 +331,24 @@ describe('fixRequestBody', () => {
286331
expect(proxyRequest.setHeader).toHaveBeenCalledWith('Content-Length', expectedBody.length);
287332
expect(proxyRequest.write).toHaveBeenCalledWith(expectedBody);
288333
});
334+
335+
it('should destroy the proxy request with an Error when body serialization throws a string', () => {
336+
const proxyRequest = fakeProxyRequest();
337+
proxyRequest.setHeader('content-type', 'application/json; charset=utf-8');
338+
339+
const requestBody = {
340+
toJSON() {
341+
throw 'serialization failed';
342+
},
343+
};
344+
345+
fixRequestBody(proxyRequest, createRequestWithBody(requestBody));
346+
347+
expect(proxyRequest.write).not.toHaveBeenCalled();
348+
expect(proxyRequest.destroy).toHaveBeenCalledTimes(1);
349+
expect(proxyRequest.destroy).toHaveBeenCalledWith(expect.any(Error));
350+
expect(proxyRequest.destroy).toHaveBeenCalledWith(
351+
expect.objectContaining({ message: 'serialization failed' }),
352+
);
353+
});
289354
});

0 commit comments

Comments
 (0)