|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { sanitizeWebFetchUrl } from "./web-fetch.js"; |
| 3 | + |
| 4 | +describe("sanitizeWebFetchUrl", () => { |
| 5 | +it("removes whitespace between scheme and authority (reported bug)", () => { |
| 6 | +expect(sanitizeWebFetchUrl("https:// docs.openclaw.ai")).toBe("https://docs.openclaw.ai"); |
| 7 | +}); |
| 8 | + |
| 9 | +it("trims leading and trailing whitespace", () => { |
| 10 | +expect(sanitizeWebFetchUrl(" https://example.com ")).toBe("https://example.com"); |
| 11 | +}); |
| 12 | + |
| 13 | +it("trims leading Unicode whitespace", () => { |
| 14 | +expect(sanitizeWebFetchUrl("\u00a0\ufeffhttps://example.com")).toBe("https://example.com"); |
| 15 | +}); |
| 16 | + |
| 17 | +it("trims trailing newlines", () => { |
| 18 | +expect(sanitizeWebFetchUrl("https://example.com\n")).toBe("https://example.com"); |
| 19 | +}); |
| 20 | + |
| 21 | +it("preserves trailing Unicode whitespace in paths", () => { |
| 22 | +expect(sanitizeWebFetchUrl("https://example.com/a\u00a0")).toBe("https://example.com/a\u00a0"); |
| 23 | +}); |
| 24 | + |
| 25 | +it("trims trailing Unicode whitespace after a bare authority", () => { |
| 26 | +expect(sanitizeWebFetchUrl("https://example.com\u00a0")).toBe("https://example.com"); |
| 27 | +}); |
| 28 | + |
| 29 | +it("preserves spaces in the path component", () => { |
| 30 | +// WHATWG URL parser percent-encodes path spaces — they must not be stripped |
| 31 | +const result = sanitizeWebFetchUrl("https://example.com/a b"); |
| 32 | +expect(result).toBe("https://example.com/a b"); |
| 33 | +}); |
| 34 | + |
| 35 | +it("preserves spaces in the query component", () => { |
| 36 | +const result = sanitizeWebFetchUrl("https://example.com?q=a b"); |
| 37 | +expect(result).toBe("https://example.com?q=a b"); |
| 38 | +}); |
| 39 | + |
| 40 | +it("preserves scheme-like text in the path component", () => { |
| 41 | +const result = sanitizeWebFetchUrl("https://example.com/a:// b"); |
| 42 | +expect(result).toBe("https://example.com/a:// b"); |
| 43 | +}); |
| 44 | + |
| 45 | +it("preserves scheme-like text in the query component", () => { |
| 46 | +const result = sanitizeWebFetchUrl("https://example.com?q=x:// y"); |
| 47 | +expect(result).toBe("https://example.com?q=x:// y"); |
| 48 | +}); |
| 49 | + |
| 50 | +it("preserves percent-encoded characters in path", () => { |
| 51 | +const result = sanitizeWebFetchUrl("https://example.com/a%20b"); |
| 52 | +expect(result).toBe("https://example.com/a%20b"); |
| 53 | +}); |
| 54 | + |
| 55 | +it("does not modify already-valid URLs", () => { |
| 56 | +expect(sanitizeWebFetchUrl("https://docs.openclaw.ai")).toBe("https://docs.openclaw.ai"); |
| 57 | +}); |
| 58 | + |
| 59 | +it("handles https:// with tab after scheme", () => { |
| 60 | +expect(sanitizeWebFetchUrl("https://\texample.com")).toBe("https://example.com"); |
| 61 | +}); |
| 62 | +}); |