Skip to content

Commit 22793a5

Browse files
committed
close #23 with new getHeader() method
1 parent 133fe2d commit 22793a5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/response.js

Lines changed: 9 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -50,6 +50,15 @@ class RESPONSE {
50
return this
50
return this
51
}
51
}
52

52

53+
// Adds a header field
54+
getHeader(field) {
55+
if (!field) return this._headers // return all headers
56+
for (const header in this._headers) { // loop through headers
57+
if (header.toLowerCase() === field.toLowerCase()) return this._headers[header]
58+
} // end for
59+
return null // return null for not found
60+
}
61+
53
// Convenience method for JSON
62
// Convenience method for JSON
54
json(body) {
63
json(body) {
55
this.header('Content-Type','application/json').send(JSON.stringify(body))
64
this.header('Content-Type','application/json').send(JSON.stringify(body))

test/headers.js

Lines changed: 28 additions & 0 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -39,6 +39,16 @@ api.get('/testJSONP', function(req,res) {
39
res.status(200).html({ method: 'get', status: 'ok' })
39
res.status(200).html({ method: 'get', status: 'ok' })
40
})
40
})
41

41

42+
api.get('/getHeader', function(req,res) {
43+
res.status(200).header('TestHeader','test')
44+
res.json({
45+
headers: res.getHeader(),
46+
typeHeader: res.getHeader('TestHeader'),
47+
typeHeaderCase: res.getHeader('coNtEnt-TyPe'),
48+
typeHeaderMissing: res.getHeader('test')
49+
})
50+
})
51+
42

52

43
/******************************************************************************/
53
/******************************************************************************/
44
/*** BEGIN TESTS ***/
54
/*** BEGIN TESTS ***/
@@ -76,4 +86,22 @@ describe('Header Tests:', function() {
76
})
86
})
77
}) // end it
87
}) // end it
78

88

89+
90+
it('Get Header', function() {
91+
let _event = Object.assign({},event,{ path: '/getHeader'})
92+
93+
return new Promise((resolve,reject) => {
94+
api.run(_event,{},function(err,res) { resolve(res) })
95+
}).then((result) => {
96+
expect(result).to.deep.equal({
97+
headers: {
98+
'Content-Type': 'application/json',
99+
'TestHeader': 'test'
100+
}, statusCode: 200,
101+
body: '{"headers":{"Content-Type":"application/json","TestHeader":"test"},"typeHeader":"test","typeHeaderCase":"application/json","typeHeaderMissing":null}',
102+
isBase64Encoded: false
103+
})
104+
})
105+
}) // end it
106+
79
}) // end HEADER tests
107
}) // end HEADER tests

0 commit comments

Comments
 (0)