Skip to content

Commit

Permalink
feat(http-protocol): Add support for https comms
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 9, 2015
1 parent c0fe70d commit efd4f39
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
17 changes: 13 additions & 4 deletions lib/cli/command.reload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var error = "Could not contact BrowserSync server.";
var error = "Could not contact BrowserSync server.";

/**
* $ browser-sync reload <options>
Expand All @@ -14,16 +14,25 @@ var error = "Could not contact BrowserSync server.";
module.exports = function (opts) {

var flags = opts.cli.flags;
var proto = require("../http-protocol");
if (!flags.url) {
flags.url = "http://localhost:" + (flags.port || 3000);
}
var proto = require("../http-protocol");
var scheme = flags.url.match(/^https/) ? "https" : "http";

var url = proto.getUrl({method: "reload", args: flags.files}, "http://localhost:" + (flags.port || 3000));
var url = proto.getUrl({method: "reload", args: flags.files}, flags.url);

require("http").get(url, function (res) {
require(scheme).get(url, function (res) {
if (res.statusCode !== 200) {
require("logger").logger.error(error);
return opts.cb(new Error(error));
} else {
opts.cb(null, res);
}
}).on("error", function (err) {
if (err.code === "ECONNREFUSED") {
err.message = "BrowserSync not running at " + flags.url;
}
return opts.cb(err);
});
};
3 changes: 2 additions & 1 deletion lib/cli/opts.reload.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"files": "File paths to reload",
"port": "Target a running instance by port number"
"port": "Target a running instance by port number",
"url": "Provide the full the url to the running BrowserSync instance"
}
41 changes: 41 additions & 0 deletions test/specs/commands/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var browserSync = require(path.resolve("./"));

var pkg = require(path.resolve("package.json"));
var sinon = require("sinon");
var assert = require("chai").assert;
var cli = require(path.resolve(pkg.bin));

describe("E2E CLI `reload` with no files arg", function () {
Expand Down Expand Up @@ -59,4 +60,44 @@ describe("E2E CLI `reload` with no files arg", function () {
});
});
});
it("should make a http request with files arg over HTTPS", function (done) {

browserSync.reset();
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;

browserSync
.create()
.init({server: "test/fixtures", open: false, https: true}, function (err, bs) {

var spy = sinon.spy(bs.events, "emit");

cli({
cli: {
input: ["reload"],
flags: {
url: bs.options.getIn(["urls", "local"]),
files: "core.css"
}
},
cb: function () {
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core"});
bs.cleanup();
done();
}
});
});
});
it("should handle ECONNREFUSED errors nicely", function (done) {
cli({
cli: {
input: ["reload"],
flags: {}
},
cb: function (err) {
assert.equal(err.code, "ECONNREFUSED");
assert.equal(err.message, "BrowserSync not running at http://localhost:3000");
done();
}
});
});
});

0 comments on commit efd4f39

Please sign in to comment.