Skip to content

Commit

Permalink
Issue jembi#1100: Request matching should include HTTP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mhawila committed Sep 12, 2020
1 parent f6a8c2e commit e6321c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
35 changes: 16 additions & 19 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions 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": "5.4.0",
"version": "5.4.1",
"main": "./lib/server.js",
"bin": {
"openhim-core": "./bin/openhim-core.js"
Expand Down Expand Up @@ -57,7 +57,7 @@
"koa-bodyparser": "^4.3.0",
"koa-compress": "3.0.0",
"koa-route": "3.2.0",
"lodash": "^4.17.15",
"lodash": "^4.17.20",
"moment": "^2.25.3",
"moment-timezone": "^0.5.31",
"mongodb": "^3.5.7",
Expand Down
7 changes: 7 additions & 0 deletions src/middleware/requestMatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ function matchUrlPattern (channel, ctx) {
return pat.test(ctx.request.path)
}

function matchMethod(channel, ctx) {
let found = channel.methods.find(method => ctx.request.method.toUpperCase() === method);
if(found) return true;
return false;
}

function matchContentTypes (channel, ctx) {
if ((channel.matchContentTypes != null ? channel.matchContentTypes.length : undefined) > 0) {
if (ctx.request.header && ctx.request.header['content-type']) {
Expand All @@ -96,6 +102,7 @@ function matchContentTypes (channel, ctx) {
// eslint-disable-next-line
let matchFunctions = [
matchUrlPattern,
matchMethod,
matchContent,
matchContentTypes
]
Expand Down
16 changes: 16 additions & 0 deletions test/unit/requestMatchingTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ describe('Request Matching middleware', () => {
})
})

describe('.matchMethod', () => {
let matchMethod = requestMatching.__get__('matchMethod')
let channel = { methods: ['GET', 'POST', 'DELETE'] }

it('should match a request http method', () => {
let actual = matchMethod(channel, { request: { method: 'GET'}})
return actual.should.be.true()
})

it('should reject request with excluded method', () => {
// PUT is not included in the channel definition
let actual = matchMethod(channel, { request: { method: 'PUT'}})
return actual.should.be.false()
})
})

describe('.matchContentTypes', () => {
it('should match correct content types', () => {
const matchContentTypes = requestMatching.__get__('matchContentTypes')
Expand Down

0 comments on commit e6321c4

Please sign in to comment.