Skip to content

Commit 04a355d

Browse files
committed
close #21 with cors() method
1 parent 41ff595 commit 04a355d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

lib/response.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,29 @@ class RESPONSE {
317317
// TODO: sendStatus
318318

319319

320+
// Convenience method for setting CORS headers
321+
cors(options) {
322+
const opts = typeof options === 'object' ? options : {}
323+
324+
// Check for existing headers
325+
let acao = this.getHeader('Access-Control-Allow-Origin')
326+
let acam = this.getHeader('Access-Control-Allow-Methods')
327+
let acah = this.getHeader('Access-Control-Allow-Headers')
328+
329+
// Default CORS headers
330+
this.header('Access-Control-Allow-Origin',opts.origin ? opts.origin : (acao ? acao : '*'))
331+
this.header('Access-Control-Allow-Methods',opts.methods ? opts.methods : (acam ? acam : 'GET, PUT, POST, DELETE, OPTIONS'))
332+
this.header('Access-Control-Allow-Headers',opts.headers ? opts.headers : (acah ? acah : 'Content-Type, Authorization, Content-Length, X-Requested-With'))
333+
334+
// Optional CORS headers
335+
if(opts.maxAge && !isNaN(opts.maxAge)) this.header('Access-Control-Max-Age',(opts.maxAge/1000|0).toString())
336+
if(opts.credentials) this.header('Access-Control-Allow-Credentials',opts.credentials.toString())
337+
if(opts.exposeHeaders) this.header('Access-Control-Expose-Headers',opts.exposeHeaders)
338+
339+
return this
340+
}
341+
342+
320343

321344
// Sends the request to the main callback
322345
send(body) {

test/headers.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ api.get('/getHeader', function(req,res) {
4949
})
5050
})
5151

52+
api.get('/cors', function(req,res) {
53+
res.cors().json({})
54+
})
55+
56+
api.get('/corsCustom', function(req,res) {
57+
res.cors({
58+
origin: 'example.com',
59+
methods: 'GET, OPTIONS',
60+
headers: 'Content-Type, Authorization',
61+
maxAge: 84000000,
62+
credentials: true,
63+
exposeHeaders: 'Content-Type'
64+
}).json({})
65+
})
66+
67+
api.get('/corsOverride', function(req,res) {
68+
res.cors().cors({
69+
origin: 'example.com',
70+
credentials: true
71+
}).json({})
72+
})
73+
5274

5375
/******************************************************************************/
5476
/*** BEGIN TESTS ***/
@@ -104,4 +126,66 @@ describe('Header Tests:', function() {
104126
})
105127
}) // end it
106128

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
145+
})
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
167+
})
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
187+
})
188+
})
189+
}) // end it
190+
107191
}) // end HEADER tests

0 commit comments

Comments
 (0)