|
| 1 | +import { getHostFromUrl } from "./parseURL" |
| 2 | + |
| 3 | +describe("getHostFromUrl", () => { |
| 4 | + it("should throw when no host is found", () => { |
| 5 | + expect(() => { |
| 6 | + getHostFromUrl("") |
| 7 | + }).toThrow() |
| 8 | + }) |
| 9 | + |
| 10 | + it("should get host from URL without scheme", () => { |
| 11 | + Object.entries({ |
| 12 | + localhost: "localhost", |
| 13 | + "127.0.0.1": "127.0.0.1", |
| 14 | + "[::1]": "[::1]", |
| 15 | + }).forEach(([host, url]) => { |
| 16 | + expect(getHostFromUrl(url)).toEqual(host) |
| 17 | + }) |
| 18 | + expect(getHostFromUrl("localhost")).toEqual("localhost") |
| 19 | + expect(getHostFromUrl("127.0.0.1")).toEqual("127.0.0.1") |
| 20 | + }) |
| 21 | + |
| 22 | + it("should get the host from URL with http scheme", () => { |
| 23 | + Object.entries({ |
| 24 | + localhost: "http://localhost", |
| 25 | + "example.com": "http://example.com", |
| 26 | + }).forEach(([host, url]) => { |
| 27 | + expect(getHostFromUrl(url)).toEqual(host) |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it("should get the host from URL with https scheme", () => { |
| 32 | + Object.entries({ |
| 33 | + localhost: "https://localhost", |
| 34 | + "example.com": "https://example.com", |
| 35 | + }).forEach(([host, url]) => { |
| 36 | + expect(getHostFromUrl(url)).toEqual(host) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + it("should get the host from URL and ignore path, port, and query params", () => { |
| 41 | + Object.entries({ |
| 42 | + localhost: |
| 43 | + "http://localhost:8081/.expo/.virtual-metro-entry.bundle?platform=ios&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.reactotronapp", |
| 44 | + "192.168.1.141": |
| 45 | + "https://192.168.1.141:8081/.expo/.virtual-metro-entry.bundle?platform=ios&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.reactotronapp", |
| 46 | + }).forEach(([host, url]) => { |
| 47 | + expect(getHostFromUrl(url)).toEqual(host) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it("should get the host from an IPv6 URL and ignore path, port, and query params", () => { |
| 52 | + Object.entries({ |
| 53 | + "[::1]": |
| 54 | + "http://[::1]:8081/.expo/.virtual-metro-entry.bundle?platform=ios&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.reactotronapp", |
| 55 | + "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]": |
| 56 | + "https://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8081/.expo/.virtual-metro-entry.bundle?platform=ios&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.reactotronapp", |
| 57 | + }).forEach(([host, url]) => { |
| 58 | + expect(getHostFromUrl(url)).toEqual(host) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it("should get the host from URL with hyphens", () => { |
| 63 | + expect(getHostFromUrl("https://example-app.com")).toEqual("example-app.com") |
| 64 | + }) |
| 65 | + |
| 66 | + it("should throw when the URL is an unsupported scheme", () => { |
| 67 | + expect(() => { |
| 68 | + getHostFromUrl("file:///Users/tron") |
| 69 | + }).toThrow() |
| 70 | + }) |
| 71 | +}) |
0 commit comments