From 8bf2ed85e42bf6502c3dfa391dc97506de4c9c6c Mon Sep 17 00:00:00 2001 From: Brett Onions Date: Tue, 6 Feb 2024 11:32:01 +0200 Subject: [PATCH 1/4] Add support for PATCH requests in router middleware --- src/middleware/router.js | 6 +++- test/unit/routerTest.js | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/middleware/router.js b/src/middleware/router.js index 1633c936d..15e07ad42 100644 --- a/src/middleware/router.js +++ b/src/middleware/router.js @@ -625,7 +625,11 @@ 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 ( + ctx.request.method === 'POST' || + ctx.request.method === 'PUT' || + ctx.request.method === 'PATCH' + ) { if (ctx.body != null) { // TODO : Should probally add checks to see if the body is a buffer or string routeReq.write(ctx.body) diff --git a/test/unit/routerTest.js b/test/unit/routerTest.js index 335421857..827eef4cc 100644 --- a/test/unit/routerTest.js +++ b/test/unit/routerTest.js @@ -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) @@ -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( From 4ae2ac84b6a1ccf0561d7b7d5363413d221017f9 Mon Sep 17 00:00:00 2001 From: Brett Onions Date: Tue, 6 Feb 2024 14:03:10 +0200 Subject: [PATCH 2/4] Refactor HTTP method check in router middleware --- src/middleware/router.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/middleware/router.js b/src/middleware/router.js index 15e07ad42..b81825e3f 100644 --- a/src/middleware/router.js +++ b/src/middleware/router.js @@ -625,11 +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' || - ctx.request.method === 'PATCH' - ) { + 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) From 9dcea92298f83be36b842e92a773a72bd3dc3dc1 Mon Sep 17 00:00:00 2001 From: Brett Onions Date: Tue, 6 Feb 2024 14:03:15 +0200 Subject: [PATCH 3/4] 8.5.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index c76e84c52..449180401 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "openhim-core", - "version": "8.3.0", + "version": "8.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4677ca423..e3d25fb61 100644 --- a/package.json +++ b/package.json @@ -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.5.0", "main": "./lib/server.js", "bin": { "openhim-core": "./bin/openhim-core.js" From 84cbcbbe011732b6b91fa16d14a66a1204263ce0 Mon Sep 17 00:00:00 2001 From: Brett Onions Date: Wed, 7 Feb 2024 10:25:01 +0200 Subject: [PATCH 4/4] fx: incorrect version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e3d25fb61..0e24b1f55 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "openhim-core", "description": "The OpenHIM core application that provides logging and routing of http requests", - "version": "8.5.0", + "version": "8.4.2", "main": "./lib/server.js", "bin": { "openhim-core": "./bin/openhim-core.js"