Add CORS param for API config

This commit is contained in:
Andrew Stewart 2014-04-08 10:58:58 -07:00
parent 430ffafe81
commit 4b409f7777
3 changed files with 22 additions and 3 deletions

View File

@ -54,6 +54,11 @@ namespace("Cylon", function() {
this.server.use(express.urlencoded());
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
this.server.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", opts.CORS || "*");
next();
});
this.server.get("/*", function(req, res, next) {
res.set('Content-Type', 'application/json');
return next();

View File

@ -48,7 +48,8 @@ var Cylon = (function() {
port: '3000',
cert: null,
key: null,
auth: {}
auth: {},
CORS: null
};
this.robot = bind(this.robot, this);
@ -104,6 +105,7 @@ var Cylon = (function() {
port = opts.port || this.api_config.port,
auth = opts.auth || this.api_config.auth,
cert = opts.cert || this.api_config.cert,
cors = opts.CORS || this.api_config.CORS,
key = opts.key || this.api_config.key;
this.api_config = {
@ -111,7 +113,8 @@ var Cylon = (function() {
port: port,
cert: cert,
key: key,
auth: auth
auth: auth,
CORS: cors
};
return this.api_config;

View File

@ -48,7 +48,8 @@ describe("Cylon", function() {
port: '3000',
cert: null,
key: null,
auth: {}
auth: {},
CORS: null
};
// this is the shortest, cheapest way to dup an object in JS.
@ -113,6 +114,16 @@ describe("Cylon", function() {
expect(cylon.api_config).to.be.eql(expectedConfig);
});
});
context("specifying CORS restrictions", function() {
it("changes the CORS restrictions", function() {
var CORS = "https://localhost:4000";
expectedConfig.CORS = CORS;
cylon.api({ CORS: CORS })
expect(cylon.api_config).to.be.eql(expectedConfig);
});
});
});
describe("#findRobot", function() {