|
| 1 | +import * as dns from "./dns"; |
| 2 | + |
| 3 | +describe("containsValidCharacters", () => { |
| 4 | + test("alphanumerics, dots, and dashes pass", () => { |
| 5 | + expect(dns.containsValidCharacters(".string-starting.with-dot")).toBe(true); |
| 6 | + expect(dns.containsValidCharacters("-string.starting-with-dash")).toBe( |
| 7 | + true |
| 8 | + ); |
| 9 | + expect(dns.containsValidCharacters("string-starting.with.dot")).toBe(true); |
| 10 | + expect(dns.containsValidCharacters("string-ending.with.dot.")).toBe(true); |
| 11 | + expect(dns.containsValidCharacters("string-ending.with.dash-")).toBe(true); |
| 12 | + expect(dns.containsValidCharacters("string-with.with.char-x")).toBe(true); |
| 13 | + }); |
| 14 | + |
| 15 | + test("non alphanumerics, non-dots, non-dashes fail", () => { |
| 16 | + expect(dns.containsValidCharacters("string-with a-space")).toBe(false); |
| 17 | + expect(dns.containsValidCharacters("string-with-!")).toBe(false); |
| 18 | + expect(dns.containsValidCharacters("string-with-@")).toBe(false); |
| 19 | + expect(dns.containsValidCharacters("string-with-#")).toBe(false); |
| 20 | + expect(dns.containsValidCharacters("string-with-$")).toBe(false); |
| 21 | + expect(dns.containsValidCharacters("string-with-%")).toBe(false); |
| 22 | + expect(dns.containsValidCharacters("string-with-^")).toBe(false); |
| 23 | + expect(dns.containsValidCharacters("string-with-&")).toBe(false); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("isValid", () => { |
| 28 | + test("valid DNS pass", () => { |
| 29 | + expect(dns.isValid("foo.com")).toBe(true); |
| 30 | + expect(dns.isValid("foo.bar.baz.123.com")).toBe(true); |
| 31 | + expect(dns.isValid("1.foo.bar.123")).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + test("invalid DNS fail", () => { |
| 35 | + expect(dns.isValid("$foo.com")).toBe(false); |
| 36 | + expect(dns.isValid("!foo.bar.baz.123.com")).toBe(false); |
| 37 | + expect(dns.isValid("%1.foo.bar.123")).toBe(false); |
| 38 | + expect(dns.isValid("foo.com%")).toBe(false); |
| 39 | + expect(dns.isValid("foo.bar.baz.123.com!")).toBe(false); |
| 40 | + expect(dns.isValid("1.foo.bar.123#")).toBe(false); |
| 41 | + expect(dns.isValid("foo/com%")).toBe(false); |
| 42 | + expect(dns.isValid("foo.bar#!@#baz.123.com")).toBe(false); |
| 43 | + expect(dns.isValid("1.foo*&bar.123")).toBe(false); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("replaceIllegalCharacters", () => { |
| 48 | + test("all characters become lowercase", () => { |
| 49 | + expect(dns.replaceIllegalCharacters("ABC")).toBe("abc"); |
| 50 | + }); |
| 51 | + test("all non-alphanumerics become dashes", () => { |
| 52 | + expect(dns.replaceIllegalCharacters("a!a@a#a$a%a^a&a*a")).toBe( |
| 53 | + "a-a-a-a-a-a-a-a-a" |
| 54 | + ); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("assertIsValid", () => { |
| 59 | + test("does not throw when valid", () => { |
| 60 | + expect(() => dns.assertIsValid("foo", "foo.bar.com")).not.toThrow(); |
| 61 | + expect(() => dns.assertIsValid("foo", "foo.bar-com")).not.toThrow(); |
| 62 | + }); |
| 63 | + |
| 64 | + test("throws when invalid", () => { |
| 65 | + expect(() => dns.assertIsValid("foo", "-foo")).toThrow(); |
| 66 | + expect(() => dns.assertIsValid("foo", "_foo")).toThrow(); |
| 67 | + expect(() => dns.assertIsValid("foo", "foo-")).toThrow(); |
| 68 | + expect(() => dns.assertIsValid("foo", "foo_")).toThrow(); |
| 69 | + expect(() => dns.assertIsValid("foo", "invalid#dns$name")).toThrow(); |
| 70 | + expect(() => dns.assertIsValid("foo", "invalid/dns/name")).toThrow(); |
| 71 | + expect(() => dns.assertIsValid("foo", "invalid@dns%name")).toThrow(); |
| 72 | + }); |
| 73 | +}); |
0 commit comments