Skip to content

Commit

Permalink
feat(httpModule): Allow custom HTTP module to be given in options
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Nov 16, 2016
1 parent 5136fd5 commit 8ba178e
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 108 deletions.
3 changes: 1 addition & 2 deletions examples/server.http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

"use strict";

var http2 = require("http2");
var browserSync = require("browser-sync").create();

browserSync.init({
Expand All @@ -21,5 +20,5 @@ browserSync.init({
baseDir: "app"
},
https: true,
httpModule: http2
httpModule: "http2"
});
2 changes: 1 addition & 1 deletion lib/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ module.exports = {
/**
* Override http module to allow using 3rd party server modules (such as http2)
* @property httpModule
* @type Object|Function
* @type string
* @default undefined
* @since 2.17.6
*/
Expand Down
10 changes: 0 additions & 10 deletions lib/public/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ module.exports = function (browserSync, name, pjson) {

args.config.version = pjson.version;

/**
* Preserve the httpModule property's functions.
* the http2 module exports an object of functions and the merge function seems
* to want to destroy that, but if the base object is a function it seems fine
* TODO: find a better or more generic way to handle this
*/
if(args.config.httpModule && !_.isFunction(args.config.httpModule)) {
args.config.httpModule = Object.assign(function() {}, args.config.httpModule);
}

return browserSync.init(merge(args.config), args.cb);
};
};
25 changes: 24 additions & 1 deletion lib/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,42 @@ var serverUtils = {
getServer: function (app, options) {
return {
server: (function () {
var httpModule = options.get("httpModule") || (options.get("scheme") === "https" ? https : http);

var httpModule = serverUtils.getHttpModule(options);

if (options.get("scheme") === "https") {
var pfxPath = options.getIn(["https", "pfx"]);
return pfxPath ?
httpModule.createServer(serverUtils.getPFX(pfxPath), app) :
httpModule.createServer(serverUtils.getKeyAndCert(options), app);
}

return httpModule.createServer(app);
})(),
app: app
};
},
getHttpModule: function (options) {
/**
* Users may provide a string to be used by nodes
* require lookup.
*/
var httpModule = options.get("httpModule");

if (typeof httpModule === "string") {
/**
* Note, this could throw, but let that happen as
* the error message good enough.
*/
return require(httpModule);
}

if (options.get("scheme") === "https") {
return https;
}

return http;
},
getMiddlewares: function (bs) {

var clientJs = bs.pluginManager.hook("client:js", {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"scripts": {
"env": "node ./test/env.js",
"test": "npm run lint && npm run env && npm run unit",
"lint": "eslint index.js lib bin examples test/specs gulpfile.js",
"lint": "eslint index.js lib bin examples test/specs gulpfile.js --fix",
"pro": "protractor test/protractor/config.single.js",
"pro-local": "node test/protractor/setup.js",
"unit": "mocha --recursive test/specs --timeout 10000 --bail",
Expand Down Expand Up @@ -73,6 +73,7 @@
"gulp-contribs": "0.0.3",
"gulp-conventional-changelog": "1.1.0",
"gulp-filter": "4.0.0",
"http2": "^3.3.6",
"istanbul": "0.4.5",
"istanbul-coveralls": "1.0.3",
"lodash-cli": "4.15.0",
Expand Down
2 changes: 1 addition & 1 deletion test/specs/e2e/middleware/middleware.option.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("Accepting middleware as a plain object", function () {
};

browserSync.init(config, function (err, bs) {
request(bs.server)
request(bs.options.getIn(["urls", "local"]))
.get("/")
.set("accept", "text/html")
.expect(200)
Expand Down
110 changes: 25 additions & 85 deletions test/specs/e2e/server/e2e.server.httpModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,34 @@
var browserSync = require("../../../../index");

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

describe("E2E httpModule options test", function () {

this.timeout(15000);

var bs;
var mockHttpModule, mockHttpServer, createServerSpy, https;

describe("httpModule undefined", function () {

before(function (done) {

browserSync.reset();
https = require("https");

createServerSpy = sinon.spy(https, "createServer");

var config = {
server: {
baseDir: "test/fixtures",
},
https: true,
open: false,
logLevel: "silent"
};

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

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

it("creates server using the default https module", function () {
sinon.assert.calledOnce(createServerSpy);
});

it("should be using the server from the https module", function () {
assert.equal(bs.server instanceof https.Server, true);
});
});

describe("httpModule defined", function () {

before(function (done) {

browserSync.reset();

mockHttpServer = {
on: function() { },
listen: function() { },
listeners: function() { return []; },
removeAllListeners: function() { },
close: function() { }
};

mockHttpModule = {
createServer: function() {
return mockHttpServer;
}
};

createServerSpy = sinon.spy(mockHttpModule, "createServer");

var config = {
server: {
baseDir: "test/fixtures",
},
https: true,
httpModule: mockHttpModule,
open: false,
logLevel: "silent"
};

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

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

it("creates server using provided httpModule", function () {
sinon.assert.calledOnce(createServerSpy);
});

it("should be using the server created by the provided httpModule", function () {
assert.equal(bs.server, mockHttpServer);
it("creates server using provided httpModule", function (done) {

browserSync.reset();

var config = {
server: {
baseDir: "test/fixtures"
},
https: true,
httpModule: "http2",
open: false,
logLevel: "silent"
};

browserSync.init(config, function (err, bs) {
request(bs.options.getIn(["urls", "local"]))
.get("/index.html")
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
assert.include(res.text, bs.options.get("snippet"));
bs.cleanup();
done();
});
});
});
});
4 changes: 2 additions & 2 deletions test/specs/e2e/server/e2e.server.secure.custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("E2E TLS server with custom certs test", function () {
});

it("serves files with the snippet added", function (done) {
request(instance.options.getIn(['urls', 'local']))
request(instance.options.getIn(["urls", "local"]))
.get("/index.html")
.set("accept", "text/html")
.expect(200)
Expand All @@ -53,7 +53,7 @@ describe("E2E TLS server with custom certs test", function () {

it("serves the client script", function (done) {

request(instance.options.getIn(['urls', 'local']))
request(instance.options.getIn(["urls", "local"]))
.get(instance.options.getIn(["scriptPaths", "versioned"]))
.expect(200)
.end(function (err, res) {
Expand Down
6 changes: 3 additions & 3 deletions test/specs/e2e/server/e2e.server.secure.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("E2E TLS server options test", function () {

assert.isString(bs.options.get("snippet"));

request(bs.options.getIn(['urls', 'local']))
request(bs.options.getIn(["urls", "local"]))
.get("/index.html")
.set("accept", "text/html")
.expect(200)
Expand Down Expand Up @@ -82,7 +82,7 @@ describe("E2E TLS server test (1)", function () {

assert.isString(bs.options.get("snippet"));

request(bs.options.getIn(['urls', 'local']))
request(bs.options.getIn(["urls", "local"]))
.get("/index.html")
.set("accept", "text/html")
.expect(200)
Expand All @@ -94,7 +94,7 @@ describe("E2E TLS server test (1)", function () {

it("serves the client script", function (done) {

request(bs.options.getIn(['urls', 'local']))
request(bs.options.getIn(["urls", "local"]))
.get(bs.options.getIn(["scriptPaths", "versioned"]))
.expect(200)
.end(function (err, res) {
Expand Down
4 changes: 2 additions & 2 deletions test/specs/e2e/server/e2e.server.secure.pfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("E2E TLS server with PFX certs test", function () {

assert.isString(instance.options.get("snippet"));

request(instance.options.getIn(['urls', 'local']))
request(instance.options.getIn(["urls", "local"]))
.get("/index.html")
.set("accept", "text/html")
.expect(200)
Expand All @@ -54,7 +54,7 @@ describe("E2E TLS server with PFX certs test", function () {

it("serves the client script", function (done) {

request(instance.options.getIn(['urls', 'local']))
request(instance.options.getIn(["urls", "local"]))
.get(instance.options.getIn(["scriptPaths", "versioned"]))
.expect(200)
.end(function (err, res) {
Expand Down

0 comments on commit 8ba178e

Please sign in to comment.