-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlcache.test.ts
365 lines (306 loc) · 9.37 KB
/
lcache.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import "@/types/fastify";
import type { RequestMethod } from "@/types/lcache";
import type { FastifyInstance } from "fastify";
import { getApp, spies } from "./helpers/lcache";
describe("Light Cache Fastify plugin", () => {
let app: FastifyInstance;
afterEach(async () => {
jest.clearAllMocks();
await app.close();
});
describe("Caching with default options", () => {
beforeEach(async () => {
app = await getApp();
});
it("Plugin should exist on app instance", () => {
expect(app.hasDecorator("lcache")).toBeTruthy();
});
it("should be possible to put/retrieve values from app instance", () => {
const strKey = "str";
const expectedStrValue = "data1";
const objKey = "obj";
const expectedObjValue = {
name: "Bob",
age: 23,
};
app.lcache.set(strKey, expectedStrValue);
app.lcache.set(objKey, expectedObjValue);
const actualStrValue = app.lcache.get(strKey);
expect(actualStrValue).toEqual(expectedStrValue);
const actualObjValue = app.lcache.get(objKey);
expect(actualObjValue).toEqual(actualObjValue);
});
it("should be possible to overwrite data by key", () => {
const key = "str";
const value1 = "data1";
const value2 = NaN;
app.lcache.set(key, value1);
app.lcache.set(key, value2);
expect(app.lcache.get(key)).toStrictEqual(value2);
});
it("cache should work", async () => {
const expectedValue = "pong";
const getPing = async () =>
app.inject({
method: "GET",
path: "/ping",
});
const res1 = await getPing();
expect(res1.body).toBe(expectedValue);
// reaches actual endpoint
expect(spies.getPing).toHaveBeenCalledTimes(1);
// cached data
const res2 = await getPing();
// expect endpoint no longer reachable, data coming from cache
expect(spies.getPing).toHaveBeenCalledTimes(1);
expect(res2.body).toBe(expectedValue);
});
it("cache should return same headers as the original request", async () => {
const getJson = async () =>
app.inject({
method: "GET",
path: "/json",
});
const res1 = await getJson();
const res2 = await getJson();
expect(res2.headers["content-type"]).toBe(res1.headers["content-type"]);
// endpoint reached only once
expect(spies.getJSON).toHaveBeenCalledTimes(1);
});
it("response should be cached separately for different query", async () => {
const res1 = await app.inject({
method: "GET",
path: "/date",
query: "asd",
});
const res2 = await app.inject({
method: "GET",
path: "/date",
query: "sdf",
});
expect(res1.body).not.toBe(res2.body);
// cache is not expected to be used
expect(spies.getDate).toHaveBeenCalledTimes(2);
});
});
describe("caching with custom options", () => {
beforeEach(async () => {
app = await getApp({
excludeRoutes: ["/date"],
statusesToCache: [200, 201],
methodsToCache: ["GET", "POST"],
});
});
it("response should be cached separately for different body", async () => {
const res1 = await app.inject({
method: "POST",
path: "/post",
body: {
data: "first-payload",
},
});
const res2 = await app.inject({
method: "POST",
path: "/post",
body: {
data: "second-payload",
},
});
expect(res1.body).not.toBe(res2.body);
// endpoint should be reached each time
expect(spies.postPost).toHaveBeenCalledTimes(2);
});
it("excluded routes should not be cached", async () => {
const res1 = await app.inject({
method: "GET",
path: "/date",
});
const res2 = await app.inject({
method: "GET",
path: "/date",
});
expect(res1.body).not.toBe(res2.body);
expect(spies.getDate).toHaveBeenCalledTimes(2);
});
it("PUT method should not be cached when only status code is 201", async () => {
const res1 = await app.inject({
method: "PUT",
path: "/put",
payload: {
data: "456",
},
});
const res2 = await app.inject({
method: "PUT",
path: "/put",
payload: {
data: "123",
},
});
expect(res1.body).not.toBe(res2.body);
expect(spies.putPut).toHaveBeenCalledTimes(2);
});
it("cache reset should work", async () => {
const getPing = async () =>
app.inject({
method: "GET",
path: "/ping",
});
const postPing = async () =>
app.inject({
method: "POST",
path: "/ping",
});
const dataKey1 = "someKey1";
const dataValue1 = "someValue1";
const dataKey2 = "someKey2";
const dataValue2 = [1, 2, 3, 4];
const dataKey3 = "someKey3";
const dataValue3 = "someValue3";
// add data to the cache via request
await postPing();
await getPing();
// manually set some values
app.lcache.set(dataKey1, dataValue1);
app.lcache.set(dataKey2, dataValue2);
app.lcache.set(dataKey3, dataValue3);
// 1 post + 1 get + 3 manual set
// remove specific key
app.lcache.reset(dataKey1);
app.lcache.reset([dataKey3]); // also test array
expect(app.lcache.has(dataKey1)).toBeFalsy();
expect(app.lcache.get(dataKey1)).toBeUndefined();
expect(app.lcache.get(dataKey3)).toBeUndefined();
// expect not removed data is still in the cache
expect(app.lcache.get(dataKey2)).toStrictEqual(dataValue2);
// prune all cached data
app.lcache.reset();
expect(app.lcache.has(dataKey2)).toBeFalsy();
expect(app.lcache.get(dataKey2)).toBeUndefined();
// check cached data via request
await getPing();
await postPing();
// the endpoints should be reached again
expect(spies.getPing).toHaveBeenCalledTimes(2);
expect(spies.postPing).toHaveBeenCalledTimes(2);
});
});
describe("Route caching", () => {
beforeEach(async () => {
app = await getApp({
excludeRoutes: ["/ping", "/api/adm*"],
includeRoutes: ["/ping", "/js*n", "/api/admin*", "/date/*"],
statusesToCache: [200, 201],
methodsToCache: ["GET", "POST", "PUT"],
});
});
it("should cache included route pattern", async () => {
await app.inject({
path: "/json",
});
await app.inject({
path: "/json",
});
expect(spies.getJSON).toHaveBeenCalledTimes(1);
});
it("shouldn't cache unspecified route in 'includeRoutes'", async () => {
await app.inject({
method: "put",
path: "/put",
});
await app.inject({
method: "put",
path: "/put",
});
expect(spies.putPut).toHaveBeenCalledTimes(2);
});
it("shouldn't cache included routes if excluded routes match", async () => {
await app.inject({
method: "POST",
path: "/ping",
});
await app.inject({
method: "POST",
path: "/ping",
});
expect(spies.postPing).toHaveBeenCalledTimes(2);
});
it("shouldn't cache excluded pattern", async () => {
await app.inject({
method: "POST",
path: "/api/admin",
});
await app.inject({
method: "POST",
path: "/api/admin",
});
expect(spies.postApiAdmin).toHaveBeenCalledTimes(2);
});
it("shouldn't cache route pattern with trailing slash", async () => {
await app.inject({
path: "/date",
});
await app.inject({
path: "/date",
});
expect(spies.getDate).toHaveBeenCalledTimes(2);
});
});
describe("Disabled lcache plugin", () => {
const methodsToCache: RequestMethod[] = ["get", "post", "delete"];
beforeEach(async () => {
app = await getApp({
excludeRoutes: [],
statusesToCache: [200, 201],
methodsToCache,
// disable cache programmatically
disableCache: true,
});
});
it.each(methodsToCache)(
"shouldn't reach %s /ping avoiding cache",
async (httpMethod) => {
await app.inject({
method: httpMethod,
path: "/ping",
payload: {
data: "456",
},
});
await app.inject({
method: httpMethod,
path: "/ping",
payload: {
data: "456",
},
});
const method = `${httpMethod}Ping` as keyof typeof spies;
// each time reach endpoint
expect(spies[method]).toHaveBeenCalledTimes(2);
}
);
});
describe("TTL", () => {
const msToWait = 3_000;
// convert to minutes for lcache usage
const ttlInMinutes = msToWait / 60_000;
beforeEach(() => {
// increase timeout of the tests
jest.setTimeout(msToWait * 3);
});
it("cached data should be removed after ttl - timeout", async () => {
app = await getApp({
ttlInMinutes,
});
const key = "someKey";
const value = "someValue";
app.lcache.set(key, value);
// wait increased ttl time
await new Promise((resolve) => {
setTimeout(resolve, msToWait);
});
expect(app.lcache.has(key)).toBeFalsy();
expect(app.lcache.get(key)).toBeUndefined();
});
});
});