2018-11-04 07:59:52 +00:00
|
|
|
/* eslint-disable flowtype/require-valid-file-annotation */
|
2020-06-23 04:29:21 +00:00
|
|
|
import { stripSubdomain, parseDomain, isCustomSubdomain } from "./domains";
|
2019-01-12 21:50:30 +00:00
|
|
|
|
|
|
|
// test suite is based on subset of parse-domain module we want to support
|
|
|
|
// https://github.com/peerigon/parse-domain/blob/master/test/parseDomain.test.js
|
2020-06-23 04:29:21 +00:00
|
|
|
describe("#parseDomain", () => {
|
|
|
|
it("should remove the protocol", () => {
|
|
|
|
expect(parseDomain("http://example.com")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
2020-06-23 04:29:21 +00:00
|
|
|
expect(parseDomain("//example.com")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
2020-06-23 04:29:21 +00:00
|
|
|
expect(parseDomain("https://example.com")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should remove sub-domains", () => {
|
|
|
|
expect(parseDomain("www.example.com")).toMatchObject({
|
|
|
|
subdomain: "www",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should remove the path", () => {
|
|
|
|
expect(parseDomain("example.com/some/path?and&query")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
2020-06-23 04:29:21 +00:00
|
|
|
expect(parseDomain("example.com/")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should remove the query string", () => {
|
|
|
|
expect(parseDomain("example.com?and&query")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should remove special characters", () => {
|
|
|
|
expect(parseDomain("http://m.example.com\r")).toMatchObject({
|
|
|
|
subdomain: "m",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should remove the port", () => {
|
|
|
|
expect(parseDomain("example.com:8080")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should allow @ characters in the path", () => {
|
|
|
|
expect(parseDomain("https://medium.com/@username/")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "medium",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should also work with three-level domains like .co.uk", () => {
|
|
|
|
expect(parseDomain("www.example.co.uk")).toMatchObject({
|
|
|
|
subdomain: "www",
|
|
|
|
domain: "example",
|
|
|
|
tld: "co.uk",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should not include private domains like blogspot.com by default", () => {
|
|
|
|
expect(parseDomain("foo.blogspot.com")).toMatchObject({
|
|
|
|
subdomain: "foo",
|
|
|
|
domain: "blogspot",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should also work with the minimum", () => {
|
|
|
|
expect(parseDomain("example.com")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "example",
|
|
|
|
tld: "com",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should return null if the given value is not a string", () => {
|
2019-01-12 21:50:30 +00:00
|
|
|
expect(parseDomain(undefined)).toBe(null);
|
|
|
|
expect(parseDomain({})).toBe(null);
|
2020-06-23 04:29:21 +00:00
|
|
|
expect(parseDomain("")).toBe(null);
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
it("should work with custom top-level domains (eg .local)", () => {
|
|
|
|
expect(parseDomain("mymachine.local")).toMatchObject({
|
|
|
|
subdomain: "",
|
|
|
|
domain: "mymachine",
|
|
|
|
tld: "local",
|
2019-01-12 21:50:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-04 07:59:52 +00:00
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
describe("#stripSubdomain", () => {
|
|
|
|
test("to work with localhost", () => {
|
|
|
|
expect(stripSubdomain("localhost")).toBe("localhost");
|
2018-11-04 07:59:52 +00:00
|
|
|
});
|
2020-06-23 04:29:21 +00:00
|
|
|
test("to return domains without a subdomain", () => {
|
|
|
|
expect(stripSubdomain("example")).toBe("example");
|
|
|
|
expect(stripSubdomain("example.com")).toBe("example.com");
|
|
|
|
expect(stripSubdomain("example.org:3000")).toBe("example.org");
|
2018-11-04 07:59:52 +00:00
|
|
|
});
|
2020-06-23 04:29:21 +00:00
|
|
|
test("to remove subdomains", () => {
|
|
|
|
expect(stripSubdomain("test.example.com")).toBe("example.com");
|
|
|
|
expect(stripSubdomain("test.example.com:3000")).toBe("example.com");
|
2018-11-04 07:59:52 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-18 20:07:11 +00:00
|
|
|
|
2020-06-23 04:29:21 +00:00
|
|
|
describe("#isCustomSubdomain", () => {
|
|
|
|
test("to work with localhost", () => {
|
|
|
|
expect(isCustomSubdomain("localhost")).toBe(false);
|
|
|
|
});
|
|
|
|
test("to return false for domains without a subdomain", () => {
|
|
|
|
expect(isCustomSubdomain("example")).toBe(false);
|
|
|
|
expect(isCustomSubdomain("example.com")).toBe(false);
|
|
|
|
expect(isCustomSubdomain("example.org:3000")).toBe(false);
|
|
|
|
});
|
|
|
|
test("to return false for www", () => {
|
|
|
|
expect(isCustomSubdomain("www.example.com")).toBe(false);
|
|
|
|
expect(isCustomSubdomain("www.example.com:3000")).toBe(false);
|
|
|
|
});
|
|
|
|
test("to return true for subdomains", () => {
|
|
|
|
expect(isCustomSubdomain("test.example.com")).toBe(true);
|
|
|
|
expect(isCustomSubdomain("test.example.com:3000")).toBe(true);
|
2018-11-18 20:07:11 +00:00
|
|
|
});
|
|
|
|
});
|