// Copyright 2020 Fastly, Inc. import { URL } from "../url"; describe("Relative URLs", () => { test("Can parse protocol relative URLs", () => { let url = new URL( "//user:pass@sub.example.com:8080/p/a/t/h?query=string#hash", "https://fastly.com" ); expect(url.protocol).toBe("https:"); expect(url.username).toBe("user"); expect(url.password).toBe("pass"); expect(url.hostname).toBe("sub.example.com"); expect(url.port).toBe("8080"); expect(url.host).toBe("sub.example.com:8080"); expect(url.origin).toBe("https://sub.example.com:8080"); expect(url.pathname).toBe("/p/a/t/h"); expect(url.search).toBe("?query=string"); expect(url.hash).toBe("#hash"); expect(url.href).toBe( "https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash" ); }); test("Can parse path absolute URLs", () => { let url = new URL("/p/a/t/h?query=string#hash", "https://fastly.com"); expect(url.pathname).toBe("/p/a/t/h"); expect(url.search).toBe("?query=string"); expect(url.hash).toBe("#hash"); expect(url.href).toBe("https://fastly.com/p/a/t/h?query=string#hash"); }); test("Can parse complex path relative URLs", () => { let url = new URL( "../././.././p/a/t/h?query=string#hash", "https://fastly.com/path1/path2" ); expect(url.pathname).toBe("/p/a/t/h"); expect(url.search).toBe("?query=string"); expect(url.hash).toBe("#hash"); expect(url.href).toBe("https://fastly.com/p/a/t/h?query=string#hash"); }); test("Can parse path relative URLs (without parent)", () => { let url = new URL( "./p/a/t/h?query=string#hash", "https://fastly.com/somepath" ); expect(url.pathname).toBe("/somepath/p/a/t/h"); expect(url.search).toBe("?query=string"); expect(url.hash).toBe("#hash"); expect(url.href).toBe( "https://fastly.com/somepath/p/a/t/h?query=string#hash" ); }); test("Can parse path relative URLs (with parent), to the root", () => { let url = new URL( "../../p/a/t/h?query=string#hash", "https://fastly.com/path1/path2" ); expect(url.pathname).toBe("/p/a/t/h"); expect(url.search).toBe("?query=string"); expect(url.hash).toBe("#hash"); expect(url.href).toBe("https://fastly.com/p/a/t/h?query=string#hash"); }); test("Can parse path relative URLs (with parent), up a single directory", () => { let url = new URL( "../p/a/t/h?query=string#hash", "https://fastly.com/path1/path2" ); expect(url.pathname).toBe("/path1/p/a/t/h"); expect(url.search).toBe("?query=string"); expect(url.hash).toBe("#hash"); expect(url.href).toBe("https://fastly.com/path1/p/a/t/h?query=string#hash"); }); // NOTE (torch2424): This test throws an error, and creates a high Rtrace (RTrace: +16) // This test needs to run last, or else will cause issues with aspect // Opened an issue here for discussion: https://github.com/jtenner/as-pect/issues/323 test("Can parse path relative URLs (with parent), will error if too high", () => { expect(() => { let url = new URL( "../../../p/a/t/h?query=string#hash", "https://fastly.com/path1/path2" ); }).toThrow(); }); });