Permalink
Browse files

close #35 by adding head convenience method and updating HEAD aliasing

  • Loading branch information...
jeremydaly committed May 16, 2018
1 parent 8a29ae9 commit befc95ccf4f634015a9284bcccdfa188afdfa8b9
Showing with 20 additions and 6 deletions.
  1. +1 −0 index.js
  2. +5 −6 lib/request.js
  3. +14 −0 test/routes.js
@@ -70,6 +70,7 @@ class API {
patch(p,h) { this.METHOD('PATCH',p,h) }
delete(p,h) { this.METHOD('DELETE',p,h) }
options(p,h) { this.METHOD('OPTIONS',p,h) }
head(p,h) { this.METHOD('HEAD',p,h) }


// METHOD: Adds method and handler to routes
@@ -101,12 +101,11 @@ class REQUEST {
}
} // end for loop

// Alias HEAD to GET method
let method = this.method === 'HEAD' ? 'GET' : this.method

let route = routes['__'+method] ? routes['__'+method] :
(wildcard && wildcard['__'+method] ?
wildcard['__'+method] : undefined)
// Select ROUTE if exist for method, alias HEAD requests, apply wildcards
let route = routes['__'+this.method] ? routes['__'+this.method] :
(this.method === 'HEAD' && routes['__GET'] ? routes['__GET'] :
(wildcard && wildcard['__'+this.method] ? wildcard['__'+this.method] :
undefined))

// Check for the requested method
if (route) {
@@ -156,6 +156,14 @@ api.options('/test_options2/:param1/*', function(req,res) {
res.status(200).json({ method: 'options', status: 'ok', path: '/test_options2/:param1/*', params:req.params})
})

api.get('/override/head/request', (req,res) => {
res.status(200).header('method','get').json({ method: 'get', path: '/override/head/request' })
})

api.head('/override/head/request', (req,res) => {
res.status(200).header('method','head').json({ method: 'head', path: '/override/head/request' })
})

/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
@@ -304,6 +312,12 @@ describe('Route Tests:', function() {
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '', isBase64Encoded: false })
}) // end it

it('Override HEAD request', async function() {
let _event = Object.assign({},event,{ path: '/override/head/request', httpMethod: 'head' })
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json', 'method': 'head' }, statusCode: 200, body: '', isBase64Encoded: false })
}) // end it

}) // end HEAD tests

/******************/

0 comments on commit befc95c

Please sign in to comment.