Skip to content

Commit

Permalink
Merge pull request #1101 from mhawila/ohm1100
Browse files Browse the repository at this point in the history
Issue #1100: Request matching should include HTTP methods
  • Loading branch information
MattyJ007 authored May 11, 2021
2 parents c281a34 + 5d8f63f commit 64ce864
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

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

8 changes: 8 additions & 0 deletions src/middleware/requestMatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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 @@ -98,6 +104,8 @@ function matchContentTypes (channel, ctx) {
// TODO: OHM-695 uncomment line below when working on ticket
const 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 @@ -115,6 +115,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 64ce864

Please sign in to comment.