|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + buildFullyQualifiedUrl, |
| 4 | + isFullyQualifiedUrl |
| 5 | +} from "../../utils/urlUtils"; |
| 6 | + |
| 7 | +describe("urlUtils", () => { |
| 8 | + describe("isFullyQualifiedUrl", () => { |
| 9 | + it("should return true when url is valid", async () => { |
| 10 | + const result = isFullyQualifiedUrl("https://diamond.ac.uk:4000/path1"); |
| 11 | + expect(result).toEqual(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should return false when url is undefined", async () => { |
| 15 | + const result = isFullyQualifiedUrl(undefined); |
| 16 | + expect(result).toEqual(false); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should return false when url is invalid", async () => { |
| 20 | + const result = isFullyQualifiedUrl("abcde1234.ac.uk"); |
| 21 | + expect(result).toEqual(false); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("buildFullyQualifiedUrl", () => { |
| 26 | + it("should use base url when the joined path args don't make a fully qualified URL", async () => { |
| 27 | + const baseUrl = "http://diamond.ac.uk"; |
| 28 | + const args = ["path1"]; |
| 29 | + |
| 30 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 31 | + |
| 32 | + expect(result).toEqual("http://diamond.ac.uk/path1"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should return the default base url when the path args not specified", async () => { |
| 36 | + const baseUrl = "http://diamond.ac.uk"; |
| 37 | + |
| 38 | + const result = buildFullyQualifiedUrl(baseUrl); |
| 39 | + |
| 40 | + expect(result).toEqual("http://diamond.ac.uk/"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should return the default base url when the only path arg is undefined", async () => { |
| 44 | + const baseUrl = "http://diamond.ac.uk"; |
| 45 | + |
| 46 | + const result = buildFullyQualifiedUrl(baseUrl, undefined); |
| 47 | + |
| 48 | + expect(result).toEqual("http://diamond.ac.uk/"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("url encodes the path string", async () => { |
| 52 | + const baseUrl = "http://diamond.ac.uk"; |
| 53 | + const args = ["path 1"]; |
| 54 | + |
| 55 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 56 | + |
| 57 | + expect(result).toEqual("http://diamond.ac.uk/path%201"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("removes trailing and leading slash", async () => { |
| 61 | + const baseUrl = "http://diamond.ac.uk/"; |
| 62 | + const args = ["/path1/"]; |
| 63 | + |
| 64 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 65 | + |
| 66 | + expect(result).toEqual("http://diamond.ac.uk/path1"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("Joins multiple path args with / separator", async () => { |
| 70 | + const baseUrl = "http://diamond.ac.uk"; |
| 71 | + const args = ["/path1/", "/path2", "path3/"]; |
| 72 | + |
| 73 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 74 | + |
| 75 | + expect(result).toEqual("http://diamond.ac.uk/path1/path2/path3"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("Joins multiple path args with / separator, ignores filename component of host path", async () => { |
| 79 | + const baseUrl = "http://diamond.ac.uk/path0/filename"; |
| 80 | + const args = ["/path1/"]; |
| 81 | + |
| 82 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 83 | + |
| 84 | + expect(result).toEqual("http://diamond.ac.uk/path0/path1"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("Joins multiple path args with / separator, trailing slash on the url", async () => { |
| 88 | + const baseUrl = "http://diamond.ac.uk/path0/path1/"; |
| 89 | + const args = ["/path10/", "/path20"]; |
| 90 | + |
| 91 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 92 | + |
| 93 | + expect(result).toEqual("http://diamond.ac.uk/path0/path1/path10/path20"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("ignores default base url if the args start with a fully qualified url", async () => { |
| 97 | + const baseUrl = "http://diamond.ac.uk"; |
| 98 | + const args = ["http://ral.ac.uk/", "path1", "path2"]; |
| 99 | + |
| 100 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 101 | + |
| 102 | + expect(result).toEqual("http://ral.ac.uk/path1/path2"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("ignores default base url if the args start with a fully qualified url and even if the base Url contains a path", async () => { |
| 106 | + const baseUrl = "https://diamond.ac.uk/path0"; |
| 107 | + const args = ["https://ral.ac.uk:4000/", "path1", "path2"]; |
| 108 | + |
| 109 | + const result = buildFullyQualifiedUrl(baseUrl, ...args); |
| 110 | + |
| 111 | + expect(result).toEqual("https://ral.ac.uk:4000/path1/path2"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("ignores default base url if the args start with a fully qualified url, when base url is invalid", async () => { |
| 115 | + const args = ["http://ral.ac.uk/", "path1", "path2"]; |
| 116 | + |
| 117 | + const result = buildFullyQualifiedUrl("abcd", ...args); |
| 118 | + |
| 119 | + expect(result).toEqual("http://ral.ac.uk/path1/path2"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("ignores default base url if the args start with a fully qualified url, when base url is undefined", async () => { |
| 123 | + const args = ["http://ral.ac.uk/", "path1", "path2"]; |
| 124 | + |
| 125 | + const result = buildFullyQualifiedUrl(undefined, ...args); |
| 126 | + |
| 127 | + expect(result).toEqual("http://ral.ac.uk/path1/path2"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("Throws if default base url is invalid and args don't form a fully qualified url", async () => { |
| 131 | + const args = ["path1", "path2"]; |
| 132 | + |
| 133 | + expect(() => buildFullyQualifiedUrl("abcd", ...args)).toThrow( |
| 134 | + "Invalid base URL: abcd and args do not form a valid URL" |
| 135 | + ); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments