Skip to content

Commit c98e3b7

Browse files
committed
add tests for Server.prototype.checkHost
1 parent 3eaa26e commit c98e3b7

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/checkHost.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
beforeEach(() => {
11+
context = {};
12+
headers = { host: "www.example.com" };
13+
});
14+
15+
it("should return false by default", () => {
16+
checkHost(context, headers).should.be.false;
17+
});
18+
it("should return true if 'disableHostCheck' option is true", () => {
19+
context = { disableHostCheck: true };
20+
checkHost(context, headers).should.be.true;
21+
});
22+
it("should return false if no host header is present and disableHostCheck is not true", () => {
23+
headers = {};
24+
checkHost(context, headers).should.be.false;
25+
});
26+
it("should return true if host is 'localhost' or '127.0.0.1'", () => {
27+
headers = { host: "localhost" };
28+
checkHost(context, headers).should.be.true;
29+
30+
headers = { host: "127.0.0.1" };
31+
checkHost(context, headers).should.be.true;
32+
});
33+
it("should return true if host === listenHostName", () => {
34+
context = { listenHostName: "www.example.com" };
35+
checkHost(context, headers).should.be.true;
36+
});
37+
it("should return true if host === publicHost", () => {
38+
context = { publicHost: "www.example.com" };
39+
checkHost(context, headers).should.be.true;
40+
});
41+
describe("allowed hosts", () => {
42+
it("should return true if host is in allowedHosts", () => {
43+
context = { allowedHosts: ["www.example.com"] };
44+
checkHost(context, headers).should.be.true;
45+
});
46+
it("should return true if host passes a wildcard in allowedHosts", () => {
47+
context = { allowedHosts: [".example.com"] };
48+
49+
headers = { host: "www.example.com" };
50+
checkHost(context, headers).should.be.true;
51+
52+
headers = { host: "subdomain.example.com" };
53+
checkHost(context, headers).should.be.true;
54+
55+
headers = { host: "example.com" };
56+
checkHost(context, headers).should.be.true;
57+
58+
headers = { host: "subsubdomain.subdomain.example.com" };
59+
checkHost(context, headers).should.be.true;
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)