1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/tests/utils/matchHostname.test.js

52 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

const matchHostname = require("../../src/utils/matchHostname.js");
2024-08-24 17:23:06 +02:00
describe("Hostname matching function", () => {
test("should return true if hostname is undefined", () => {
expect(matchHostname(undefined, "example.com")).toBe(true);
});
test('should return true if hostname is "*"', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*", "example.com")).toBe(true);
});
2024-08-24 17:23:06 +02:00
test("should return true if reqHostname matches hostname exactly", () => {
expect(matchHostname("example.com", "example.com")).toBe(true);
});
2024-08-24 17:23:06 +02:00
test("should return false if reqHostname does not match hostname exactly", () => {
expect(matchHostname("example.com", "example.org")).toBe(false);
});
test('should return true if hostname starts with "*." and reqHostname matches the root', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*.example.com", "sub.example.com")).toBe(true);
});
test('should return false if hostname starts with "*." and reqHostname does not match the root', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*.example.com", "example.org")).toBe(false);
});
test('should return true if hostname starts with "*." and reqHostname is the root', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*.example.com", "example.com")).toBe(true);
});
test('should return false if hostname is "*."', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*.", "example.com")).toBe(false);
});
2024-08-24 17:23:06 +02:00
test("should return false if reqHostname is undefined", () => {
expect(matchHostname("example.com", undefined)).toBe(false);
});
test('should return false if hostname does not start with "*." and reqHostname does not match', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("sub.example.com", "example.com")).toBe(false);
});
test('should return true if hostname starts with "*." and reqHostname matches the root with additional subdomains', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*.example.com", "sub.sub.example.com")).toBe(true);
});
test('should return false if hostname starts with "*." and reqHostname does not match the root with additional subdomains', () => {
2024-08-24 17:23:06 +02:00
expect(matchHostname("*.example.com", "sub.sub.example.org")).toBe(false);
});
});