Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ function HttpServer(options) {
});

if (options.cors) {
this.headers['Access-Control-Allow-Origin'] = '*';
this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
if (options.corsHeaders) {
options.corsHeaders.split(/\s*,\s*/)
.forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
}
before.push(corser.create(options.corsHeaders ? {
requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
} : null));
var allowedHeaders =
['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Range']
.concat(options.corsHeaders ? options.corsHeaders.split(', ') : []);

before.push(corser.create({
requestHeaders: allowedHeaders
}));
}

if (options.robots) {
Expand Down
27 changes: 24 additions & 3 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,27 @@ vows.describe('http-server').addBatch({
server.listen(8082);
this.callback(null, server);
},
'and given OPTIONS request': {
'and given OPTIONS request with an allowed (and custom) header': {
topic: function () {
request({
method: 'OPTIONS',
uri: 'http://127.0.0.1:8082/',
headers: {
'Access-Control-Request-Method': 'GET',
Origin: 'http://example.com',
'Access-Control-Request-Headers': 'X-Test'
}
}, this.callback);
},
'status code should be 204': function (err, res) {
assert.equal(res.statusCode, 204);
},
'response Access-Control-Allow-Headers should contain X-Test and allow the origin': function (err, res) {
assert.ok(/\bX-Test\b/i.test(res.headers['access-control-allow-headers']));
assert.ok(res.headers['access-control-allow-origin'] === '*');
}
},
'and given OPTIONS request with a disallowed header': {
topic: function () {
request({
method: 'OPTIONS',
Expand All @@ -150,8 +170,9 @@ vows.describe('http-server').addBatch({
'status code should be 204': function (err, res) {
assert.equal(res.statusCode, 204);
},
'response Access-Control-Allow-Headers should contain X-Test': function (err, res) {
assert.ok(res.headers['access-control-allow-headers'].split(/\s*,\s*/g).indexOf('X-Test') >= 0, 204);
'Should not allow the origin': function (err, res) {
assert.ok(res.headers['access-control-allow-headers'] === undefined);
assert.ok(res.headers['access-control-allow-origin'] === undefined);
}
}
}
Expand Down