Skip to content

Commit befc95c

Browse files
committed
close #35 by adding head convenience method and updating HEAD aliasing
1 parent 8a29ae9 commit befc95c

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class API {
7070
patch(p,h) { this.METHOD('PATCH',p,h) }
7171
delete(p,h) { this.METHOD('DELETE',p,h) }
7272
options(p,h) { this.METHOD('OPTIONS',p,h) }
73+
head(p,h) { this.METHOD('HEAD',p,h) }
7374

7475

7576
// METHOD: Adds method and handler to routes

lib/request.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,11 @@ class REQUEST {
101101
}
102102
} // end for loop
103103

104-
// Alias HEAD to GET method
105-
let method = this.method === 'HEAD' ? 'GET' : this.method
106-
107-
let route = routes['__'+method] ? routes['__'+method] :
108-
(wildcard && wildcard['__'+method] ?
109-
wildcard['__'+method] : undefined)
104+
// Select ROUTE if exist for method, alias HEAD requests, apply wildcards
105+
let route = routes['__'+this.method] ? routes['__'+this.method] :
106+
(this.method === 'HEAD' && routes['__GET'] ? routes['__GET'] :
107+
(wildcard && wildcard['__'+this.method] ? wildcard['__'+this.method] :
108+
undefined))
110109

111110
// Check for the requested method
112111
if (route) {

test/routes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ api.options('/test_options2/:param1/*', function(req,res) {
156156
res.status(200).json({ method: 'options', status: 'ok', path: '/test_options2/:param1/*', params:req.params})
157157
})
158158

159+
api.get('/override/head/request', (req,res) => {
160+
res.status(200).header('method','get').json({ method: 'get', path: '/override/head/request' })
161+
})
162+
163+
api.head('/override/head/request', (req,res) => {
164+
res.status(200).header('method','head').json({ method: 'head', path: '/override/head/request' })
165+
})
166+
159167
/******************************************************************************/
160168
/*** BEGIN TESTS ***/
161169
/******************************************************************************/
@@ -304,6 +312,12 @@ describe('Route Tests:', function() {
304312
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json' }, statusCode: 404, body: '', isBase64Encoded: false })
305313
}) // end it
306314

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

309323
/******************/

0 commit comments

Comments
 (0)