diff --git a/test/checkHost.test.js b/test/checkHost.test.js new file mode 100644 index 0000000000..d036218438 --- /dev/null +++ b/test/checkHost.test.js @@ -0,0 +1,62 @@ +"use strict"; + +const Server = require("../lib/Server"); + +describe("Server.prototype.checkHost", () => { + const checkHost = (context, headers) => Server.prototype.checkHost.call(context, headers); + let context; + let headers; + + beforeEach(() => { + context = {}; + headers = { host: "www.example.com" }; + }); + + it("should return false by default", () => { + checkHost(context, headers).should.be.false; + }); + it("should return true if 'disableHostCheck' option is true", () => { + context = { disableHostCheck: true }; + checkHost(context, headers).should.be.true; + }); + it("should return false if no host header is present and disableHostCheck is not true", () => { + headers = {}; + checkHost(context, headers).should.be.false; + }); + it("should return true if host is 'localhost' or '127.0.0.1'", () => { + headers = { host: "localhost" }; + checkHost(context, headers).should.be.true; + + headers = { host: "127.0.0.1" }; + checkHost(context, headers).should.be.true; + }); + it("should return true if host === listenHostName", () => { + context = { listenHostName: "www.example.com" }; + checkHost(context, headers).should.be.true; + }); + it("should return true if host === publicHost", () => { + context = { publicHost: "www.example.com" }; + checkHost(context, headers).should.be.true; + }); + describe("allowed hosts", () => { + it("should return true if host is in allowedHosts", () => { + context = { allowedHosts: ["www.example.com"] }; + checkHost(context, headers).should.be.true; + }); + it("should return true if host passes a wildcard in allowedHosts", () => { + context = { allowedHosts: [".example.com"] }; + + headers = { host: "www.example.com" }; + checkHost(context, headers).should.be.true; + + headers = { host: "subdomain.example.com" }; + checkHost(context, headers).should.be.true; + + headers = { host: "example.com" }; + checkHost(context, headers).should.be.true; + + headers = { host: "subsubdomain.subdomain.example.com" }; + checkHost(context, headers).should.be.true; + }); + }); +});