Permalink
Browse files

close #23 with new getHeader() method

  • Loading branch information...
jeremydaly committed Apr 23, 2018
1 parent 133fe2d commit 22793a58a86ea8813b5dba6fefb56ee27a947eb9
Showing with 37 additions and 0 deletions.
  1. +9 −0 lib/response.js
  2. +28 −0 test/headers.js
@@ -50,6 +50,15 @@ class RESPONSE {
return this
}

// Adds a header field
getHeader(field) {
if (!field) return this._headers // return all headers
for (const header in this._headers) { // loop through headers
if (header.toLowerCase() === field.toLowerCase()) return this._headers[header]
} // end for
return null // return null for not found
}

// Convenience method for JSON
json(body) {
this.header('Content-Type','application/json').send(JSON.stringify(body))
@@ -39,6 +39,16 @@ api.get('/testJSONP', function(req,res) {
res.status(200).html({ method: 'get', status: 'ok' })
})

api.get('/getHeader', function(req,res) {
res.status(200).header('TestHeader','test')
res.json({
headers: res.getHeader(),
typeHeader: res.getHeader('TestHeader'),
typeHeaderCase: res.getHeader('coNtEnt-TyPe'),
typeHeaderMissing: res.getHeader('test')
})
})


/******************************************************************************/
/*** BEGIN TESTS ***/
@@ -76,4 +86,22 @@ describe('Header Tests:', function() {
})
}) // end it


it('Get Header', function() {
let _event = Object.assign({},event,{ path: '/getHeader'})

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
expect(result).to.deep.equal({
headers: {
'Content-Type': 'application/json',
'TestHeader': 'test'
}, statusCode: 200,
body: '{"headers":{"Content-Type":"application/json","TestHeader":"test"},"typeHeader":"test","typeHeaderCase":"application/json","typeHeaderMissing":null}',
isBase64Encoded: false
})
})
}) // end it

}) // end HEADER tests

0 comments on commit 22793a5

Please sign in to comment.