Permalink
Browse files

add hasHeader and removeHeader, modify headers to lowercase

  • Loading branch information...
jeremydaly committed Apr 24, 2018
1 parent 5cfe0b3 commit b4caff921d5d79422a3e262dbecc3dbf3c90297b
Showing with 186 additions and 118 deletions.
  1. +20 −10 lib/response.js
  2. +166 −108 test/headers.js
@@ -28,7 +28,7 @@ class RESPONSE {
// Default the header
this._headers = {
// Set the Content-Type by default
"Content-Type": "application/json" //charset=UTF-8
"content-type": "application/json" //charset=UTF-8
}

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

// Adds a header field
header(field,value) {
this._headers[field] = value
header(key,value) {
let _key = key.toLowerCase() // store as lowercase
value = value !== undefined ? value : '' // default value
this._headers[_key] = value // set
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
// Gets a header field
getHeader(key) {
if (!key) return this._headers // return all headers
return this._headers[key.toLowerCase()]
}

// Removes a header field
removeHeader(key) {
delete this._headers[key.toLowerCase()]
return this
}

// Returns boolean if header exists
hasHeader(key) {
return this.getHeader(key ? key : '') !== undefined
}

// Convenience method for JSON
@@ -43,9 +43,28 @@ 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')
getHeader: res.getHeader('testheader'),
getHeaderCase: res.getHeader('coNtEnt-TyPe'),
getHeaderMissing: res.getHeader('test') ? false : true,
getHeaderEmpty: res.getHeader() ? false : true
})
})

api.get('/hasHeader', function(req,res) {
res.status(200).header('TestHeader','test')
res.json({
hasHeader: res.hasHeader('testheader'),
hasHeaderCase: res.hasHeader('coNtEnt-TyPe'),
hasHeaderMissing: res.hasHeader('test'),
hasHeaderEmpty: res.hasHeader() ? false : true
})
})

api.get('/removeHeader', function(req,res) {
res.status(200).header('TestHeader','test').header('NewHeader','test').removeHeader('testHeader')
res.json({
removeHeader: res.hasHeader('testheader') ? false : true,
hasHeader: res.hasHeader('NewHeader')
})
})

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

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

it('New Header: /test -- test: testVal', function() {
let _event = Object.assign({},event,{})

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', 'test': 'testVal' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
})
}) // end it

it('Override Header: /testOveride -- Content-Type: text/html', function() {
let _event = Object.assign({},event,{ path: '/testOverride'})

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
expect(result).to.deep.equal({ headers: { 'Content-Type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
})
}) // end it

it('HTML Convenience Method: /testHTML', function() {
let _event = Object.assign({},event,{ path: '/testHTML'})

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
expect(result).to.deep.equal({ headers: { 'Content-Type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
})
}) // 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
describe('Standard Tests:', function() {
it('New Header: /test -- test: testVal', function() {
let _event = Object.assign({},event,{})

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', 'test': 'testVal' }, statusCode: 200, body: '{"method":"get","status":"ok"}', isBase64Encoded: false })
})
}) // end it

it('Override Header: /testOveride -- Content-Type: text/html', function() {
let _event = Object.assign({},event,{ path: '/testOverride'})

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
expect(result).to.deep.equal({ headers: { 'content-type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
})
}) // end it

it('HTML Convenience Method: /testHTML', function() {
let _event = Object.assign({},event,{ path: '/testHTML'})

return new Promise((resolve,reject) => {
api.run(_event,{},function(err,res) { resolve(res) })
}).then((result) => {
expect(result).to.deep.equal({ headers: { 'content-type': 'text/html' }, statusCode: 200, body: '<div>testHTML</div>', isBase64Encoded: false })
})
}) // 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"},"getHeader":"test","getHeaderCase":"application/json","getHeaderMissing":true,"getHeaderEmpty":false}',
isBase64Encoded: false
})
})
}) // end it

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

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: '{"hasHeader":true,"hasHeaderCase":true,"hasHeaderMissing":false,"hasHeaderEmpty":false}',
isBase64Encoded: false
})
})
}) // end it

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

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',
'newheader': 'test'
}, statusCode: 200,
body: '{"removeHeader":true,"hasHeader":true}',
isBase64Encoded: false
})
})
})
}) // end it


it('Add Default CORS Headers', function() {
let _event = Object.assign({},event,{ path: '/cors'})

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',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS',
'Access-Control-Allow-Origin': '*'
}, statusCode: 200,
body: '{}',
isBase64Encoded: false
}) // end it

}) // end Standard tests

describe('CORS Tests:', function() {

it('Add Default CORS Headers', function() {
let _event = Object.assign({},event,{ path: '/cors'})

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',
'access-control-allow-headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
'access-control-allow-methods': 'GET, PUT, POST, DELETE, OPTIONS',
'access-control-allow-origin': '*'
}, statusCode: 200,
body: '{}',
isBase64Encoded: false
})
})
})
}) // end it

it('Add Custom CORS Headers', function() {
let _event = Object.assign({},event,{ path: '/corsCustom'})

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',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Expose-Headers': 'Content-Type',
'Access-Control-Max-Age': '84000'
}, statusCode: 200,
body: '{}',
isBase64Encoded: false
}) // end it

it('Add Custom CORS Headers', function() {
let _event = Object.assign({},event,{ path: '/corsCustom'})

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',
'access-control-allow-headers': 'Content-Type, Authorization',
'access-control-allow-methods': 'GET, OPTIONS',
'access-control-allow-origin': 'example.com',
'access-control-allow-credentials': 'true',
'access-control-expose-headers': 'Content-Type',
'access-control-max-age': '84000'
}, statusCode: 200,
body: '{}',
isBase64Encoded: false
})
})
})
}) // end it

it('Override CORS Headers', function() {
let _event = Object.assign({},event,{ path: '/corsOverride'})

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',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS',
'Access-Control-Allow-Origin': 'example.com',
'Access-Control-Allow-Credentials': 'true'
}, statusCode: 200,
body: '{}',
isBase64Encoded: false
}) // end it

it('Override CORS Headers', function() {
let _event = Object.assign({},event,{ path: '/corsOverride'})

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',
'access-control-allow-headers': 'Content-Type, Authorization, Content-Length, X-Requested-With',
'access-control-allow-methods': 'GET, PUT, POST, DELETE, OPTIONS',
'access-control-allow-origin': 'example.com',
'access-control-allow-credentials': 'true'
}, statusCode: 200,
body: '{}',
isBase64Encoded: false
})
})
})
}) // end it
}) // end it

}) // end CORS tests
}) // end HEADER tests

0 comments on commit b4caff9

Please sign in to comment.