|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { createMisina, HTTPError, type ProblemDetails } from "../src/index.ts" |
| 3 | + |
| 4 | +describe("RFC 9457 — problem+json on HTTPError", () => { |
| 5 | + it("parses application/problem+json into err.problem", async () => { |
| 6 | + const driver = { |
| 7 | + name: "p", |
| 8 | + request: async () => |
| 9 | + new Response( |
| 10 | + JSON.stringify({ |
| 11 | + type: "https://example.test/errors/insufficient-funds", |
| 12 | + title: "Insufficient Funds", |
| 13 | + status: 402, |
| 14 | + detail: "Your balance is $0.00.", |
| 15 | + instance: "/transactions/123", |
| 16 | + balance: 0, |
| 17 | + }), |
| 18 | + { |
| 19 | + status: 402, |
| 20 | + headers: { "content-type": "application/problem+json" }, |
| 21 | + }, |
| 22 | + ), |
| 23 | + } |
| 24 | + const m = createMisina({ driver, retry: 0 }) |
| 25 | + |
| 26 | + const err = (await m.post("https://api.test/buy", { item: 1 }).catch((e) => e)) as HTTPError |
| 27 | + expect(err).toBeInstanceOf(HTTPError) |
| 28 | + expect(err.problem).toBeDefined() |
| 29 | + expect(err.problem?.type).toBe("https://example.test/errors/insufficient-funds") |
| 30 | + expect(err.problem?.title).toBe("Insufficient Funds") |
| 31 | + expect(err.problem?.status).toBe(402) |
| 32 | + expect(err.problem?.detail).toBe("Your balance is $0.00.") |
| 33 | + expect(err.problem?.instance).toBe("/transactions/123") |
| 34 | + // Extension fields are preserved. |
| 35 | + expect(err.problem?.balance).toBe(0) |
| 36 | + }) |
| 37 | + |
| 38 | + it("includes problem.detail in the error message when present", async () => { |
| 39 | + const driver = { |
| 40 | + name: "p", |
| 41 | + request: async () => |
| 42 | + new Response( |
| 43 | + JSON.stringify({ |
| 44 | + title: "Validation Error", |
| 45 | + detail: "field 'email' is required", |
| 46 | + }), |
| 47 | + { |
| 48 | + status: 400, |
| 49 | + statusText: "Bad Request", |
| 50 | + headers: { "content-type": "application/problem+json" }, |
| 51 | + }, |
| 52 | + ), |
| 53 | + } |
| 54 | + const m = createMisina({ driver, retry: 0 }) |
| 55 | + |
| 56 | + const err = (await m.post("https://api.test/", { x: 1 }).catch((e) => e)) as HTTPError |
| 57 | + expect(err.message).toContain("400") |
| 58 | + expect(err.message).toContain("field 'email' is required") |
| 59 | + }) |
| 60 | + |
| 61 | + it("falls back to problem.title when detail is missing", async () => { |
| 62 | + const driver = { |
| 63 | + name: "p", |
| 64 | + request: async () => |
| 65 | + new Response(JSON.stringify({ title: "Forbidden" }), { |
| 66 | + status: 403, |
| 67 | + headers: { "content-type": "application/problem+json" }, |
| 68 | + }), |
| 69 | + } |
| 70 | + const m = createMisina({ driver, retry: 0 }) |
| 71 | + |
| 72 | + const err = (await m.get("https://api.test/").catch((e) => e)) as HTTPError |
| 73 | + expect(err.message).toContain("Forbidden") |
| 74 | + }) |
| 75 | + |
| 76 | + it("non-problem content-type → err.problem is undefined", async () => { |
| 77 | + const driver = { |
| 78 | + name: "p", |
| 79 | + request: async () => |
| 80 | + new Response(JSON.stringify({ title: "Forbidden" }), { |
| 81 | + status: 403, |
| 82 | + headers: { "content-type": "application/json" }, |
| 83 | + }), |
| 84 | + } |
| 85 | + const m = createMisina({ driver, retry: 0 }) |
| 86 | + |
| 87 | + const err = (await m.get("https://api.test/").catch((e) => e)) as HTTPError |
| 88 | + expect(err).toBeInstanceOf(HTTPError) |
| 89 | + expect(err.problem).toBeUndefined() |
| 90 | + }) |
| 91 | + |
| 92 | + it("application/problem+json with charset parameter still parses", async () => { |
| 93 | + const driver = { |
| 94 | + name: "p", |
| 95 | + request: async () => |
| 96 | + new Response(JSON.stringify({ title: "x", detail: "y" }), { |
| 97 | + status: 400, |
| 98 | + headers: { "content-type": "application/problem+json; charset=utf-8" }, |
| 99 | + }), |
| 100 | + } |
| 101 | + const m = createMisina({ driver, retry: 0 }) |
| 102 | + |
| 103 | + const err = (await m.get("https://api.test/").catch((e) => e)) as HTTPError |
| 104 | + expect(err.problem?.title).toBe("x") |
| 105 | + }) |
| 106 | + |
| 107 | + it("ProblemDetails type re-exports from the root", () => { |
| 108 | + // Compile-time check: the type is reachable from the root entry. |
| 109 | + const sample: ProblemDetails = { title: "x", status: 400 } |
| 110 | + expect(sample.title).toBe("x") |
| 111 | + }) |
| 112 | +}) |
0 commit comments