-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fc013c
commit 4ec3130
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 not write when body is empty', () => { | ||
const proxyRequest = fakeProxyRequest(); | ||
|
||
jest.spyOn(proxyRequest, 'setHeader'); | ||
jest.spyOn(proxyRequest, 'write'); | ||
|
||
fixRequestBody(proxyRequest, { body: {} } as Request); | ||
|
||
expect(proxyRequest.setHeader).not.toHaveBeenCalled(); | ||
expect(proxyRequest.write).not.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); | ||
}); | ||
}); |