Permalink
Browse files

close #20 with additional wildcard support

  • Loading branch information...
jeremydaly committed Apr 24, 2018
1 parent 03ca08f commit 28fead20b20c440793753eb8112427cde8fed6c3
Showing with 66 additions and 30 deletions.
  1. +17 −15 lib/request.js
  2. +49 −15 test/routes.js
@@ -79,8 +79,15 @@ class REQUEST {
// Create a local routes reference
let routes = app._routes

let wildcard = {}

// Loop the routes and see if this matches
for (let i=0; i<path.length; i++) {

// Capture wildcard routes
if (routes['*']) { wildcard = routes['*'] }

// Traverse routes
if (routes[path[i]]) {
routes = routes[path[i]]
} else if (routes['__VAR__']) {
@@ -91,31 +98,26 @@ class REQUEST {
}
} // end for loop

let route = routes['__'+this.method] ? routes['__'+this.method] :
(wildcard && wildcard['__'+this.method] ?
wildcard['__'+this.method] : undefined)

// Check for the requested method
if (routes['__'+this.method]) {
if (route) {

// Assign path parameters
for (let x in routes['__'+this.method].vars) {
this.params[routes['__'+this.method].vars[x]] = path[x]
for (let x in route.vars) {
this.params[route.vars[x]] = path[x]
} // end for

// Set the route used
this.route = routes['__'+this.method].route

// Set the handler
app.handler = routes['__'+this.method].handler

// TODO: This needs to start looking from the root (leave this for now)
} else if (app._routes['*'] && app._routes['*']['__'+this.method]) {

// Set the route used
this.route = app._routes['*']['__'+this.method].route
this.route = route.route

// Set the handler
app.handler = app._routes['*']['__'+this.method].handler
app.handler = route.handler

} else {
app._errorStatus = 405;
app._errorStatus = 405
throw new Error('Method not allowed')
}

@@ -131,13 +131,25 @@ api.put('/test/form', function(req,res) {
res.status(200).json({ method: 'put', status: 'ok', body: req.body })
})

api.METHOD('TEST','/test/:param1/queryx', function(req,res) {
res.status(200).json({ method: 'test', status: 'ok', body: req.body })
})

api.METHOD('TEST','/test_options2/:param1/test', function(req,res) {
res.status(200).json({ method: 'test', status: 'ok', body: req.body })
})

api.options('/*', function(req,res) {
res.status(200).json({ method: 'options', status: 'ok', path: '/*'})
})

// api.options('/test_options2/*', function(req,res) {
// res.status(200).json({ method: 'options', status: 'ok', path: '/test_options2/*'})
// })
api.options('/test_options2/*', function(req,res) {
res.status(200).json({ method: 'options', status: 'ok', path: '/test_options2/*'})
})

api.options('/test_options2/:param1/*', function(req,res) {
res.status(200).json({ method: 'options', status: 'ok', path: '/test_options2/:param1/*', params:req.params})
})

/******************************************************************************/
/*** BEGIN TESTS ***/
@@ -650,9 +662,20 @@ describe('Route Tests:', function() {
})
}) // end it

it('Wildcard with parameter: /test_options2/123', function() {
it('Wildcard with path: /test_options2/123', function() {
let _event = Object.assign({},event,{ path: '/test_options2/123', httpMethod: 'options' })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
// console.log(result);
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","path":"/test_options2/*"}', isBase64Encoded: false })
})
}) // end it

it('Wildcard with deep path: /test/param1/queryx', function() {
let _event = Object.assign({},event,{ path: '/test/param1/queryx', httpMethod: 'options' })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
@@ -661,16 +684,27 @@ describe('Route Tests:', function() {
})
}) // end it

// it('Nested Wildcard: /test_options2', function() {
// let _event = Object.assign({},event,{ path: '/test_options2/test', httpMethod: 'options' })
//
// return new Promise((resolve,reject) => {
// api.run(_event,{},function(err,res) { resolve(res) })
// }).then((result) => {
// // console.log(result);
// expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","path":"/test_options2/*"}' })
// })
// }) // end it
it('Nested Wildcard: /test_options2', function() {
let _event = Object.assign({},event,{ path: '/test_options2/test', httpMethod: 'options' })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
//console.log(JSON.stringify(api._routes,null,2));
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","path":"/test_options2/*"}', isBase64Encoded: false })
})
}) // end it

it('Nested Wildcard with parameters: /test_options2/param1/test', function() {
let _event = Object.assign({},event,{ path: '/test_options2/param1/test', httpMethod: 'options' })

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
//console.log(JSON.stringify(api._routes,null,2));
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json' }, statusCode: 200, body: '{"method":"options","status":"ok","path":"/test_options2/:param1/*","params":{"param1":"param1"}}', isBase64Encoded: false })
})
}) // end it

it('Missing path: /not_found', function() {
let _event = Object.assign({},event,{ path: '/not_found', httpMethod: 'options' })
@@ -683,6 +717,6 @@ describe('Route Tests:', function() {
})
}) // end it

}) // end DELETE tests
}) // end OPTIONS tests

}) // end ROUTE tests

0 comments on commit 28fead2

Please sign in to comment.