|
| 1 | +import { applyOverrides, getOverridesFromCookies } from "../server/server-api"; |
| 2 | +import http from "http"; |
| 3 | +import { serialize } from "cookie"; |
| 4 | + |
| 5 | +describe("applyOverrides", () => { |
| 6 | + it("should apply overrides to the import map", () => { |
| 7 | + // Arrange |
| 8 | + const importMap = { |
| 9 | + imports: { |
| 10 | + package1: "https://cdn.skypack.dev/package1", |
| 11 | + }, |
| 12 | + }; |
| 13 | + const overrides = { |
| 14 | + package1: "https://unpkg.com/package1", |
| 15 | + package2: "https://unpkg.com/package2", |
| 16 | + }; |
| 17 | + |
| 18 | + // Act |
| 19 | + const result = applyOverrides(importMap, overrides); |
| 20 | + |
| 21 | + // Assert |
| 22 | + expect(importMap.imports.package1).toBe("https://cdn.skypack.dev/package1"); |
| 23 | + expect(result.imports.package1).toBe("https://unpkg.com/package1"); |
| 24 | + expect(result.imports.package2).toBe("https://unpkg.com/package2"); |
| 25 | + expect(result.scopes).toEqual({}); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should handle empty overrides", () => { |
| 29 | + // Arrange |
| 30 | + const importMap = { |
| 31 | + imports: { |
| 32 | + package1: "https://cdn.skypack.dev/package1", |
| 33 | + }, |
| 34 | + }; |
| 35 | + const overrides = {}; |
| 36 | + |
| 37 | + // Act |
| 38 | + const result = applyOverrides(importMap, overrides); |
| 39 | + |
| 40 | + // Assert |
| 41 | + expect(result.imports.package1).toBe("https://cdn.skypack.dev/package1"); |
| 42 | + expect(result.scopes).toEqual({}); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should handle overrides with scopes", () => { |
| 46 | + // Arrange |
| 47 | + const importMap = { |
| 48 | + imports: { |
| 49 | + package1: "https://cdn.skypack.dev/package1", |
| 50 | + }, |
| 51 | + scopes: { |
| 52 | + "https://example.com/": { |
| 53 | + package2: "https://cdn.skypack.dev/package2", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + const overrides = { |
| 58 | + package2: "https://unpkg.com/package2", |
| 59 | + }; |
| 60 | + |
| 61 | + // Act |
| 62 | + const result = applyOverrides(importMap, overrides); |
| 63 | + |
| 64 | + // Assert |
| 65 | + expect(result.scopes["https://example.com/"].package2).toBe( |
| 66 | + "https://cdn.skypack.dev/package2", |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe("getOverridesFromCookies", () => { |
| 72 | + const req = new http.IncomingMessage(); |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + req.headers = {}; |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return an empty object if no cookies are set", () => { |
| 79 | + // Act |
| 80 | + const result = getOverridesFromCookies(req); |
| 81 | + |
| 82 | + // Assert |
| 83 | + expect(result).toEqual({}); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should return overrides from cookies", () => { |
| 87 | + // Arrange |
| 88 | + req.headers.cookie = serialize( |
| 89 | + "import-map-override:package1", |
| 90 | + "https://unpkg.com/package1", |
| 91 | + ); |
| 92 | + |
| 93 | + // Act |
| 94 | + const result = getOverridesFromCookies(req); |
| 95 | + |
| 96 | + // Assert |
| 97 | + expect(result.package1).toBe("https://unpkg.com/package1"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle multiple overrides from cookies", () => { |
| 101 | + // Arrange |
| 102 | + req.headers.cookie = [ |
| 103 | + serialize("import-map-override:package1", "https://unpkg.com/package1"), |
| 104 | + serialize("import-map-override:package2", "https://unpkg.com/package2"), |
| 105 | + ].join("; "); |
| 106 | + |
| 107 | + // Act |
| 108 | + const result = getOverridesFromCookies(req); |
| 109 | + |
| 110 | + // Assert |
| 111 | + expect(result.package1).toBe("https://unpkg.com/package1"); |
| 112 | + expect(result.package2).toBe("https://unpkg.com/package2"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should handle port numbers in cookies", () => { |
| 116 | + // Arrange |
| 117 | + req.headers.cookie = serialize("import-map-override:package1", "8080"); |
| 118 | + |
| 119 | + // Act |
| 120 | + const result = getOverridesFromCookies( |
| 121 | + req, |
| 122 | + (port, moduleName) => `http://localhost:${port}/${moduleName}.js`, |
| 123 | + ); |
| 124 | + |
| 125 | + // Assert |
| 126 | + expect(result.package1).toBe("http://localhost:8080/package1.js"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should handle protocol-relative URLs in cookies", () => { |
| 130 | + // Arrange |
| 131 | + req.protocol = "https"; |
| 132 | + req.headers.cookie = serialize( |
| 133 | + "import-map-override:package1", |
| 134 | + "//unpkg.com/package1", |
| 135 | + ); |
| 136 | + |
| 137 | + // Act |
| 138 | + const result = getOverridesFromCookies(req); |
| 139 | + |
| 140 | + // Assert |
| 141 | + expect(result.package1).toBe("https://unpkg.com/package1"); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should ignore cookies that do not start with the correct prefix", () => { |
| 145 | + // Arrange |
| 146 | + req.headers.cookie = serialize("some-other-cookie", "some-value"); |
| 147 | + |
| 148 | + // Act |
| 149 | + const result = getOverridesFromCookies(req); |
| 150 | + |
| 151 | + // Assert |
| 152 | + expect(result).toEqual({}); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should handle cookies with empty module names", () => { |
| 156 | + // Arrange |
| 157 | + req.headers.cookie = serialize( |
| 158 | + "import-map-override:", |
| 159 | + "https://unpkg.com/package1", |
| 160 | + ); |
| 161 | + |
| 162 | + // Act |
| 163 | + const result = getOverridesFromCookies(req); |
| 164 | + |
| 165 | + // Assert |
| 166 | + expect(result).toEqual({}); |
| 167 | + }); |
| 168 | +}); |
0 commit comments