|
| 1 | +import { validateLibraryUrl } from "./library"; |
| 2 | + |
| 3 | +describe("validateLibraryUrl", () => { |
| 4 | + it("should validate hostname & pathname", () => { |
| 5 | + // valid hostnames |
| 6 | + // ------------------------------------------------------------------------- |
| 7 | + expect( |
| 8 | + validateLibraryUrl("https://www.excalidraw.com", ["excalidraw.com"]), |
| 9 | + ).toBe(true); |
| 10 | + expect( |
| 11 | + validateLibraryUrl("https://excalidraw.com", ["excalidraw.com"]), |
| 12 | + ).toBe(true); |
| 13 | + expect( |
| 14 | + validateLibraryUrl("https://library.excalidraw.com", ["excalidraw.com"]), |
| 15 | + ).toBe(true); |
| 16 | + expect( |
| 17 | + validateLibraryUrl("https://library.excalidraw.com", [ |
| 18 | + "library.excalidraw.com", |
| 19 | + ]), |
| 20 | + ).toBe(true); |
| 21 | + expect( |
| 22 | + validateLibraryUrl("https://excalidraw.com/", ["excalidraw.com/"]), |
| 23 | + ).toBe(true); |
| 24 | + expect( |
| 25 | + validateLibraryUrl("https://excalidraw.com", ["excalidraw.com/"]), |
| 26 | + ).toBe(true); |
| 27 | + expect( |
| 28 | + validateLibraryUrl("https://excalidraw.com/", ["excalidraw.com"]), |
| 29 | + ).toBe(true); |
| 30 | + |
| 31 | + // valid pathnames |
| 32 | + // ------------------------------------------------------------------------- |
| 33 | + expect( |
| 34 | + validateLibraryUrl("https://excalidraw.com/path", ["excalidraw.com"]), |
| 35 | + ).toBe(true); |
| 36 | + expect( |
| 37 | + validateLibraryUrl("https://excalidraw.com/path/", ["excalidraw.com"]), |
| 38 | + ).toBe(true); |
| 39 | + expect( |
| 40 | + validateLibraryUrl("https://excalidraw.com/specific/path", [ |
| 41 | + "excalidraw.com/specific/path", |
| 42 | + ]), |
| 43 | + ).toBe(true); |
| 44 | + expect( |
| 45 | + validateLibraryUrl("https://excalidraw.com/specific/path/", [ |
| 46 | + "excalidraw.com/specific/path", |
| 47 | + ]), |
| 48 | + ).toBe(true); |
| 49 | + expect( |
| 50 | + validateLibraryUrl("https://excalidraw.com/specific/path", [ |
| 51 | + "excalidraw.com/specific/path/", |
| 52 | + ]), |
| 53 | + ).toBe(true); |
| 54 | + expect( |
| 55 | + validateLibraryUrl("https://excalidraw.com/specific/path/other", [ |
| 56 | + "excalidraw.com/specific/path", |
| 57 | + ]), |
| 58 | + ).toBe(true); |
| 59 | + |
| 60 | + // invalid hostnames |
| 61 | + // ------------------------------------------------------------------------- |
| 62 | + expect(() => |
| 63 | + validateLibraryUrl("https://xexcalidraw.com", ["excalidraw.com"]), |
| 64 | + ).toThrow(); |
| 65 | + expect(() => |
| 66 | + validateLibraryUrl("https://x-excalidraw.com", ["excalidraw.com"]), |
| 67 | + ).toThrow(); |
| 68 | + expect(() => |
| 69 | + validateLibraryUrl("https://excalidraw.comx", ["excalidraw.com"]), |
| 70 | + ).toThrow(); |
| 71 | + expect(() => |
| 72 | + validateLibraryUrl("https://excalidraw.comx", ["excalidraw.com"]), |
| 73 | + ).toThrow(); |
| 74 | + expect(() => |
| 75 | + validateLibraryUrl("https://excalidraw.com.mx", ["excalidraw.com"]), |
| 76 | + ).toThrow(); |
| 77 | + // protocol must be https |
| 78 | + expect(() => |
| 79 | + validateLibraryUrl("http://excalidraw.com.mx", ["excalidraw.com"]), |
| 80 | + ).toThrow(); |
| 81 | + |
| 82 | + // invalid pathnames |
| 83 | + // ------------------------------------------------------------------------- |
| 84 | + expect(() => |
| 85 | + validateLibraryUrl("https://excalidraw.com/specific/other/path", [ |
| 86 | + "excalidraw.com/specific/path", |
| 87 | + ]), |
| 88 | + ).toThrow(); |
| 89 | + expect(() => |
| 90 | + validateLibraryUrl("https://excalidraw.com/specific/paths", [ |
| 91 | + "excalidraw.com/specific/path", |
| 92 | + ]), |
| 93 | + ).toThrow(); |
| 94 | + expect(() => |
| 95 | + validateLibraryUrl("https://excalidraw.com/specific/path-s", [ |
| 96 | + "excalidraw.com/specific/path", |
| 97 | + ]), |
| 98 | + ).toThrow(); |
| 99 | + expect(() => |
| 100 | + validateLibraryUrl("https://excalidraw.com/some/specific/path", [ |
| 101 | + "excalidraw.com/specific/path", |
| 102 | + ]), |
| 103 | + ).toThrow(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments