diff --git a/apps/api/src/http/url-validator.test.ts b/apps/api/src/http/url-validator.test.ts index cab778997..f4353ea0b 100644 --- a/apps/api/src/http/url-validator.test.ts +++ b/apps/api/src/http/url-validator.test.ts @@ -1,22 +1,40 @@ import { assert, describe, expect, it } from "@effect/vitest" -import { Effect } from "effect" -import { safeFetch, UrlValidationError, validateExternalUrl, validateExternalUrlSync } from "./url-validator" +import { Effect, Result } from "effect" +import { + safeFetch, + UrlValidationError, + validateExternalUrl, + validateExternalUrlResult, +} from "./url-validator" -describe("validateExternalUrlSync", () => { +/** Rejected, and rejected as the tagged error rather than by throwing. */ +const rejects = (raw: string): void => { + const result = validateExternalUrlResult(raw) + assert.isTrue(Result.isFailure(result), `expected ${raw} to be rejected`) + if (Result.isFailure(result)) expect(result.failure).toBeInstanceOf(UrlValidationError) +} + +const accepts = (raw: string): URL => { + const result = validateExternalUrlResult(raw) + assert.isTrue(Result.isSuccess(result), `expected ${raw} to be accepted`) + return Result.isSuccess(result) ? result.success : new URL("https://unreachable.invalid") +} + +describe("validateExternalUrlResult", () => { it("accepts public https URLs", () => { - const url = validateExternalUrlSync("https://api.example.com/probe") + const url = accepts("https://api.example.com/probe") expect(url.hostname).toBe("api.example.com") }) it("accepts public http URLs", () => { - const url = validateExternalUrlSync("http://prom.public.dev:9090/metrics") + const url = accepts("http://prom.public.dev:9090/metrics") expect(url.hostname).toBe("prom.public.dev") }) it.each(["javascript:alert(1)", "file:///etc/passwd", "ftp://example.com", "data:text/html,