|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const Server = require("../lib/Server"); |
| 4 | + |
| 5 | +describe("Server.prototype.checkHost", () => { |
| 6 | + const checkHost = (context, headers) => Server.prototype.checkHost.call(context, headers); |
| 7 | + let context; |
| 8 | + let headers; |
| 9 | + |
| 10 | + it("should return false by default", () => { |
| 11 | + context = {}; |
| 12 | + headers = { host: "www.example.com" }; |
| 13 | + checkHost(context, headers).should.be.false; |
| 14 | + }); |
| 15 | + it("should return true if 'disableHostCheck' option is true", () => { |
| 16 | + context = { disableHostCheck: true }; |
| 17 | + headers = { host: "www.example.com" }; |
| 18 | + checkHost(context, headers).should.be.true; |
| 19 | + }); |
| 20 | + it("should return false if no host header is present and disableHostCheck is not true", () => { |
| 21 | + context = {}; |
| 22 | + headers = {}; |
| 23 | + checkHost(context, headers).should.be.false; |
| 24 | + }); |
| 25 | + it("should return true if host is 'localhost' or '127.0.0.1'", () => { |
| 26 | + context = {}; |
| 27 | + |
| 28 | + headers = { host: "localhost" }; |
| 29 | + checkHost(context, headers).should.be.true; |
| 30 | + |
| 31 | + headers = { host: "127.0.0.1" }; |
| 32 | + checkHost(context, headers).should.be.true; |
| 33 | + }); |
| 34 | + it("should return true if host === listenHostName", () => { |
| 35 | + context = { listenHostName: "www.example.com" }; |
| 36 | + headers = { host: "www.example.com" }; |
| 37 | + checkHost(context, headers).should.be.true; |
| 38 | + }); |
| 39 | + it("should return true if host === publicHost", () => { |
| 40 | + context = { publicHost: "www.example.com" }; |
| 41 | + headers = { host: "www.example.com" }; |
| 42 | + checkHost(context, headers).should.be.true; |
| 43 | + }); |
| 44 | + describe("allowed hosts", () => { |
| 45 | + it("should return true if host is in allowedHosts", () => { |
| 46 | + context = { allowedHosts: ["www.example.com"] }; |
| 47 | + headers = { host: "www.example.com" }; |
| 48 | + checkHost(context, headers).should.be.true; |
| 49 | + }); |
| 50 | + it("should return true if host passes a wildcard in allowedHosts", () => { |
| 51 | + context = { allowedHosts: [".example.com"] }; |
| 52 | + |
| 53 | + headers = { host: "www.example.com" }; |
| 54 | + checkHost(context, headers).should.be.true; |
| 55 | + |
| 56 | + headers = { host: "subdomain.example.com" }; |
| 57 | + checkHost(context, headers).should.be.true; |
| 58 | + |
| 59 | + headers = { host: "example.com" }; |
| 60 | + checkHost(context, headers).should.be.true; |
| 61 | + |
| 62 | + headers = { host: "subsubdomain.subdomain.example.com" }; |
| 63 | + checkHost(context, headers).should.be.true; |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
0 commit comments