|
| 1 | +import { assertEquals } from "@std/assert"; |
| 2 | + |
| 3 | +// Reproduces the URL resolution behavior in authedFetch. |
| 4 | +// Before the fix: new URL(endpoint, base) drops path segments from the base. |
| 5 | +// After the fix: string concatenation preserves the full base path. |
| 6 | + |
| 7 | +Deno.test("new URL() two-arg drops base path segments", () => { |
| 8 | + // This is the OLD (broken) behavior that motivates the fix. |
| 9 | + // When the endpoint is a proxy URL with a path, the path-based |
| 10 | + // target host segment gets silently dropped. |
| 11 | + const base = "https://proxy.example.com/target-host"; |
| 12 | + const endpoint = "api/diffsync/org/app/rev123"; |
| 13 | + |
| 14 | + const broken = new URL(endpoint, base); |
| 15 | + // new URL resolves relative to the parent of "target-host", losing it: |
| 16 | + assertEquals(broken.pathname, "/api/diffsync/org/app/rev123"); |
| 17 | + // "target-host" is gone — the proxy can no longer route the request. |
| 18 | +}); |
| 19 | + |
| 20 | +Deno.test("string concatenation preserves base path segments", () => { |
| 21 | + // This is the FIXED behavior: concatenation keeps the full base path. |
| 22 | + const base = "https://proxy.example.com/target-host"; |
| 23 | + const endpoint = "api/diffsync/org/app/rev123"; |
| 24 | + |
| 25 | + const fixed = new URL(`${base}/${endpoint}`); |
| 26 | + assertEquals( |
| 27 | + fixed.pathname, |
| 28 | + "/target-host/api/diffsync/org/app/rev123", |
| 29 | + ); |
| 30 | +}); |
| 31 | + |
| 32 | +Deno.test("string concatenation works for standard endpoint too", () => { |
| 33 | + // Ensure the fix doesn't regress the normal (non-proxy) case. |
| 34 | + const base = "https://console.deno.com"; |
| 35 | + const endpoint = "api/diffsync/org/app/rev123"; |
| 36 | + |
| 37 | + const fixed = new URL(`${base}/${endpoint}`); |
| 38 | + assertEquals(fixed.pathname, "/api/diffsync/org/app/rev123"); |
| 39 | + assertEquals(fixed.hostname, "console.deno.com"); |
| 40 | +}); |
0 commit comments