Skip to content

Commit 34bb29b

Browse files
committed
fix 'route not found' error with wildcard paths
1 parent 149ff6c commit 34bb29b

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/request.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,12 @@ class REQUEST {
196196
} else if (
197197
wc[wc.length-1]
198198
&& wc[wc.length-1]['METHODS']
199-
&& (wc[wc.length-1]['METHODS'][this.method] || wc[wc.length-1]['METHODS']['ANY'])
200-
&& (this.method !== 'OPTIONS' || this.validWildcard(wc,this.method))
199+
// && (wc[wc.length-1]['METHODS'][this.method] || wc[wc.length-1]['METHODS']['ANY'])
200+
&& (
201+
(this.method !== 'OPTIONS'
202+
&& Object.keys(wc[wc.length-1]['METHODS']).toString() !== 'OPTIONS')
203+
|| this.validWildcard(wc,this.method)
204+
)
201205
) {
202206
routes = wc[wc.length-1]
203207
} else {

test/routes.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const api3 = require('../index')({ version: 'v1.0', logger: false })
99
const api4 = require('../index')({ version: 'v1.0', logger: false })
1010
const api5 = require('../index')({ version: 'v1.0', logger: false })
1111
const api6 = require('../index')({ version: 'v1.0', logger: false })
12+
const api7 = require('../index')({ version: 'v1.0', logger: false })
1213

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

211+
api.get('/methodNotAllowed', (req,res) => {
212+
res.send({status: 'OK'})
213+
})
214+
210215
// Multi methods
211216
api3.METHOD('get,post','/multimethod/test', (req,res) => {
212217
res.status(200).json({ method: req.method, path: '/multimethod/test' })
@@ -258,6 +263,10 @@ api6.get('/test', function testHandler(req,res) {
258263
res.send({ status: 'ok' })
259264
})
260265

266+
api7.get(function(req,res) {
267+
res.status(200).json({ method: 'get', status: 'ok' })
268+
})
269+
261270

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

404+
it('Method not allowed', async function() {
405+
let _event = Object.assign({},event,{ path: '/methodNotAllowed', httpMethod: 'post' })
406+
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
407+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
408+
}) // end it
409+
410+
411+
it('Method not allowed (/* path - valid method)', async function() {
412+
let _event = Object.assign({},event,{ path: '/methodNotAllowedStar', httpMethod: 'get' })
413+
let result = await new Promise(r => api7.run(_event,{},(e,res) => { r(res) }))
414+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
415+
}) // end it
416+
417+
it('Method not allowed (/* path)', async function() {
418+
let _event = Object.assign({},event,{ path: '/methodNotAllowedStar', httpMethod: 'post' })
419+
let result = await new Promise(r => api7.run(_event,{},(e,res) => { r(res) }))
420+
expect(result).to.deep.equal({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 405, body: '{"error":"Method not allowed"}', isBase64Encoded: false })
421+
}) // end it
422+
395423
}) // end GET tests
396424

397425

0 commit comments

Comments
 (0)