Skip to content

Commit 1053f51

Browse files
authored
Merge pull request #1219 from jembi/TB-385-bug-patch-request-unexpected-eof-read-on-the-socket-open-him-core
Add support for PATCH requests in router middleware
2 parents 201308a + 84cbcbb commit 1053f51

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openhim-core",
33
"description": "The OpenHIM core application that provides logging and routing of http requests",
4-
"version": "8.4.1",
4+
"version": "8.4.2",
55
"main": "./lib/server.js",
66
"bin": {
77
"openhim-core": "./bin/openhim-core.js"

src/middleware/router.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ function sendHttpRequest(ctx, route, options) {
625625
routeReq.destroy(new Error(`Request took longer than ${timeout}ms`))
626626
})
627627

628-
if (ctx.request.method === 'POST' || ctx.request.method === 'PUT') {
628+
if (['POST', 'PUT', 'PATCH'].includes(ctx.request.method)) {
629629
if (ctx.body != null) {
630630
// TODO : Should probally add checks to see if the body is a buffer or string
631631
routeReq.write(ctx.body)

test/unit/routerTest.js

+61
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,37 @@ describe('HTTP Router', () => {
243243
req.method.should.eql('POST')
244244
})
245245

246+
it('should forward PATCH requests correctly', async () => {
247+
const response = 'Hello Patch'
248+
const patchSpy = sinon.spy(() => response)
249+
server = await testUtils.createMockHttpServer(
250+
patchSpy,
251+
constants.HTTP_PORT,
252+
200
253+
)
254+
255+
const channel = {
256+
name: 'PATCH channel',
257+
urlPattern: '.+',
258+
routes: [
259+
{
260+
host: 'localhost',
261+
port: constants.HTTP_PORT,
262+
primary: true
263+
}
264+
]
265+
}
266+
const ctx = createContext(channel, '/test', 'PATCH', 'some body')
267+
await promisify(router.route)(ctx)
268+
Buffer.isBuffer(ctx.response.body).should.be.true()
269+
ctx.response.body.toString().should.eql(response)
270+
271+
patchSpy.callCount.should.be.eql(1)
272+
const call = patchSpy.getCall(0)
273+
const req = call.args[0]
274+
req.method.should.eql('PATCH')
275+
})
276+
246277
it('should handle empty put and post requests correctly', async () => {
247278
const response = 'Hello Empty Post'
248279
const postSpy = sinon.spy(() => response)
@@ -273,6 +304,36 @@ describe('HTTP Router', () => {
273304
req.method.should.eql('POST')
274305
})
275306

307+
it('should handle empty patch requests correctly', async () => {
308+
const response = 'Hello Empty Patch'
309+
const patchSpy = sinon.spy(() => response)
310+
server = await testUtils.createMockHttpServer(
311+
patchSpy,
312+
constants.HTTP_PORT,
313+
200
314+
)
315+
const channel = {
316+
name: 'PATCH channel',
317+
urlPattern: '.+',
318+
routes: [
319+
{
320+
host: 'localhost',
321+
port: constants.HTTP_PORT,
322+
primary: true
323+
}
324+
]
325+
}
326+
const ctx = createContext(channel, '/test', 'PATCH')
327+
await promisify(router.route)(ctx)
328+
Buffer.isBuffer(ctx.response.body).should.be.true()
329+
ctx.response.body.toString().should.eql(response)
330+
331+
patchSpy.callCount.should.be.eql(1)
332+
const call = patchSpy.getCall(0)
333+
const req = call.args[0]
334+
req.method.should.eql('PATCH')
335+
})
336+
276337
it('should send request params if these where received from the incoming request', async () => {
277338
const requestSpy = sinon.spy(() => {})
278339
server = await testUtils.createMockHttpServer(

0 commit comments

Comments
 (0)