Skip to content

Commit 7ae1036

Browse files
committed
Merge pull request #9 from oreillymedia/master
Fixing default cors settings
2 parents 57992f9 + 73c8b30 commit 7ae1036

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

bin/canned

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var canned = require('../canned')
88
.describe('p', 'server port')
99
.default('cors', true)
1010
.describe('cors', 'disable cors support')
11+
.default('headers', false)
12+
.describe('headers', 'add custom headers allowed in cors requests')
1113
.default('h', false)
1214
.alias('h', 'help')
1315
.describe('h', 'show the help')
@@ -21,7 +23,8 @@ if(argv.h) {
2123

2224
var dir = ''
2325
, port = argv.p
24-
, cors = argv.c
26+
, cors = argv.cors
27+
, cors_headers = argv.headers
2528
, logger
2629
, cannedDir
2730

@@ -34,6 +37,6 @@ if(argv.q) {
3437
process.stdout.write('starting canned on port ' + port + ' for ' + cannedDir + '\n')
3538
}
3639

37-
var can = canned(dir, { logger: logger, cors: cors})
40+
var can = canned(dir, { logger: logger, cors: cors, cors_headers: cors_headers})
3841
http.createServer(can).listen(port)
3942

canned.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ var Response = require('./lib/response')
55

66
function Canned(dir, options) {
77
this.logger = options.logger
8-
this.response_opts = { cors_enabled: options.cors }
8+
this.response_opts = {
9+
cors_enabled: options.cors,
10+
cors_headers: options.cors_headers
11+
}
912
this.dir = process.cwd() + '/' + dir
1013
}
1114

lib/response.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Response.cors_headers = [
1414

1515
function Response(content_type, content, statusCode, res, options) {
1616
this.cors_enabled = !!options.cors_enabled
17+
this.cors_headers = options.cors_headers
1718
this.content_type = content_type
1819
this.content = content
1920
this.statusCode = statusCode
@@ -43,8 +44,14 @@ Response.prototype._addContentTypeHeaders = function(headers) {
4344
}
4445

4546
Response.prototype._addCORSHeaders = function(headers) {
47+
var that = this;
4648
if (this.cors_enabled) {
47-
Response.cors_headers.forEach(function(h) { headers.push(h) })
49+
Response.cors_headers.forEach(function(h) {
50+
if(!!that.cors_headers && h[0] == 'Access-Control-Allow-Headers')
51+
headers.push([h[0], h[1] + ", " + that.cors_headers])
52+
else
53+
headers.push(h)
54+
})
4855
}
4956
return headers
5057
}

spec/canned.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,23 @@ describe('canned', function() {
157157
}
158158
can(req, res)
159159
})
160+
161+
it('adds custom headers', function(done) {
162+
var can2 = canned('./spec/test_responses', { cors: true, cors_headers: "Authorization" })
163+
req.url = '/'
164+
var expectedHeaders = {
165+
'Access-Control-Allow-Origin': "*",
166+
'Access-Control-Allow-Headers': "X-Requested-With, Authorization"
167+
}
168+
res.setHeader = function(name, value) {
169+
if(expectedHeaders[name]) {
170+
expect(expectedHeaders[name]).toBe(value)
171+
delete expectedHeaders[name]
172+
}
173+
// all expected headers have been set!
174+
if(Object.keys(expectedHeaders).length === 0) done()
175+
}
176+
can2(req, res)
177+
})
160178
})
161179
})

0 commit comments

Comments
 (0)