Skip to content

Commit

Permalink
test(proxy): Add proxy + Middleware specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Osbourne committed Feb 16, 2015
1 parent 51cd57a commit 8e60723
Showing 1 changed file with 180 additions and 18 deletions.
198 changes: 180 additions & 18 deletions test/specs/e2e/middleware/middleware.proxy.option.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
"use strict";

var browserSync = require("../../../../index");

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

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

var bs;
var bs, spy, server;

before(function (done) {

browserSync.reset();

var fn = function (req) {
console.log(req.url);
var app = connect();
app.use(require("serve-static")("./test/fixtures"));

server = app.listen();

spy = sinon.spy();

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

var config = {
server: {
baseDir: "test/fixtures",
proxy: {
target: "http://localhost:" + server.address().port,
middleware: fn // Back compat
},
logLevel: "silent",
Expand All @@ -29,34 +41,51 @@ describe("Accepting middleware as a server option", function () {

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

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) {
var path = "/forms.html";
request(bs.server)
.get(path)
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, path);
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

describe("Ignoring middleware as a server option when given at top level", function () {
describe("Accepting single middleware as a top-level proxy option", function () {

var bs;
var bs, spy, server;

before(function (done) {

browserSync.reset();

var fn = function fn (req) {
console.log(req.url);
};
var fn2 = function fn2 (req) {
console.log(req.url);
var app = connect();
app.use(require("serve-static")("./test/fixtures"));

server = app.listen();

spy = sinon.spy();

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

var config = {
server: {
baseDir: "test/fixtures",
middleware: fn // Back compat
proxy: {
target: "http://localhost:" + server.address().port
},
middleware: fn2,
middleware: fn, // Back compat
logLevel: "silent",
open: false
};
Expand All @@ -66,10 +95,143 @@ describe("Ignoring middleware as a server option when given at top level", funct

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

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

describe("Accepting multiple middlewares as a proxy option", function () {

var bs, spy, spy2, server;

before(function (done) {

browserSync.reset();

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

server = app.listen();

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 = {
proxy: {
target: "http://localhost:" + server.address().port,
middleware: [fn, fn2] // Back compat
},
logLevel: "silent",
open: false
};

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

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

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) {
var path = "/forms.html";
request(bs.server)
.get(path)
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, path);
sinon.assert.calledWithExactly(spy2, path);
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

describe("Accepting multiple middlewares as a proxy option", function () {

var bs, spy, spy2, server;

before(function (done) {

browserSync.reset();

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

server = app.listen();

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 = {
proxy: {
target: "http://localhost:" + server.address().port,

},
middleware: [fn, fn2], // Back compat
logLevel: "silent",
open: false
};

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

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

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) {
var path = "/forms.html";
request(bs.server)
.get(path)
.set("accept", "text/html")
.expect(200)
.end(function (err, res) {
sinon.assert.calledWithExactly(spy, path);
sinon.assert.calledWithExactly(spy2, path);
assert.include(res.text, bs.options.get("snippet"));
done();
});
});
});

0 comments on commit 8e60723

Please sign in to comment.