Skip to content

Commit 24a25c6

Browse files
committed
Tests & style changes for #1227
1 parent 0ae759a commit 24a25c6

3 files changed

Lines changed: 66 additions & 25 deletions

File tree

lib/browser-sync.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ BrowserSync.prototype.addMiddleware = function (route, handle, opts) {
424424
}
425425

426426
bs.options = bs.options.update("middleware", function (mw) {
427-
return (bs.options.get("mode") === "proxy")? mw.insert(mw.size-1, entry) : mw.concat(entry);
427+
if (bs.options.get("mode") === "proxy") {
428+
return mw.insert(mw.size-1, entry);
429+
}
430+
return mw.concat(entry);
428431
});
429432

430433
bs.resetMiddlewareStack();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"lint": "eslint index.js lib bin examples test/specs gulpfile.js",
2525
"pro": "protractor test/protractor/config.single.js",
2626
"pro-local": "node test/protractor/setup.js",
27-
"unit": "mocha --recursive test/specs --timeout 10000",
27+
"unit": "mocha --recursive test/specs --timeout 10000 --bail",
2828
"cover": "npm run env && npm run cover-local && npm run coveralls",
2929
"cover-local": "istanbul cover -x lodash.custom.js _mocha -- --timeout 10000 --recursive ./test/specs",
3030
"coveralls": "istanbul-coveralls",

test/specs/e2e/middleware/middleware.proxy.option.js

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ var request = require("supertest");
99

1010
describe("Accepting single middleware as a proxy option", function () {
1111

12-
var bs, spy, server;
12+
it("should call the middleware", function (done) {
1313

14-
before(function (done) {
14+
var path = "/forms.html";
1515

1616
browserSync.reset();
1717

1818
var app = connect();
1919
app.use(require("serve-static")("./test/fixtures"));
2020

21-
server = app.listen();
22-
23-
spy = sinon.spy();
21+
var server = app.listen();
22+
var spy = sinon.spy();
2423

2524
var fn = function (req, res, next) {
2625
spy(req.url);
@@ -36,28 +35,67 @@ describe("Accepting single middleware as a proxy option", function () {
3635
open: false
3736
};
3837

39-
bs = browserSync.init(config, done).instance;
40-
});
38+
browserSync.init(config, function (err, bs) {
4139

42-
after(function () {
43-
bs.cleanup();
44-
server.close();
45-
});
40+
assert.equal(bs.options.get("middleware").size, 2);
4641

47-
it("serves files from the middleware with snippet added", function () {
48-
assert.equal(bs.options.get("middleware").size, 2);
42+
request(bs.server)
43+
.get(path)
44+
.set("accept", "text/html")
45+
.expect(200)
46+
.end(function (err, res) {
47+
48+
sinon.assert.calledWithExactly(spy, path);
49+
assert.include(res.text, bs.options.get("snippet"));
50+
51+
bs.cleanup(function () {
52+
server.close();
53+
done();
54+
});
55+
});
56+
});
4957
});
50-
it("should call the middlewares", function (done) {
58+
it("can add middleware at run-time", function (done) {
59+
5160
var path = "/forms.html";
52-
request(bs.server)
53-
.get(path)
54-
.set("accept", "text/html")
55-
.expect(200)
56-
.end(function (err, res) {
57-
sinon.assert.calledWithExactly(spy, path);
58-
assert.include(res.text, bs.options.get("snippet"));
59-
done();
60-
});
61+
62+
browserSync.reset();
63+
64+
var app = connect();
65+
app.use(require("serve-static")("./test/fixtures"));
66+
67+
var server = app.listen();
68+
var spy = sinon.spy();
69+
70+
var fn = function (req, res, next) {
71+
spy(req.url);
72+
next();
73+
};
74+
75+
var config = {
76+
proxy: "http://localhost:" + server.address().port,
77+
logLevel: "silent",
78+
open: false
79+
};
80+
81+
browserSync.init(config, function (err, bs) {
82+
83+
bs.addMiddleware("*", fn);
84+
85+
request(bs.server)
86+
.get(path)
87+
.set("accept", "text/html")
88+
.expect(200)
89+
.end(function () {
90+
91+
sinon.assert.calledWithExactly(spy, path);
92+
93+
bs.cleanup(function () {
94+
server.close();
95+
done();
96+
});
97+
});
98+
});
6199
});
62100
});
63101

0 commit comments

Comments
 (0)