|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { wrapAsPathBasedClient } from "../../src/index.js"; |
| 3 | +import { createObservedClient, headersToObj } from "../helpers.js"; |
| 4 | +import type { paths } from "./schemas/query.js"; |
| 5 | + |
| 6 | +describe("QUERY", () => { |
| 7 | + test("sends the correct method", async () => { |
| 8 | + let method = ""; |
| 9 | + const client = createObservedClient<paths>({}, async (req) => { |
| 10 | + method = req.method; |
| 11 | + return Response.json({}); |
| 12 | + }); |
| 13 | + await client.QUERY("/resources/{id}", { |
| 14 | + params: { path: { id: 123 } }, |
| 15 | + body: { ids: [1, 2, 3] }, |
| 16 | + }); |
| 17 | + expect(method).toBe("QUERY"); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("request body", () => { |
| 21 | + test("requires necessary requestBodies", async () => { |
| 22 | + const client = createObservedClient<paths>({}); |
| 23 | + |
| 24 | + // expect error on missing `body` |
| 25 | + await client.QUERY("/resources/{id}", { |
| 26 | + params: { path: { id: 1 } }, |
| 27 | + // @ts-expect-error |
| 28 | + body: undefined, |
| 29 | + }); |
| 30 | + |
| 31 | + // expect error on missing required fields |
| 32 | + await client.QUERY("/resources/{id}", { |
| 33 | + params: { path: { id: 1 } }, |
| 34 | + // @ts-expect-error |
| 35 | + body: {}, |
| 36 | + }); |
| 37 | + |
| 38 | + // expect present body to be good enough |
| 39 | + await client.QUERY("/resources/{id}", { |
| 40 | + params: { path: { id: 1 } }, |
| 41 | + body: { ids: [1, 2, 3] }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + test("requestBody with required: false", async () => { |
| 46 | + const client = createObservedClient<paths>({}); |
| 47 | + |
| 48 | + // assert missing `body` doesn't raise a TS error |
| 49 | + await client.QUERY("/resources-optional", { |
| 50 | + params: { path: { id: 1 } }, |
| 51 | + }); |
| 52 | + |
| 53 | + // assert error on type mismatch |
| 54 | + await client.QUERY("/resources-optional", { |
| 55 | + params: { path: { id: 1 } }, |
| 56 | + body: { |
| 57 | + // @ts-expect-error |
| 58 | + ids: "not-an-array", |
| 59 | + }, |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + test("sends correct options, returns success", async () => { |
| 65 | + const mockData = { status: "ok" }; |
| 66 | + let actualPathname = ""; |
| 67 | + const client = createObservedClient<paths>({}, async (req) => { |
| 68 | + actualPathname = new URL(req.url).pathname; |
| 69 | + return Response.json(mockData, { status: 200 }); |
| 70 | + }); |
| 71 | + |
| 72 | + const { data, error, response } = await client.QUERY("/resources/{id}", { |
| 73 | + params: { path: { id: 456 } }, |
| 74 | + body: { ids: [7, 8, 9] }, |
| 75 | + }); |
| 76 | + |
| 77 | + // assert correct URL was called |
| 78 | + expect(actualPathname).toBe("/resources/456"); |
| 79 | + |
| 80 | + // assert correct data was returned |
| 81 | + expect(data).toEqual(mockData); |
| 82 | + expect(response.status).toBe(200); |
| 83 | + |
| 84 | + // assert error is empty |
| 85 | + expect(error).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + test("sends the request body with a Content-Type", async () => { |
| 89 | + // RFC 10008 §2: a QUERY request has content, so it must identify its media type |
| 90 | + let actualBody = ""; |
| 91 | + let actualContentType: string | null = ""; |
| 92 | + const client = createObservedClient<paths>({}, async (req) => { |
| 93 | + actualBody = await req.text(); |
| 94 | + actualContentType = req.headers.get("Content-Type"); |
| 95 | + return Response.json({}); |
| 96 | + }); |
| 97 | + |
| 98 | + await client.QUERY("/resources/{id}", { |
| 99 | + params: { path: { id: 123 } }, |
| 100 | + body: { ids: [1, 2, 3] }, |
| 101 | + }); |
| 102 | + |
| 103 | + expect(actualBody).toBe(JSON.stringify({ ids: [1, 2, 3] })); |
| 104 | + expect(actualContentType).toBe("application/json"); |
| 105 | + }); |
| 106 | + |
| 107 | + // QUERY is defined as safe & idempotent (RFC 10008 §2), so identical calls must stay |
| 108 | + // identical on the wire: the client may not add per-request state of its own, and it |
| 109 | + // may not consume or mutate the caller’s `init`. |
| 110 | + describe("idempotency", () => { |
| 111 | + test("repeated identical calls produce identical requests", async () => { |
| 112 | + const observed: { method: string; url: string; headers: Record<string, string>; body: string }[] = []; |
| 113 | + const client = createObservedClient<paths>({}, async (req) => { |
| 114 | + observed.push({ |
| 115 | + method: req.method, |
| 116 | + url: req.url, |
| 117 | + headers: headersToObj(req.headers), |
| 118 | + body: await req.text(), |
| 119 | + }); |
| 120 | + return Response.json({}); |
| 121 | + }); |
| 122 | + |
| 123 | + const init = { |
| 124 | + params: { path: { id: 123 } }, |
| 125 | + body: { ids: [1, 2, 3] }, |
| 126 | + }; |
| 127 | + |
| 128 | + // reuse the exact same init object, to assert it is not consumed or mutated |
| 129 | + await client.QUERY("/resources/{id}", init); |
| 130 | + await client.QUERY("/resources/{id}", init); |
| 131 | + |
| 132 | + expect(observed).toHaveLength(2); |
| 133 | + expect(observed[1]).toEqual(observed[0]); |
| 134 | + expect(init).toEqual({ params: { path: { id: 123 } }, body: { ids: [1, 2, 3] } }); |
| 135 | + }); |
| 136 | + |
| 137 | + test("is safe: no request body is read or replayed across calls", async () => { |
| 138 | + // a request body may only be consumed once, so each call must build its own Request |
| 139 | + const bodies: string[] = []; |
| 140 | + const client = createObservedClient<paths>({}, async (req) => { |
| 141 | + bodies.push(await req.text()); |
| 142 | + return Response.json({}); |
| 143 | + }); |
| 144 | + |
| 145 | + await client.QUERY("/resources/{id}", { params: { path: { id: 1 } }, body: { ids: [1] } }); |
| 146 | + await client.QUERY("/resources/{id}", { params: { path: { id: 1 } }, body: { ids: [1] } }); |
| 147 | + |
| 148 | + expect(bodies).toEqual([JSON.stringify({ ids: [1] }), JSON.stringify({ ids: [1] })]); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + test("works with the path based client", async () => { |
| 153 | + let method = ""; |
| 154 | + let actualPathname = ""; |
| 155 | + const client = wrapAsPathBasedClient<paths>( |
| 156 | + createObservedClient<paths>({}, async (req) => { |
| 157 | + method = req.method; |
| 158 | + actualPathname = new URL(req.url).pathname; |
| 159 | + return Response.json({}); |
| 160 | + }), |
| 161 | + ); |
| 162 | + |
| 163 | + await client["/resources/{id}"].QUERY({ |
| 164 | + params: { path: { id: 123 } }, |
| 165 | + body: { ids: [1, 2, 3] }, |
| 166 | + }); |
| 167 | + |
| 168 | + expect(method).toBe("QUERY"); |
| 169 | + expect(actualPathname).toBe("/resources/123"); |
| 170 | + }); |
| 171 | +}); |
0 commit comments