1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
import { describe, expect, it } from "vitest";
import { recordFetch } from "./record.js";
import type { FetchLike, HttpExchangeFixture } from "./types.js";
function fakeResponse(body: string, init?: ResponseInit): Response {
return new Response(body, {
status: 200,
statusText: "OK",
headers: { "content-type": "application/json" },
...init,
});
}
describe("recordFetch", () => {
it("onExchange receives fixture matching request+response", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () => fakeResponse('{"ok":true}');
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
await recorded("https://api.example.com/v1/chat", {
method: "POST",
headers: { "content-type": "application/json" },
body: '{"prompt":"hi"}',
});
expect(fixtures).toHaveLength(1);
const fx = fixtures[0] as HttpExchangeFixture;
expect(fx.request.method).toBe("POST");
expect(fx.request.url).toBe("https://api.example.com/v1/chat");
expect(fx.request.headers["content-type"]).toBe("application/json");
expect(fx.request.body).toBe('{"prompt":"hi"}');
expect(fx.response.status).toBe(200);
expect(fx.response.statusText).toBe("OK");
expect(fx.response.headers["content-type"]).toBe("application/json");
expect(fx.response.body).toBe('{"ok":true}');
});
it("response returned to caller is still fully readable after recording", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () => fakeResponse("full body content");
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
const res = await recorded("https://x");
expect(await res.text()).toBe("full body content");
});
it("response body can be read even after fixture is captured", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () => fakeResponse("stream data");
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
const res = await recorded("https://x");
expect(fixtures[0]?.response.body).toBe("stream data");
expect(await res.text()).toBe("stream data");
});
it("captures Request object input with method", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () => fakeResponse("ok");
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
const req = new Request("https://from-req.com/path", { method: "PUT" });
await recorded(req);
expect(fixtures[0]?.request.url).toBe("https://from-req.com/path");
expect(fixtures[0]?.request.method).toBe("PUT");
});
it("defaults method to GET", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () => fakeResponse("ok");
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
await recorded("https://x");
expect(fixtures[0]?.request.method).toBe("GET");
});
it("captures null request body", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () => fakeResponse("ok");
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
await recorded("https://x", { method: "GET" });
expect(fixtures[0]?.request.body).toBeNull();
});
it("captures response error status", async () => {
const fixtures: HttpExchangeFixture[] = [];
const fakeFetch: FetchLike = async () =>
new Response('{"error":"bad request"}', {
status: 400,
statusText: "Bad Request",
});
const recorded = recordFetch(fakeFetch, (fx) => fixtures.push(fx));
const res = await recorded("https://x");
expect(fixtures[0]?.response.status).toBe(400);
expect(res.status).toBe(400);
});
it("passes through to real fetch", async () => {
let called = false;
const fakeFetch: FetchLike = async () => {
called = true;
return new Response("from fake", { status: 201 });
};
const recorded = recordFetch(fakeFetch, () => {});
const res = await recorded("https://x");
expect(called).toBe(true);
expect(res.status).toBe(201);
});
});
|