-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.test.ts
113 lines (91 loc) · 3.68 KB
/
fetcher.test.ts
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
113
import { assertEquals, assertRejects } from "@std/assert";
import { afterAll, afterEach, beforeAll, describe, it } from "@std/testing/bdd";
import { assertSpyCalls, spy } from "@std/testing/mock";
import * as mf from "mock_fetch";
import { DefaultFetcher } from "./fetcher.ts";
describe("DefaultFetcher", () => {
beforeAll(() => {
mf.install();
});
afterAll(() => {
mf.uninstall();
});
afterEach(() => {
mf.reset();
});
describe("fetch", () => {
it("can fetch without parameters and body", async () => {
const handler = spy((_req: Request) => new Response(JSON.stringify({ id: 42 })));
mf.mock("GET@/api/v0/dummy", handler);
const fetcher = new DefaultFetcher("deadbeef");
const res = await fetcher.fetch("GET", "/api/v0/dummy");
assertSpyCalls(handler, 1);
const req = handler.calls[0].args[0];
assertEquals(req.method, "GET");
assertEquals(req.url, "https://api.mackerelio.com/api/v0/dummy");
assertEquals(req.headers.get("X-Api-Key"), "deadbeef");
assertEquals(req.body, null);
assertEquals(res, { id: 42 });
});
it("can fetch with parameters", async () => {
const handler = spy((_req: Request) => new Response(JSON.stringify({ id: 42 })));
mf.mock("GET@/api/v0/dummy", handler);
const fetcher = new DefaultFetcher("deadbeef");
const res = await fetcher.fetch("GET", "/api/v0/dummy", {
params: new URLSearchParams({ foo: "bar" }),
});
assertSpyCalls(handler, 1);
const req = handler.calls[0].args[0];
assertEquals(req.method, "GET");
assertEquals(req.url, "https://api.mackerelio.com/api/v0/dummy?foo=bar");
assertEquals(req.headers.get("X-Api-Key"), "deadbeef");
assertEquals(req.body, null);
assertEquals(res, { id: 42 });
});
it("can fetch with body", async () => {
const handler = spy((_req: Request) => new Response(JSON.stringify({ id: 42 })));
mf.mock("POST@/api/v0/dummy", handler);
const fetcher = new DefaultFetcher("deadbeef");
const res = await fetcher.fetch("POST", "/api/v0/dummy", {
body: { foo: "bar" },
});
assertSpyCalls(handler, 1);
const req = handler.calls[0].args[0];
assertEquals(req.method, "POST");
assertEquals(req.url, "https://api.mackerelio.com/api/v0/dummy");
assertEquals(req.headers.get("X-Api-Key"), "deadbeef");
assertEquals(req.headers.get("Content-Type"), "application/json");
assertEquals(await req.json(), { foo: "bar" });
assertEquals(res, { id: 42 });
});
it("allows to override the base URL", async () => {
const handler = spy((_req: Request) => new Response(JSON.stringify({ id: 42 })));
mf.mock("GET@/api/v0/dummy", handler);
const fetcher = new DefaultFetcher("deadbeef", {
base: "https://foo.example/",
});
const res = await fetcher.fetch("GET", "/api/v0/dummy");
assertSpyCalls(handler, 1);
const req = handler.calls[0].args[0];
assertEquals(req.method, "GET");
assertEquals(req.url, "https://foo.example/api/v0/dummy");
assertEquals(req.headers.get("X-Api-Key"), "deadbeef");
assertEquals(req.body, null);
assertEquals(res, { id: 42 });
});
it("rejects if API request was unsuccessful", async () => {
const handler = spy((_req: Request) =>
new Response(JSON.stringify({ error: "bad request" }), {
status: 400,
})
);
mf.mock("GET@/api/v0/dummy", handler);
const fetcher = new DefaultFetcher("deadbeef");
await assertRejects(
() => fetcher.fetch("GET", "/api/v0/dummy"),
Error,
"Failed to fetch GET /api/v0/dummy",
);
});
});
});