-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for Server.prototype.checkHost
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
}); | ||
}); |