|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var browserSync = require("../../../../index"); |
| 4 | +var testUtils = require("../../../protractor/utils"); |
| 5 | +var Immutable = require("immutable"); |
| 6 | +var request = require("supertest"); |
| 7 | +var assert = require("chai").assert; |
| 8 | +var foxyPath = require.resolve("foxy"); |
| 9 | +var foxy = require(foxyPath); // jshint ignore:line |
| 10 | + |
| 11 | +describe("E2E proxy test with `proxyOptions`", function () { |
| 12 | + |
| 13 | + this.timeout(15000); |
| 14 | + |
| 15 | + var bs, app, spy; |
| 16 | + |
| 17 | + before(function (done) { |
| 18 | + |
| 19 | + browserSync.reset(); |
| 20 | + |
| 21 | + app = testUtils.getApp(Immutable.Map({scheme: "https"})); |
| 22 | + |
| 23 | + app.server.listen(); |
| 24 | + |
| 25 | + var config = { |
| 26 | + proxy: { |
| 27 | + target: "https://localhost:" + app.server.address().port, |
| 28 | + proxyOptions: { |
| 29 | + xfwd: true |
| 30 | + } |
| 31 | + }, |
| 32 | + open: false, |
| 33 | + logLevel: "silent" |
| 34 | + }; |
| 35 | + |
| 36 | + spy = require("sinon").spy(require.cache[foxyPath], "exports"); |
| 37 | + bs = browserSync.init(config, done).instance; |
| 38 | + }); |
| 39 | + |
| 40 | + after(function () { |
| 41 | + bs.cleanup(); |
| 42 | + app.server.close(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("sets options for node-http-proxy", function (done) { |
| 46 | + |
| 47 | + assert.isTrue(spy.getCall(0).args[1].proxyOptions.xfwd); // check fn passed to foxy |
| 48 | + |
| 49 | + spy.restore(); |
| 50 | + |
| 51 | + var expected = app.html.replace("BS", bs.options.get("snippet") + "BS"); |
| 52 | + var headers; |
| 53 | + app.app.stack.unshift({ |
| 54 | + route: "/index.html", |
| 55 | + handle: function (req, res, next) { |
| 56 | + headers = req.headers; |
| 57 | + next(); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + request(bs.options.getIn(["urls", "local"])) |
| 62 | + .get("/index.html") |
| 63 | + .set("accept", "text/html") |
| 64 | + .expect(200) |
| 65 | + .end(function (err, res) { |
| 66 | + assert.ok(headers["x-forwarded-for"]); |
| 67 | + assert.ok(headers["x-forwarded-port"]); |
| 68 | + assert.equal(res.text, expected); |
| 69 | + done(); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments