Skip to content

Commit

Permalink
#30: use the vary module
Browse files Browse the repository at this point in the history
  • Loading branch information
troygoode committed Nov 7, 2014
1 parent aaa1a4e commit 165f35e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
27 changes: 11 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,23 @@

'use strict';

var defaults = {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'
};
var vary = require('vary'),
defaults = {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE'
};

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

function configureMethods(options) {
Expand Down Expand Up @@ -107,6 +98,7 @@
return applyHeaders(header, res);
} else if (header.value) {
if (res.set) {
// for Express 4+
res.set(header.key, header.value);
} else {
// for Express <4
Expand All @@ -115,6 +107,9 @@
}
}
});
if (res.get('Origin') !== '*') {

This comment has been minimized.

Copy link
@cgiffard

cgiffard Nov 10, 2014

Gak! This one line broke the ability to use this middleware outside of express. (worked beautifully before!) :-(

This comment has been minimized.

Copy link
@troygoode

troygoode Nov 10, 2014

Author Member

actually this probably breaks express < 4 as well... sorry about that. I'll get a fix out

This comment has been minimized.

Copy link
@troygoode

troygoode Nov 10, 2014

Author Member

@cgiffard this is resolved in 2.5.2 on npm. see issue #33

vary(res, 'Origin');
}
};

if (method === 'OPTIONS') {
Expand Down
72 changes: 37 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
{
"name": "cors"
, "version": "2.5.0"
, "author": "Troy Goode <troygoode@gmail.com> (https://github.com/troygoode/)"
, "description": "middleware for dynamically or statically enabling CORS in express/connect applications"
, "keywords": ["cors", "express", "connect", "middleware"]
, "homepage": "https://github.com/troygoode/node-cors/"
, "repository": {
"type": "git"
, "url": "git://github.com/troygoode/node-cors.git"
}
, "contributors": [
"name": "cors",
"version": "2.5.1",
"author": "Troy Goode <troygoode@gmail.com> (https://github.com/troygoode/)",
"description": "middleware for dynamically or statically enabling CORS in express/connect applications",
"keywords": ["cors", "express", "connect", "middleware"],
"homepage": "https://github.com/troygoode/node-cors/",
"repository": {
"type": "git",
"url": "git://github.com/troygoode/node-cors.git"
},
"contributors": [
{
"name": "Troy Goode"
, "email": "troygoode@gmail.com"
, "web": "https://github.com/troygoode/"
"name": "Troy Goode",
"email": "troygoode@gmail.com",
"web": "https://github.com/troygoode/"
}
]
, "licenses": [
],
"licenses": [
{"type": "MIT", "url": "http://www.opensource.org/licenses/mit-license.php"}
]
, "bugs": {"url": "https://github.com/troygoode/node-cors/issues"}
, "main": "./lib/index.js"
, "engines": {
"node": ">=0.10.0"
}
, "dependencies": {}
, "devDependencies": {
"basic-auth-connect": "^1"
, "body-parser": "^1.4.3"
, "express": "^4"
, "lint": "^1.1.2"
, "mocha": "^1.18.2"
, "should": "^3.3.1"
, "supertest": "^0.12.0"
}
, "scripts": {
"test": "./node_modules/mocha/bin/mocha"
, "lint": "./node_modules/lint/bin/node-lint lib test"
],
"bugs": {"url": "https://github.com/troygoode/node-cors/issues"},
"main": "./lib/index.js",
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"vary": "^1"
},
"devDependencies": {
"basic-auth-connect": "^1",
"body-parser": "^1.4.3",
"express": "^4",
"lint": "^1.1.2",
"mocha": "^1.18.2",
"should": "^3.3.1",
"supertest": "^0.12"
},
"scripts": {
"test": "./node_modules/mocha/bin/mocha",
"lint": "./node_modules/lint/bin/node-lint lib test"
}
}
24 changes: 23 additions & 1 deletion test/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
setHeader: function (key, value) {
headers[key] = value;
return;
},
get: function (key) {
return headers[key];
}
};
};
Expand Down Expand Up @@ -208,7 +211,7 @@
cors(options)(req, res, next);
});

it('includes vary origin header for specific origins', function (done) {
it('includes Vary header for specific origins', function (done) {
// arrange
var req, res, next, options;
options = {
Expand All @@ -226,6 +229,25 @@
cors(options)(req, res, next);
});

it('appends to an existing Vary header', function (done) {
// arrange
var req, res, next, options;
options = {
origin: 'example.com'
};
req = fakeRequest();
res = fakeResponse();
res.setHeader('Vary', 'Foo');
next = function () {
// assert
res.getHeader('Vary').should.equal('Foo, 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 165f35e

Please sign in to comment.