Skip to content

Commit 8d55e29

Browse files
authored
Merge pull request #1 from elastic-coders/cors
Cors
2 parents b34f4a6 + 2218def commit 8d55e29

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ module.exports = function(options) {
9393
callbacks.push(executeLambdaCallback(func));
9494
// Setup endpoint
9595
if (e.cors) {
96-
httpConfig.eventHandler('options', path, [decorateAddCORSCallback(), send200]);
96+
httpConfig.eventHandler('options', path, [decorateAddCORSCallback(e.cors), send200]);
97+
callbacks.unshift(decorateAddCORSCallback(e.cors));
9798
}
9899
httpConfig.eventHandler(method, path, callbacks);
99100
}

lib/decorators-callbacks.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ module.exports.decorateLambdaReqCallback = function(getPrincipalId) {
1818
};
1919
};
2020

21-
module.exports.decorateAddCORSCallback = function() {
21+
const defaultCors = {
22+
origins: ['*'],
23+
methods: ['GET', 'PUT', 'HEAD', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
24+
headers: ['Authorization', 'Content-Type', 'x-amz-date', 'x-amz-security-token']
25+
};
26+
27+
module.exports.decorateAddCORSCallback = function(corsOpts) {
28+
let cors = defaultCors;
29+
if(corsOpts instanceof Object) {
30+
cors = Object.assign({}, defaultCors, corsOpts);
31+
}
2232
return function(req, res, next) {
23-
res.header('Access-Control-Allow-Origin', '*');
24-
res.header('Access-Control-Allow-Methods', 'GET,PUT,HEAD,PATCH,POST,DELETE,OPTIONS');
25-
res.header('Access-Control-Allow-Headers', 'Authorization,Content-Type,x-amz-date,x-amz-security-token');
33+
res.header('Access-Control-Allow-Methods', cors.methods.join(','));
34+
res.header('Access-Control-Allow-Headers', cors.headers.join(','));
35+
res.header('Access-Control-Allow-Origin', cors.origins.join(','));
2636
next();
2737
};
2838
};

tests/decorators-callbacks.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ describe('decorators-callbacks', () => {
5959
expect(subject.decorateAddCORSCallback()).to.be.a('function');
6060
});
6161

62-
it('should add CORS headers', () => {
63-
const cb = subject.decorateAddCORSCallback();
62+
it('should add default CORS headers', () => {
63+
const cb = subject.decorateAddCORSCallback(true);
6464
const req = {};
6565
const res = {
6666
header: sinon.spy(),
@@ -72,5 +72,23 @@ describe('decorators-callbacks', () => {
7272
expect(res.header).to.have.been.calledWith('Access-Control-Allow-Headers', 'Authorization,Content-Type,x-amz-date,x-amz-security-token');
7373
expect(next).to.have.callCount(1);
7474
});
75+
76+
it('should add custom CORS headers', () => {
77+
const cb = subject.decorateAddCORSCallback({
78+
origins: ['https://myapp.test.com'],
79+
methods: ['GET', 'POST']
80+
});
81+
const req = {};
82+
const res = {
83+
header: sinon.spy(),
84+
};
85+
const next = sinon.spy();
86+
cb(req, res, next);
87+
expect(res.header).to.have.been.calledWith('Access-Control-Allow-Origin', 'https://myapp.test.com');
88+
expect(res.header).to.have.been.calledWith('Access-Control-Allow-Methods', 'GET,POST');
89+
expect(res.header).to.have.been.calledWith('Access-Control-Allow-Headers', 'Authorization,Content-Type,x-amz-date,x-amz-security-token');
90+
expect(next).to.have.callCount(1);
91+
});
92+
7593
});
7694
});

0 commit comments

Comments
 (0)