-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.test.ts
227 lines (184 loc) Β· 6.23 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { describe, expect, test } from "vitest";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Either from "effect/Either";
import * as Fiber from "effect/Fiber";
import { pipe } from "effect/Function";
import * as TestClock from "effect/TestClock";
import * as TestContext from "effect/TestContext";
import { TimeoutException } from "effect/Cause";
import * as Adapter from "../src/Adapters/Fetch.js";
import * as Http from "../src/Client.js";
import * as Fetch from "../src/Fetch.js";
import * as Interceptor from "../src/Interceptor.js";
import * as BaseUrl from "../src/Interceptors/Url.js";
import { json } from "../src/internal/body.js";
import * as Response from "../src/Response.js";
const base_url = "https://reqres.in/api";
const base_url_interceptor = BaseUrl.Url(base_url);
test("should make client with http methods", async () => {
const interceptors = Interceptor.of(base_url_interceptor);
const adapter = pipe(
Interceptor.make(interceptors),
Interceptor.provide(Adapter.fetch),
Fetch.effect
);
const result = await pipe(
Http.get("/users/2"),
Effect.flatMap(Response.json),
Effect.provide(adapter),
Effect.runPromise
);
expect(result.data.id).toBe(2);
});
test("should make client with base URL for every request", async () => {
const program = Effect.gen(function* () {
const client = yield* Http.Client;
return yield* Effect.flatMap(client.get("/users/2"), Response.json);
});
const client = Http.create({ url: base_url, adapter: Adapter.fetch });
const result = await Effect.runPromise(
Effect.provide(program, Http.layer(client))
);
expect(result.data.id).toBe(2);
});
test("should make client with interceptors", async () => {
const program = Effect.gen(function* () {
const client = yield* Http.Client;
return yield* Effect.flatMap(client.get("/users/2"), Response.json);
});
const interceptors = Interceptor.of(base_url_interceptor);
const client = Http.create({ interceptors, adapter: Adapter.fetch });
const result = await program.pipe(
Effect.provide(Http.layer(client)),
Effect.runPromise
);
expect(result.data.id).toBe(2);
});
test("should attach JSON body and headers", async () => {
const spy = Effect.flatMap(Interceptor.Chain, (chain) => {
const headers = new Headers(chain.request.init?.headers);
expect(chain.request.init?.body).toBe('{"name":"morpheus","job":"leader"}');
expect(headers.get("Content-Type")).toBe("application/json");
expect(headers.has("Content-Length")).toBeTruthy();
return chain.proceed(chain.request);
});
const interceptors = Interceptor.of(base_url_interceptor).pipe(
Interceptor.add(spy)
);
const adapter = pipe(
Interceptor.make(interceptors),
Interceptor.provide(Adapter.fetch),
Fetch.effect
);
const result = await pipe(
Http.post("/users", json({ name: "morpheus", job: "leader" })),
Effect.flatMap(Response.json),
Effect.provide(adapter),
Effect.runPromise
);
expect(result).toMatchObject({
name: "morpheus",
job: "leader",
});
});
test("should attach JSON body and headers with custom headers", async () => {
const spy = Effect.flatMap(Interceptor.Chain, (chain) => {
const headers = new Headers(chain.request.init?.headers);
expect(headers.get("X-API-Key")).toBe("Bearer <APIKEY>");
return chain.proceed(chain.request);
});
const interceptors = Interceptor.of(base_url_interceptor).pipe(
Interceptor.add(spy)
);
const adapter = pipe(
Interceptor.make(interceptors),
Interceptor.provide(Adapter.fetch),
Fetch.effect
);
const result = await pipe(
Http.post("/users", {
headers: { "X-API-Key": "Bearer <APIKEY>" },
body: json({ name: "morpheus", job: "leader" }),
}),
Effect.flatMap(Response.json),
Effect.provide(adapter),
Effect.runPromise
);
expect(result).toMatchObject({
name: "morpheus",
job: "leader",
});
});
describe("timeout", () => {
const program = Effect.gen(function* () {
const client = yield* Http.Client;
return yield* client.get("/users/2?delay=10");
});
const clock = Effect.gen(function* () {
const chain = yield* Interceptor.Chain;
const res = yield* Effect.fork(chain.proceed(chain.request));
yield* TestClock.adjust(Duration.seconds(10));
return yield* Fiber.join(res);
});
test("with number timeout", async () => {
const client = Http.create({
timeout: 100,
url: base_url,
adapter: Adapter.fetch,
interceptors: Interceptor.of(clock),
});
const result = await program.pipe(
Effect.provide(Http.layer(client)),
Effect.provide(TestContext.TestContext),
Effect.either,
Effect.runPromise
);
expect(Either.isLeft(result)).toBeTruthy();
expect((result as Either.Left<any, any>).left).instanceOf(TimeoutException);
});
test("with Duration timeout", async () => {
const client = Http.create({
url: base_url,
adapter: Adapter.fetch,
timeout: Duration.seconds(5),
interceptors: Interceptor.of(clock),
});
const result = await program.pipe(
Effect.provide(Http.layer(client)),
Effect.provide(TestContext.TestContext),
Effect.either,
Effect.runPromise
);
expect(Either.isLeft(result)).toBeTruthy();
expect((result as Either.Left<any, any>).left).instanceOf(TimeoutException);
});
});
describe("method", () => {
const check = (method: string) => {
return Effect.flatMap(Interceptor.Chain, (chain) => {
expect(chain.request.init?.method).toBe(method);
return Effect.succeed(new globalThis.Response(""));
});
};
test("POST", async () => {
const interceptors = Interceptor.of(check("POST"));
const interceptor = pipe(
Interceptor.make(interceptors),
Interceptor.provide(Adapter.fetch)
);
await Effect.runPromise(
Effect.provide(Http.post("/users/2"), Fetch.effect(interceptor))
);
});
test("HEAD", async () => {
const interceptors = Interceptor.of(check("HEAD"));
const interceptor = pipe(
Interceptor.make(interceptors),
Interceptor.provide(Adapter.fetch)
);
await Effect.runPromise(
Effect.provide(Http.head("/users/2"), Fetch.effect(interceptor))
);
});
});