Skip to content

Commit

Permalink
Use hook-merged middleware for server
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Feb 16, 2015
1 parent 09dbca6 commit 51cd57a
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 27 deletions.
26 changes: 8 additions & 18 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,14 @@ module.exports = {
*/
mergeMiddlewares: function (bs, done) {

var server = bs.options.get("server");

if (server) {

return done(null, {
optionsIn: [
{
path: ["server", "middleware"],
value: bs.pluginManager.hook(
"server:middleware",
server.get("middleware") || bs.options.getIn(["proxy", "middleware"]) || null
)
}
]
});
}

return done();
return done(null, {
options: {
middleware: bs.pluginManager.hook(
"server:middleware",
bs.options.get("middleware")
)
}
});
},
/**
* @param {BrowserSync} bs
Expand Down
5 changes: 2 additions & 3 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ module.exports = {
var result = hook(this);

if (Array.isArray(result)) {

result.forEach(function (res) {
if (_.isFunction(res)) {
initial.push(res);
initial = initial.push(res);
}
});

} else {
if (_.isFunction(result)) {
initial.push(result);
initial = initial.push(result);
}
}
}, this);
Expand Down
2 changes: 1 addition & 1 deletion lib/server/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function createServer (bs, scripts) {

var options = bs.options;
var server = options.get("server");
var middleware = server.get("middleware");
var middleware = options.get("middleware");
var index = server.get("index") || "index.html";
var basedir = server.get("baseDir");

Expand Down
173 changes: 168 additions & 5 deletions test/specs/e2e/middleware/middleware.server.option.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
var browserSync = require("../../../../index");

var assert = require("chai").assert;
var sinon = require("sinon");
var request = require("supertest");

describe("Accepting middleware as a server option", function () {
describe("Accepting single middleware as a server option", function () {

var bs;
var bs, spy;

before(function (done) {

browserSync.reset();
spy = sinon.spy();

var fn = function (req) {
console.log(req.url);
var fn = function (req, res, next) {
spy(req.url);
next();
};

var config = {
server: {
baseDir: "./app",
baseDir: "./test/fixtures",
middleware: fn // Back compat
},
logLevel: "silent",
Expand All @@ -35,4 +39,163 @@ describe("Accepting middleware as a server option", function () {
it("serves files from the middleware with snippet added", function () {
assert.equal(bs.options.get("middleware").size, 1);
});
it("should call the middlewares", function (done) {
request(bs.server)
.get("/")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, "/");
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

describe("Accepting single middleware as a top-level option", function () {

var bs, spy;

before(function (done) {

browserSync.reset();
spy = sinon.spy();

var fn = function (req, res, next) {
spy(req.url);
next();
};

var config = {
server: {
baseDir: "./test/fixtures"
},
middleware: fn,
logLevel: "silent",
open: false
};

bs = browserSync.init(config, done).instance;
});

after(function () {
bs.cleanup();
});

it("serves files from the middleware with snippet added", function () {
assert.equal(bs.options.get("middleware").size, 1);
});
it("should call the middlewares", function (done) {
request(bs.server)
.get("/")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, "/");
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

describe("Accepting multiple middleware as a server option", function () {

var bs, spy, spy2;

before(function (done) {

browserSync.reset();
spy = sinon.spy();
spy2 = sinon.spy();

var fn = function (req, res, next) {
spy(req.url);
next();
};
var fn2 = function (req, res, next) {
spy2(req.url);
next();
};

var config = {
server: {
baseDir: "./test/fixtures",
middleware: [fn, fn2] // Back compat
},
logLevel: "silent",
open: false
};

bs = browserSync.init(config, done).instance;
});

after(function () {
bs.cleanup();
});

it("serves files from the middleware with snippet added", function () {
assert.equal(bs.options.get("middleware").size, 2);
});
it("should call the middlewares", function (done) {
request(bs.server)
.get("/")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, "/");
sinon.assert.calledWithExactly(spy2, "/");
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

describe("Accepting multiple server middlewares as top-level option", function () {

var bs, spy, spy2;

before(function (done) {

browserSync.reset();
spy = sinon.spy();
spy2 = sinon.spy();

var fn = function (req, res, next) {
spy(req.url);
next();
};
var fn2 = function (req, res, next) {
spy2(req.url);
next();
};

var config = {
server: "test/fixtures",
logLevel: "silent",
open: false,
middleware: [fn, fn2]
};

bs = browserSync.init(config, done).instance;
});

after(function () {
bs.cleanup();
});

it("serves files from the middleware with snippet added", function () {
assert.equal(bs.options.get("middleware").size, 2);
});
it("should call the middlewares", function (done) {
request(bs.server)
.get("/")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, "/");
sinon.assert.calledWithExactly(spy2, "/");
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

0 comments on commit 51cd57a

Please sign in to comment.