|
3 | 3 | var browserSync = require("../../../index"); |
4 | 4 |
|
5 | 5 | var request = require("supertest"); |
6 | | -var assert = require("chai").assert; |
7 | 6 | var page = require("fs").readFileSync("test/fixtures/index.html", "utf-8"); |
| 7 | +var css = require("fs").readFileSync("test/fixtures/assets/style.css", "utf-8"); |
| 8 | +var Rx = require('rx'); |
8 | 9 |
|
9 | | -describe("E2E `serveStatic` option", function () { |
| 10 | +function getRequests (reqs, server) { |
| 11 | + return reqs.map(function (req) { |
| 12 | + return Rx.Observable.create(function (obs) { |
| 13 | + request(server) |
| 14 | + .get(req[0]) |
| 15 | + .end(function (err, res) { |
| 16 | + if (err) { |
| 17 | + return obs.onError(err); |
| 18 | + } |
| 19 | + if (res.text === req[1]) { |
| 20 | + obs.onNext(true); |
| 21 | + obs.onCompleted(); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + obs.onError(new Error(req[0] + " did not match the response body")); |
| 26 | + }); |
| 27 | + }); |
| 28 | + }); |
| 29 | +} |
10 | 30 |
|
11 | | - var bs; |
| 31 | +describe("E2E `serveStatic` option", function () { |
12 | 32 |
|
13 | | - before(function (done) { |
| 33 | + it("can serve static files with string only", function (done) { |
14 | 34 | browserSync.reset(); |
15 | 35 | var config = { |
16 | 36 | logLevel: "silent", |
17 | 37 | online: false, |
18 | 38 | serveStatic: ["test/fixtures"] |
19 | 39 | }; |
20 | | - bs = browserSync(config, done).instance; |
21 | | - }); |
22 | 40 |
|
23 | | - after(function () { |
24 | | - bs.cleanup(); |
| 41 | + browserSync(config, function (err, bs) { |
| 42 | + request(bs.server) |
| 43 | + .get("/index.html") |
| 44 | + .expect(200, page, function () { |
| 45 | + bs.cleanup(); |
| 46 | + done(); |
| 47 | + }); |
| 48 | + }); |
25 | 49 | }); |
26 | | - |
27 | | - it("can serve static files regardless of running mode", function (done) { |
28 | | - request(bs.server) |
29 | | - .get("/index.html") |
30 | | - .expect(200) |
31 | | - .end(function (err, res) { |
32 | | - assert.equal(res.text, page); |
33 | | - done(); |
34 | | - }); |
| 50 | + it("can serve static files with string multiple string", function (done) { |
| 51 | + browserSync.reset(); |
| 52 | + var config = { |
| 53 | + logLevel: "silent", |
| 54 | + online: false, |
| 55 | + serveStatic: ["test/fixtures", "test/fixtures/assets"] |
| 56 | + }; |
| 57 | + browserSync(config, function (err, bs) { |
| 58 | + var reqs = getRequests([["/index.html", page], ["/style.css", css]], bs.server); |
| 59 | + var obs = Rx.Observable.merge(reqs); |
| 60 | + obs.subscribeOnCompleted(done); |
| 61 | + }); |
| 62 | + }); |
| 63 | + it("can serve static files with multiple objects", function (done) { |
| 64 | + browserSync.reset(); |
| 65 | + var config = { |
| 66 | + logLevel: "silent", |
| 67 | + online: false, |
| 68 | + serveStatic: [ |
| 69 | + {route: "", dir: "test/fixtures"}, |
| 70 | + {route: [""], dir: "test/fixtures/assets"} |
| 71 | + ] |
| 72 | + }; |
| 73 | + browserSync(config, function (err, bs) { |
| 74 | + var reqs = getRequests([['/index.html', page], ['/style.css', css]], bs.server); |
| 75 | + var obs = Rx.Observable.merge(reqs); |
| 76 | + obs.subscribeOnCompleted(done); |
| 77 | + }); |
| 78 | + }); |
| 79 | + it("can serve static files with multiple roots", function (done) { |
| 80 | + browserSync.reset(); |
| 81 | + var config = { |
| 82 | + logLevel: "silent", |
| 83 | + online: false, |
| 84 | + serveStatic: [ |
| 85 | + {route: "", dir: "test/fixtures"}, |
| 86 | + {route: ["/shane", "/kittie"], dir: "test/fixtures/assets"}, |
| 87 | + {route: "", dir: "test/fixtures/assets"} |
| 88 | + ] |
| 89 | + }; |
| 90 | + browserSync(config, function (err, bs) { |
| 91 | + var reqs = getRequests([ |
| 92 | + ['/index.html', page], |
| 93 | + ['/shane/style.css', css], |
| 94 | + ['/kittie/style.css', css] |
| 95 | + ], bs.server); |
| 96 | + var obs = Rx.Observable.merge(reqs); |
| 97 | + obs.subscribeOnCompleted(done); |
| 98 | + }); |
35 | 99 | }); |
36 | 100 | }); |
0 commit comments