-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.ts
150 lines (143 loc) · 4.54 KB
/
client_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { assertAlmostEquals, assertEquals, assertRejects } from "@std/assert";
import { startDummyServer } from "./_testutil.ts";
import { Client, RequestError } from "./client.ts";
Deno.test("Client", async (t) => {
const domain = "domain";
const token = "token";
await t.step("fetch method accesses DocBase API properly", async () => {
await using baseUrl = startDummyServer((req, _info) => {
assertEquals(req.headers.get("X-DocBaseToken"), token);
assertEquals(req.headers.get("X-Api-Version"), "2");
assertEquals(req.headers.get("Content-Type"), "application/json");
assertEquals(
new URL(req.url).pathname,
`/teams/${domain}/path/to/resource`,
);
return new Response("Ok");
});
const client = new Client(
{ domain, token },
{ baseUrl },
);
assertEquals(await (await client.fetch("path/to/resource")).text(), "Ok");
});
await t.step("request method accesses DocBase API properly", async () => {
await using baseUrl = startDummyServer((req, _info) => {
assertEquals(req.headers.get("X-DocBaseToken"), token);
assertEquals(req.headers.get("X-Api-Version"), "2");
assertEquals(req.headers.get("Content-Type"), "application/json");
assertEquals(
new URL(req.url).pathname,
`/teams/${domain}/path/to/resource`,
);
return new Response(JSON.stringify({
"status_text": "ok",
}));
});
const client = new Client(
{ domain, token },
{ baseUrl },
);
assertEquals(await client.request("path/to/resource"), {
statusText: "ok",
});
});
await t.step(
"request method accesses DocBase API properly (204)",
async () => {
await using baseUrl = startDummyServer((req, _info) => {
assertEquals(req.headers.get("X-DocBaseToken"), token);
assertEquals(req.headers.get("X-Api-Version"), "2");
assertEquals(req.headers.get("Content-Type"), "application/json");
assertEquals(
new URL(req.url).pathname,
`/teams/${domain}/path/to/resource`,
);
return new Response(null, { status: 204 });
});
const client = new Client(
{ domain, token },
{ baseUrl },
);
assertEquals(await client.request("path/to/resource"), undefined);
},
);
await t.step(
"request method accesses DocBase API properly (skipCaseTransform)",
async () => {
await using baseUrl = startDummyServer((req, _info) => {
assertEquals(req.headers.get("X-DocBaseToken"), token);
assertEquals(req.headers.get("X-Api-Version"), "2");
assertEquals(req.headers.get("Content-Type"), "application/json");
assertEquals(
new URL(req.url).pathname,
`/teams/${domain}/path/to/resource`,
);
return new Response(JSON.stringify({
"status_text": "ok",
}));
});
const client = new Client(
{ domain, token },
{ baseUrl },
);
assertEquals(
await client.request("path/to/resource", undefined, {
skipCaseTransform: true,
}),
{
status_text: "ok",
},
);
},
);
await t.step(
"request throws RequestError when API returns 400 Bad request",
async () => {
await using baseUrl = startDummyServer((_req, _info) => {
return new Response("Bad request", { status: 400 });
});
const client = new Client(
{ domain, token },
{ baseUrl },
);
await assertRejects(
() => client.request("path/to/resource"),
RequestError,
"Bad request",
);
},
);
await t.step(
"request waits X-RateLimit-Reset and retry the request whe API returns 429 Too many requests",
async () => {
let count = 0;
await using baseUrl = startDummyServer((_req, _info) => {
count++;
if (count === 1) {
return new Response("Too many requests", {
status: 429,
headers: new Headers({
"X-RateLimit-Reset": (Date.now() + 100).toString(),
}),
});
} else {
return new Response(JSON.stringify({
"status": "ok",
}));
}
});
const client = new Client(
{ domain, token },
{ baseUrl },
);
const start = performance.now();
assertEquals(await client.request("path/to/resource"), {
status: "ok",
});
const diff = performance.now() - start;
assertEquals(count, 2);
assertAlmostEquals(diff, 100, 10);
},
);
});