From 104dbb4a89a47f688244a01321f2a6ee55237582 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Tue, 9 Jun 2015 09:34:34 +0100 Subject: [PATCH] fix(plugins): Allow plugins to register middleware via server:middleware hook when in proxy mode - fixes #663 --- lib/server/proxy-server.js | 4 +- test/specs/plugins/user.plugins.proxy.js | 60 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/specs/plugins/user.plugins.proxy.js diff --git a/lib/server/proxy-server.js b/lib/server/proxy-server.js index eacefef3c..5ad0661ef 100644 --- a/lib/server/proxy-server.js +++ b/lib/server/proxy-server.js @@ -80,7 +80,9 @@ function getOptions (bs, scripts) { * @returns {*} */ function getPluginMiddleware (bs, scripts) { - return bs.pluginManager.hook("server:middleware", snippetUtils.getProxyMiddleware(scripts, bs.options.getIn(["scriptPaths", "versioned"]))); + return bs.pluginManager.hook("server:middleware", [ + snippetUtils.getProxyMiddleware(scripts, bs.options.getIn(["scriptPaths", "versioned"])) + ]); } /** diff --git a/test/specs/plugins/user.plugins.proxy.js b/test/specs/plugins/user.plugins.proxy.js new file mode 100644 index 000000000..aa1697a08 --- /dev/null +++ b/test/specs/plugins/user.plugins.proxy.js @@ -0,0 +1,60 @@ +"use strict"; + +var browserSync = require("../../../"); +var testUtils = require("../../protractor/utils"); +var Immutable = require("immutable"); +var sinon = require("sinon"); +var request = require("supertest"); + +describe("Plugins: Should be able to register middleware when in proxy mode", function () { + + var bs; + var app; + var spy; + + before(function (done) { + + browserSync.reset(); + + app = testUtils.getApp(Immutable.Map({scheme: "http"})); + app.server.listen(); + + spy = sinon.spy(); + + var config = { + proxy: "http://localhost:" + app.server.address().port, + open: false, + logLevel: "silent" + }; + + browserSync.use({ + "plugin": function () { + /* noop */ + }, + "hooks": { + "server:middleware": function () { + return function (req, res, next) { + spy(); + next(); + }; + } + }, + "plugin:name": "KITTENZ" + }); + + bs = browserSync(config, done).instance; + }); + + after(function () { + bs.cleanup(); + }); + it("should serve the file", function (done) { + request(bs.server) + .get("/") + .set("accept", "text/html") + .end(function () { + sinon.assert.calledOnce(spy); + done(); + }); + }); +});