Skip to content

Commit

Permalink
Merge pull request #1219 from jembi/TB-385-bug-patch-request-unexpect…
Browse files Browse the repository at this point in the history
…ed-eof-read-on-the-socket-open-him-core

Add support for PATCH requests in router middleware
  • Loading branch information
bradsawadye authored Feb 8, 2024
2 parents 201308a + 84cbcbb commit 1053f51
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "openhim-core",
"description": "The OpenHIM core application that provides logging and routing of http requests",
"version": "8.4.1",
"version": "8.4.2",
"main": "./lib/server.js",
"bin": {
"openhim-core": "./bin/openhim-core.js"
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ function sendHttpRequest(ctx, route, options) {
routeReq.destroy(new Error(`Request took longer than ${timeout}ms`))
})

if (ctx.request.method === 'POST' || ctx.request.method === 'PUT') {
if (['POST', 'PUT', 'PATCH'].includes(ctx.request.method)) {
if (ctx.body != null) {
// TODO : Should probally add checks to see if the body is a buffer or string
routeReq.write(ctx.body)
Expand Down
61 changes: 61 additions & 0 deletions test/unit/routerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,37 @@ describe('HTTP Router', () => {
req.method.should.eql('POST')
})

it('should forward PATCH requests correctly', async () => {
const response = 'Hello Patch'
const patchSpy = sinon.spy(() => response)
server = await testUtils.createMockHttpServer(
patchSpy,
constants.HTTP_PORT,
200
)

const channel = {
name: 'PATCH channel',
urlPattern: '.+',
routes: [
{
host: 'localhost',
port: constants.HTTP_PORT,
primary: true
}
]
}
const ctx = createContext(channel, '/test', 'PATCH', 'some body')
await promisify(router.route)(ctx)
Buffer.isBuffer(ctx.response.body).should.be.true()
ctx.response.body.toString().should.eql(response)

patchSpy.callCount.should.be.eql(1)
const call = patchSpy.getCall(0)
const req = call.args[0]
req.method.should.eql('PATCH')
})

it('should handle empty put and post requests correctly', async () => {
const response = 'Hello Empty Post'
const postSpy = sinon.spy(() => response)
Expand Down Expand Up @@ -273,6 +304,36 @@ describe('HTTP Router', () => {
req.method.should.eql('POST')
})

it('should handle empty patch requests correctly', async () => {
const response = 'Hello Empty Patch'
const patchSpy = sinon.spy(() => response)
server = await testUtils.createMockHttpServer(
patchSpy,
constants.HTTP_PORT,
200
)
const channel = {
name: 'PATCH channel',
urlPattern: '.+',
routes: [
{
host: 'localhost',
port: constants.HTTP_PORT,
primary: true
}
]
}
const ctx = createContext(channel, '/test', 'PATCH')
await promisify(router.route)(ctx)
Buffer.isBuffer(ctx.response.body).should.be.true()
ctx.response.body.toString().should.eql(response)

patchSpy.callCount.should.be.eql(1)
const call = patchSpy.getCall(0)
const req = call.args[0]
req.method.should.eql('PATCH')
})

it('should send request params if these where received from the incoming request', async () => {
const requestSpy = sinon.spy(() => {})
server = await testUtils.createMockHttpServer(
Expand Down

0 comments on commit 1053f51

Please sign in to comment.