Skip to content

Commit b4caff9

Browse files
committed
add hasHeader and removeHeader, modify headers to lowercase
1 parent 5cfe0b3 commit b4caff9

File tree

2 files changed

+186
-118
lines changed

2 files changed

+186
-118
lines changed

lib/response.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RESPONSE {
2828
// Default the header
2929
this._headers = {
3030
// Set the Content-Type by default
31-
"Content-Type": "application/json" //charset=UTF-8
31+
"content-type": "application/json" //charset=UTF-8
3232
}
3333

3434
// base64 encoding flag
@@ -45,18 +45,28 @@ class RESPONSE {
4545
}
4646

4747
// Adds a header field
48-
header(field,value) {
49-
this._headers[field] = value
48+
header(key,value) {
49+
let _key = key.toLowerCase() // store as lowercase
50+
value = value !== undefined ? value : '' // default value
51+
this._headers[_key] = value // set
5052
return this
5153
}
5254

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
55+
// Gets a header field
56+
getHeader(key) {
57+
if (!key) return this._headers // return all headers
58+
return this._headers[key.toLowerCase()]
59+
}
60+
61+
// Removes a header field
62+
removeHeader(key) {
63+
delete this._headers[key.toLowerCase()]
64+
return this
65+
}
66+
67+
// Returns boolean if header exists
68+
hasHeader(key) {
69+
return this.getHeader(key ? key : '') !== undefined
6070
}
6171

6272
// Convenience method for JSON

test/headers.js

Lines changed: 166 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,28 @@ api.get('/getHeader', function(req,res) {
4343
res.status(200).header('TestHeader','test')
4444
res.json({
4545
headers: res.getHeader(),
46-
typeHeader: res.getHeader('TestHeader'),
47-
typeHeaderCase: res.getHeader('coNtEnt-TyPe'),
48-
typeHeaderMissing: res.getHeader('test')
46+
getHeader: res.getHeader('testheader'),
47+
getHeaderCase: res.getHeader('coNtEnt-TyPe'),
48+
getHeaderMissing: res.getHeader('test') ? false : true,
49+
getHeaderEmpty: res.getHeader() ? false : true
50+
})
51+
})
52+
53+
api.get('/hasHeader', function(req,res) {
54+
res.status(200).header('TestHeader','test')
55+
res.json({
56+
hasHeader: res.hasHeader('testheader'),
57+
hasHeaderCase: res.hasHeader('coNtEnt-TyPe'),
58+
hasHeaderMissing: res.hasHeader('test'),
59+
hasHeaderEmpty: res.hasHeader() ? false : true
60+
})
61+
})
62+
63+
api.get('/removeHeader', function(req,res) {
64+
res.status(200).header('TestHeader','test').header('NewHeader','test').removeHeader('testHeader')
65+
res.json({
66+
removeHeader: res.hasHeader('testheader') ? false : true,
67+
hasHeader: res.hasHeader('NewHeader')
4968
})
5069
})
5170

@@ -78,114 +97,153 @@ api.get('/corsOverride', function(req,res) {
7897

7998
describe('Header Tests:', function() {
8099

81-
it('New Header: /test -- test: testVal', function() {
82-
let _event = Object.assign({},event,{})
83-
84-
return new Promise((resolve,reject) => {
85-
api.run(_event,{},function(err,res) { resolve(res) })
86-
}).then((result) => {
87-
expect(result).to.deep.equal({ headers: { 'Content-Type': 'application/json', 'test': 'testVal' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
88-
})
89-
}) // end it
90-
91-
it('Override Header: /testOveride -- Content-Type: text/html', function() {
92-
let _event = Object.assign({},event,{ path: '/testOverride'})
93-
94-
return new Promise((resolve,reject) => {
95-
api.run(_event,{},function(err,res) { resolve(res) })
96-
}).then((result) => {
97-
expect(result).to.deep.equal({ headers: { 'Content-Type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
98-
})
99-
}) // end it
100-
101-
it('HTML Convenience Method: /testHTML', function() {
102-
let _event = Object.assign({},event,{ path: '/testHTML'})
103-
104-
return new Promise((resolve,reject) => {
105-
api.run(_event,{},function(err,res) { resolve(res) })
106-
}).then((result) => {
107-
expect(result).to.deep.equal({ headers: { 'Content-Type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
108-
})
109-
}) // end it
110-
111-
112-
it('Get Header', function() {
113-
let _event = Object.assign({},event,{ path: '/getHeader'})
114-
115-
return new Promise((resolve,reject) => {
116-
api.run(_event,{},function(err,res) { resolve(res) })
117-
}).then((result) => {
118-
expect(result).to.deep.equal({
119-
headers: {
120-
'Content-Type': 'application/json',
121-
'TestHeader': 'test'
122-
}, statusCode: 200,
123-
body: '{"headers":{"Content-Type":"application/json","TestHeader":"test"},"typeHeader":"test","typeHeaderCase":"application/json","typeHeaderMissing":null}',
124-
isBase64Encoded: false
100+
describe('Standard Tests:', function() {
101+
it('New Header: /test -- test: testVal', function() {
102+
let _event = Object.assign({},event,{})
103+
104+
return new Promise((resolve,reject) => {
105+
api.run(_event,{},function(err,res) { resolve(res) })
106+
}).then((result) => {
107+
expect(result).to.deep.equal({ headers: { 'content-type': 'application/json', 'test': 'testVal' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
108+
})
109+
}) // end it
110+
111+
it('Override Header: /testOveride -- Content-Type: text/html', function() {
112+
let _event = Object.assign({},event,{ path: '/testOverride'})
113+
114+
return new Promise((resolve,reject) => {
115+
api.run(_event,{},function(err,res) { resolve(res) })
116+
}).then((result) => {
117+
expect(result).to.deep.equal({ headers: { 'content-type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
118+
})
119+
}) // end it
120+
121+
it('HTML Convenience Method: /testHTML', function() {
122+
let _event = Object.assign({},event,{ path: '/testHTML'})
123+
124+
return new Promise((resolve,reject) => {
125+
api.run(_event,{},function(err,res) { resolve(res) })
126+
}).then((result) => {
127+
expect(result).to.deep.equal({ headers: { 'content-type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
128+
})
129+
}) // end it
130+
131+
132+
it('Get Header', function() {
133+
let _event = Object.assign({},event,{ path: '/getHeader'})
134+
135+
return new Promise((resolve,reject) => {
136+
api.run(_event,{},function(err,res) { resolve(res) })
137+
}).then((result) => {
138+
expect(result).to.deep.equal({
139+
headers: {
140+
'content-type': 'application/json',
141+
'testheader': 'test'
142+
}, statusCode: 200,
143+
body: '{"headers":{"content-type":"application/json","testheader":"test"},"getHeader":"test","getHeaderCase":"application/json","getHeaderMissing":true,"getHeaderEmpty":false}',
144+
isBase64Encoded: false
145+
})
146+
})
147+
}) // end it
148+
149+
it('Has Header', function() {
150+
let _event = Object.assign({},event,{ path: '/hasHeader'})
151+
152+
return new Promise((resolve,reject) => {
153+
api.run(_event,{},function(err,res) { resolve(res) })
154+
}).then((result) => {
155+
expect(result).to.deep.equal({
156+
headers: {
157+
'content-type': 'application/json',
158+
'testheader': 'test'
159+
}, statusCode: 200,
160+
body: '{"hasHeader":true,"hasHeaderCase":true,"hasHeaderMissing":false,"hasHeaderEmpty":false}',
161+
isBase64Encoded: false
162+
})
163+
})
164+
}) // end it
165+
166+
it('Remove Header', function() {
167+
let _event = Object.assign({},event,{ path: '/removeHeader'})
168+
169+
return new Promise((resolve,reject) => {
170+
api.run(_event,{},function(err,res) { resolve(res) })
171+
}).then((result) => {
172+
expect(result).to.deep.equal({
173+
headers: {
174+
'content-type': 'application/json',
175+
'newheader': 'test'
176+
}, statusCode: 200,
177+
body: '{"removeHeader":true,"hasHeader":true}',
178+
isBase64Encoded: false
179+
})
125180
})
126-
})
127-
}) // end it
128-
129-
130-
it('Add Default CORS Headers', function() {
131-
let _event = Object.assign({},event,{ path: '/cors'})
132-
133-
return new Promise((resolve,reject) => {
134-
api.run(_event,{},function(err,res) { resolve(res) })
135-
}).then((result) => {
136-
expect(result).to.deep.equal({
137-
headers: {
138-
'Content-Type': 'application/json',
139-
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
140-
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS',
141-
'Access-Control-Allow-Origin': '*'
142-
}, statusCode: 200,
143-
body: '{}',
144-
isBase64Encoded: false
181+
}) // end it
182+
183+
}) // end Standard tests
184+
185+
describe('CORS Tests:', function() {
186+
187+
it('Add Default CORS Headers', function() {
188+
let _event = Object.assign({},event,{ path: '/cors'})
189+
190+
return new Promise((resolve,reject) => {
191+
api.run(_event,{},function(err,res) { resolve(res) })
192+
}).then((result) => {
193+
expect(result).to.deep.equal({
194+
headers: {
195+
'content-type': 'application/json',
196+
'access-control-allow-headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
197+
'access-control-allow-methods': 'GET, PUT, POST, DELETE, OPTIONS',
198+
'access-control-allow-origin': '*'
199+
}, statusCode: 200,
200+
body: '{}',
201+
isBase64Encoded: false
202+
})
145203
})
146-
})
147-
}) // end it
148-
149-
it('Add Custom CORS Headers', function() {
150-
let _event = Object.assign({},event,{ path: '/corsCustom'})
151-
152-
return new Promise((resolve,reject) => {
153-
api.run(_event,{},function(err,res) { resolve(res) })
154-
}).then((result) => {
155-
expect(result).to.deep.equal({
156-
headers: {
157-
'Content-Type': 'application/json',
158-
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
159-
'Access-Control-Allow-Methods': 'GET, OPTIONS',
160-
'Access-Control-Allow-Origin': 'example.com',
161-
'Access-Control-Allow-Credentials': 'true',
162-
'Access-Control-Expose-Headers': 'Content-Type',
163-
'Access-Control-Max-Age': '84000'
164-
}, statusCode: 200,
165-
body: '{}',
166-
isBase64Encoded: false
204+
}) // end it
205+
206+
it('Add Custom CORS Headers', function() {
207+
let _event = Object.assign({},event,{ path: '/corsCustom'})
208+
209+
return new Promise((resolve,reject) => {
210+
api.run(_event,{},function(err,res) { resolve(res) })
211+
}).then((result) => {
212+
expect(result).to.deep.equal({
213+
headers: {
214+
'content-type': 'application/json',
215+
'access-control-allow-headers': 'Content-Type, Authorization',
216+
'access-control-allow-methods': 'GET, OPTIONS',
217+
'access-control-allow-origin': 'example.com',
218+
'access-control-allow-credentials': 'true',
219+
'access-control-expose-headers': 'Content-Type',
220+
'access-control-max-age': '84000'
221+
}, statusCode: 200,
222+
body: '{}',
223+
isBase64Encoded: false
224+
})
167225
})
168-
})
169-
}) // end it
170-
171-
it('Override CORS Headers', function() {
172-
let _event = Object.assign({},event,{ path: '/corsOverride'})
173-
174-
return new Promise((resolve,reject) => {
175-
api.run(_event,{},function(err,res) { resolve(res) })
176-
}).then((result) => {
177-
expect(result).to.deep.equal({
178-
headers: {
179-
'Content-Type': 'application/json',
180-
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
181-
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS',
182-
'Access-Control-Allow-Origin': 'example.com',
183-
'Access-Control-Allow-Credentials': 'true'
184-
}, statusCode: 200,
185-
body: '{}',
186-
isBase64Encoded: false
226+
}) // end it
227+
228+
it('Override CORS Headers', function() {
229+
let _event = Object.assign({},event,{ path: '/corsOverride'})
230+
231+
return new Promise((resolve,reject) => {
232+
api.run(_event,{},function(err,res) { resolve(res) })
233+
}).then((result) => {
234+
expect(result).to.deep.equal({
235+
headers: {
236+
'content-type': 'application/json',
237+
'access-control-allow-headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
238+
'access-control-allow-methods': 'GET, PUT, POST, DELETE, OPTIONS',
239+
'access-control-allow-origin': 'example.com',
240+
'access-control-allow-credentials': 'true'
241+
}, statusCode: 200,
242+
body: '{}',
243+
isBase64Encoded: false
244+
})
187245
})
188-
})
189-
}) // end it
246+
}) // end it
190247

248+
}) // end CORS tests
191249
}) // end HEADER tests

0 commit comments

Comments
 (0)