Skip to content

Commit

Permalink
add tests for Server.prototype.checkHost
Browse files Browse the repository at this point in the history
  • Loading branch information
orteth01 committed May 2, 2017
1 parent 3eaa26e commit c98e3b7
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/checkHost.test.js
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;
});
});
});

0 comments on commit c98e3b7

Please sign in to comment.