1 |
|
2 |
|
3 | import { URL } from "../url";
|
4 |
|
5 | describe("Relative URLs", () => {
|
6 | test("Can parse protocol relative URLs", () => {
|
7 | let url = new URL(
|
8 | "//user:pass@sub.example.com:8080/p/a/t/h?query=string#hash",
|
9 | "https://fastly.com"
|
10 | );
|
11 | expect(url.protocol).toBe("https:");
|
12 | expect(url.username).toBe("user");
|
13 | expect(url.password).toBe("pass");
|
14 | expect(url.hostname).toBe("sub.example.com");
|
15 | expect(url.port).toBe("8080");
|
16 | expect(url.host).toBe("sub.example.com:8080");
|
17 | expect(url.origin).toBe("https://sub.example.com:8080");
|
18 | expect(url.pathname).toBe("/p/a/t/h");
|
19 | expect(url.search).toBe("?query=string");
|
20 | expect(url.hash).toBe("#hash");
|
21 | expect(url.href).toBe(
|
22 | "https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash"
|
23 | );
|
24 | });
|
25 |
|
26 | test("Can parse path absolute URLs", () => {
|
27 | let url = new URL("/p/a/t/h?query=string#hash", "https://fastly.com");
|
28 | expect(url.pathname).toBe("/p/a/t/h");
|
29 | expect(url.search).toBe("?query=string");
|
30 | expect(url.hash).toBe("#hash");
|
31 | expect(url.href).toBe("https://fastly.com/p/a/t/h?query=string#hash");
|
32 | });
|
33 |
|
34 | test("Can parse complex path relative URLs", () => {
|
35 | let url = new URL(
|
36 | "../././.././p/a/t/h?query=string#hash",
|
37 | "https://fastly.com/path1/path2"
|
38 | );
|
39 | expect(url.pathname).toBe("/p/a/t/h");
|
40 | expect(url.search).toBe("?query=string");
|
41 | expect(url.hash).toBe("#hash");
|
42 | expect(url.href).toBe("https://fastly.com/p/a/t/h?query=string#hash");
|
43 | });
|
44 |
|
45 | test("Can parse path relative URLs (without parent)", () => {
|
46 | let url = new URL(
|
47 | "./p/a/t/h?query=string#hash",
|
48 | "https://fastly.com/somepath"
|
49 | );
|
50 | expect(url.pathname).toBe("/somepath/p/a/t/h");
|
51 | expect(url.search).toBe("?query=string");
|
52 | expect(url.hash).toBe("#hash");
|
53 | expect(url.href).toBe(
|
54 | "https://fastly.com/somepath/p/a/t/h?query=string#hash"
|
55 | );
|
56 | });
|
57 |
|
58 | test("Can parse path relative URLs (with parent), to the root", () => {
|
59 | let url = new URL(
|
60 | "../../p/a/t/h?query=string#hash",
|
61 | "https://fastly.com/path1/path2"
|
62 | );
|
63 | expect(url.pathname).toBe("/p/a/t/h");
|
64 | expect(url.search).toBe("?query=string");
|
65 | expect(url.hash).toBe("#hash");
|
66 | expect(url.href).toBe("https://fastly.com/p/a/t/h?query=string#hash");
|
67 | });
|
68 |
|
69 | test("Can parse path relative URLs (with parent), up a single directory", () => {
|
70 | let url = new URL(
|
71 | "../p/a/t/h?query=string#hash",
|
72 | "https://fastly.com/path1/path2"
|
73 | );
|
74 | expect(url.pathname).toBe("/path1/p/a/t/h");
|
75 | expect(url.search).toBe("?query=string");
|
76 | expect(url.hash).toBe("#hash");
|
77 | expect(url.href).toBe("https://fastly.com/path1/p/a/t/h?query=string#hash");
|
78 | });
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | test("Can parse path relative URLs (with parent), will error if too high", () => {
|
84 | expect(() => {
|
85 | let url = new URL(
|
86 | "../../../p/a/t/h?query=string#hash",
|
87 | "https://fastly.com/path1/path2"
|
88 | );
|
89 | }).toThrow();
|
90 | });
|
91 | });
|