Skip to content

Commit

Permalink
Merge pull request #30 from Soarez/master
Browse files Browse the repository at this point in the history
add vary origin header for specifc origins
  • Loading branch information
troygoode committed Oct 23, 2014
2 parents 210446e + 37ca05d commit b0e3ae3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@
};

function configureOrigin(options, req) {
var origin = options.origin;
var origin = options.origin, header;
if (origin === true) {
origin = req.headers.origin;
} else if (!origin) {
origin = '*';
}
return {
header = {
key: 'Access-Control-Allow-Origin',
value: origin
};
if (origin !== '*') {
return [
{
key: 'Vary',
value: 'Origin'
},
header
];
}
return header;
}

function configureMethods(options) {
Expand Down Expand Up @@ -92,12 +102,16 @@
method = req.method && req.method.toUpperCase && req.method.toUpperCase(),
applyHeaders = function (headers, res) {
headers.forEach(function (header) {
if (header && header.value) {
if (res.set) {
res.set(header.key, header.value);
} else {
// for Express <4
res.setHeader(header.key, header.value);
if (header) {
if (Array.isArray(header)) {
return applyHeaders(header, res);
} else if (header.value) {
if (res.set) {
res.set(header.key, header.value);
} else {
// for Express <4
res.setHeader(header.key, header.value);
}
}
}
});
Expand Down
18 changes: 18 additions & 0 deletions test/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@
cors(options)(req, res, next);
});

it('includes vary origin header for specific origins', function (done) {
// arrange
var req, res, next, options;
options = {
origin: 'example.com'
};
req = fakeRequest();
res = fakeResponse();
next = function () {
// assert
res.getHeader('Vary').should.equal('Origin');
done();
};

// act
cors(options)(req, res, next);
});

it('origin defaults to *', function (done) {
// arrange
var req, res, next, options;
Expand Down

0 comments on commit b0e3ae3

Please sign in to comment.