diff --git a/lib/api.js b/lib/api.js index b3901d6..54bc14e 100644 --- a/lib/api.js +++ b/lib/api.js @@ -32,8 +32,8 @@ namespace("Cylon", function() { master = opts.master; var options = { - cert: fs.readFileSync(__dirname + "/ssl/server.crt"), - key: fs.readFileSync(__dirname + "/ssl/server.key") + cert: fs.readFileSync(opts.cert || __dirname + "/ssl/server.crt"), + key: fs.readFileSync(opts.key || __dirname + "/ssl/server.key") } this.server = express().https(options).io(); diff --git a/lib/cylon.js b/lib/cylon.js index 5da8993..7bd6178 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -41,9 +41,15 @@ var Cylon = (function() { this.self = this; this.api_instance = null; - this.api_config = { host: '127.0.0.1', port: '3000' }; this.robots = []; + this.api_config = { + host: '127.0.0.1', + port: '3000', + cert: null, + key: null + }; + this.robot = bind(this.robot, this); if (process.platform === "win32") { @@ -94,9 +100,16 @@ var Cylon = (function() { if (opts == null) { opts = {}; } var host = opts.host || this.api_config.host, - port = opts.port || this.api_config.port; + port = opts.port || this.api_config.port, + cert = opts.cert || this.api_config.cert, + key = opts.key || this.api_config.key; - this.api_config = { host: host, port: port }; + this.api_config = { + host: host, + port: port, + cert: cert, + key: key + }; return this.api_config; }; diff --git a/test/specs/cylon.spec.js b/test/specs/cylon.spec.js index 58c999b..2ae524f 100644 --- a/test/specs/cylon.spec.js +++ b/test/specs/cylon.spec.js @@ -40,41 +40,68 @@ describe("Cylon", function() { }); describe("#api", function() { - afterEach(function() { - cylon.api_config = { host: "127.0.0.1", port: "3000" }; - }); + var expectedConfig; + + beforeEach(function() { + expectedConfig = { + host: '127.0.0.1', + port: '3000', + cert: null, + key: null + }; + + // this is the shortest, cheapest way to dup an object in JS. + // I don't like it either. + cylon.api_config = JSON.parse(JSON.stringify(expectedConfig)); + }) context("without arguments", function() { it("returns the current API configuration", function() { cylon.api(); - var config = cylon.api_config; - expect(config).to.be.eql({ host: "127.0.0.1", port: "3000" }); + expect(cylon.api_config).to.be.eql(expectedConfig); }); }); context("only specifying port", function() { it("changes the port, but not the host", function() { + expectedConfig.port = "4000"; + cylon.api({ port: "4000" }); - var config = cylon.api_config; - expect(config).to.be.eql({ host: "127.0.0.1", port: "4000" }); + + expect(cylon.api_config).to.be.eql(expectedConfig); }); }); context("only specifying host", function() { it("changes the host, but not the port", function() { + expectedConfig.host = "0.0.0.0"; cylon.api({ host: "0.0.0.0" }); - var config = cylon.api_config; - expect(config).to.be.eql({ host: "0.0.0.0", port: "3000" }); + + expect(cylon.api_config).to.be.eql(expectedConfig); }); }); context("specifying new host and port", function() { it("changes both the host and port", function() { + expectedConfig.host = "0.0.0.0"; + expectedConfig.port = "4000"; + cylon.api({ host: "0.0.0.0", port: "4000" }); - var config = cylon.api_config; - expect(config).to.be.eql({ host: "0.0.0.0", port: "4000" }); + + expect(cylon.api_config).to.be.eql(expectedConfig); }); }); + + context("specifiying new SSL key and cert", function() { + it("changes the SSL key and cert", function() { + expectedConfig.cert = "/path/to/cert/file"; + expectedConfig.key = "/path/to/key/file"; + + cylon.api({ cert: "/path/to/cert/file", key: "/path/to/key/file" }); + + expect(cylon.api_config).to.be.eql(expectedConfig); + }) + }); }); describe("#findRobot", function() {