-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http-protocol): Add reload method to http protocol
- Loading branch information
1 parent
ee2ba31
commit f6a3601
Showing
6 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"use strict"; | ||
|
||
var queryString = require("query-string"); | ||
var proto = exports; | ||
|
||
/** | ||
* Use BrowserSync options + querystring to create a | ||
* full HTTP/HTTTPS url. | ||
* | ||
* Eg. http://localhost:3000/__browser_sync__?method=reload | ||
* Eg. http://localhost:3000/__browser_sync__?method=reload&args=core.css | ||
* Eg. http://localhost:3000/__browser_sync__?method=reload&args=core.css&args=core.min | ||
* | ||
* @param args | ||
* @param bs | ||
* @returns {string} | ||
*/ | ||
proto.getUrl = function (args, bs) { | ||
return [ | ||
bs.options.getIn(["urls", "local"]), | ||
bs.options.getIn(["httpProtocol", "path"]), | ||
"?", | ||
queryString.stringify(args) | ||
].join(""); | ||
}; | ||
|
||
/** | ||
* Return a middleware for handling the requests | ||
* @param {BrowserSync} bs | ||
* @returns {Function} | ||
*/ | ||
proto.middleware = function (bs) { | ||
|
||
return function (req, res) { | ||
|
||
var params = queryString.parse(req.url.replace(/^.*\?/, "")); | ||
|
||
if (!params) { | ||
return; | ||
} | ||
|
||
if (require.resolve("./public/" + params.method)) { | ||
|
||
require("./public/" + params.method)(bs.events).apply(null, [params.args]); | ||
|
||
var output = [ | ||
"Called public API method `.%s()`".replace("%s", params.method), | ||
"With args: " + JSON.stringify(params.args) | ||
]; | ||
|
||
res.end(output.join("\n")); | ||
|
||
} else { | ||
res.end("Public API method `" + params.method + "` not found."); | ||
} | ||
|
||
|
||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use strict"; | ||
|
||
var browserSync = require("../../../index"); | ||
var request = require("request"); | ||
var assert = require("chai").assert; | ||
var sinon = require("sinon"); | ||
var proto = require("../../../lib/http-protocol"); | ||
|
||
describe("HTTP protocol", function () { | ||
|
||
var bs, spy; | ||
|
||
before(function (done) { | ||
|
||
browserSync.reset(); | ||
|
||
var config = { | ||
server: "test/fixtures", | ||
logLevel: "info", | ||
open: false, | ||
online: false | ||
}; | ||
|
||
bs = browserSync.init(config, done).instance; | ||
|
||
spy = sinon.spy(bs.events, "emit"); | ||
}); | ||
|
||
afterEach(function () { | ||
spy.reset(); | ||
}); | ||
|
||
after(function () { | ||
bs.cleanup(); | ||
}); | ||
|
||
it("responds to reload event with no args", function (done) { | ||
|
||
var url = proto.getUrl({method: "reload"}, bs); | ||
|
||
request(url, function (e, r, body) { | ||
sinon.assert.calledWith(spy, "browser:reload"); | ||
assert.include(body, "Called public API method `.reload()`"); | ||
assert.include(body, "With args: undefined"); | ||
done(); | ||
}); | ||
}); | ||
it("responds to reload event with multi file paths", function (done) { | ||
|
||
var url = proto.getUrl({method: "reload", args: ["core.min.css", "core.css"]}, bs); | ||
|
||
request(url, function (e, r, body) { | ||
sinon.assert.calledWith(spy, "file:changed"); | ||
sinon.assert.calledWithExactly(spy, "file:changed", { path: "core.min.css", log: true, namespace: "core" }); | ||
assert.include(body, "Called public API method `.reload()`"); | ||
assert.include(body, "With args: [\"core.min.css\",\"core.css\"]"); | ||
done(); | ||
}); | ||
}); | ||
it("responds to reload event with single file path", function (done) { | ||
|
||
var url = proto.getUrl({method: "reload", args: "somefile.php"}, bs); | ||
|
||
request(url, function (e, r, body) { | ||
sinon.assert.calledWith(spy, "file:changed"); | ||
sinon.assert.calledWithExactly(spy, "file:changed", { path: "somefile.php", log: true, namespace: "core" }); | ||
assert.include(body, "Called public API method `.reload()`"); | ||
assert.include(body, "With args: \"somefile.php\""); | ||
done(); | ||
}); | ||
}); | ||
}); |