Skip to content

Commit

Permalink
fix 'route not found' error with wildcard paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed Jan 6, 2019
1 parent 149ff6c commit 34bb29b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ class REQUEST {
} else if (
wc[wc.length-1]
&& wc[wc.length-1]['METHODS']
&& (wc[wc.length-1]['METHODS'][this.method] || wc[wc.length-1]['METHODS']['ANY'])
&& (this.method !== 'OPTIONS' || this.validWildcard(wc,this.method))
// && (wc[wc.length-1]['METHODS'][this.method] || wc[wc.length-1]['METHODS']['ANY'])
&& (
(this.method !== 'OPTIONS'
&& Object.keys(wc[wc.length-1]['METHODS']).toString() !== 'OPTIONS')
|| this.validWildcard(wc,this.method)
)
) {
routes = wc[wc.length-1]
} else {
Expand Down
28 changes: 28 additions & 0 deletions test/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const api3 = require('../index')({ version: 'v1.0', logger: false })
const api4 = require('../index')({ version: 'v1.0', logger: false })
const api5 = require('../index')({ version: 'v1.0', logger: false })
const api6 = require('../index')({ version: 'v1.0', logger: false })
const api7 = require('../index')({ version: 'v1.0', logger: false })

let event = {
httpMethod: 'get',
Expand Down Expand Up @@ -207,6 +208,10 @@ api.head('/head/*', (req,res) => {
res.status(200).header('wildcard',true).json({ })
})

api.get('/methodNotAllowed', (req,res) => {
res.send({status: 'OK'})
})

// Multi methods
api3.METHOD('get,post','/multimethod/test', (req,res) => {
res.status(200).json({ method: req.method, path: '/multimethod/test' })
Expand Down Expand Up @@ -258,6 +263,10 @@ api6.get('/test', function testHandler(req,res) {
res.send({ status: 'ok' })
})

api7.get(function(req,res) {
res.status(200).json({ method: 'get', status: 'ok' })
})


/******************************************************************************/
/*** BEGIN TESTS ***/
Expand Down Expand Up @@ -392,6 +401,25 @@ describe('Route Tests:', function() {
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('Method not allowed', async function() {
let _event = Object.assign({},event,{ path: '/methodNotAllowed', httpMethod: 'post' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
}) // end it


it('Method not allowed (/* path - valid method)', async function() {
let _event = Object.assign({},event,{ path: '/methodNotAllowedStar', httpMethod: 'get' })
let result = await new Promise(r => api7.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('Method not allowed (/* path)', async function() {
let _event = Object.assign({},event,{ path: '/methodNotAllowedStar', httpMethod: 'post' })
let result = await new Promise(r => api7.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
}) // end it

}) // end GET tests


Expand Down

0 comments on commit 34bb29b

Please sign in to comment.