-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.test.ts
76 lines (61 loc) · 2.74 KB
/
mod.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
import { assertEquals } from "./deps.ts";
import { isCacheStale } from "./mod.js";
// isCacheStale tests
Deno.test("isCacheStale - cache is stale when no Cache-Control header is present", () => {
const response = new Response("");
response.headers.append("Date", new Date().toUTCString());
const result = isCacheStale(response.headers);
assertEquals(result, true);
});
Deno.test("isCacheStale - cache is stale when no Date header is present", () => {
const response = new Response("");
response.headers.append("Cache-Control", 'max-age=86400"');
const result = isCacheStale(response.headers);
assertEquals(result, true);
});
Deno.test("isCacheStale - cache is stale when max-age is zero", () => {
const response = new Response("", {
headers: {
"Cache-Control": "max-age=0",
"Date": new Date().toUTCString(),
},
});
const result = isCacheStale(response.headers);
assertEquals(result, true);
});
Deno.test("isCacheStale - cache is fresh when max-age is positive and cache time is within max-age", () => {
const cache_time = new Date();
cache_time.setSeconds(cache_time.getSeconds() - 5);
const response = new Response("");
response.headers.append("Cache-Control", "max-age=10");
response.headers.append("Date", cache_time.toUTCString());
const result = isCacheStale(response.headers);
assertEquals(result, false);
});
Deno.test("isCacheStale - cache is fresh when response is immutable", () => {
const cache_time = new Date();
cache_time.setSeconds(cache_time.getSeconds() - 5);
const response = new Response("");
response.headers.append("Cache-Control", "max-age=10, immutable");
response.headers.append("Date", cache_time.toUTCString());
const result = isCacheStale(response.headers);
assertEquals(result, false);
});
Deno.test("isCacheStale - cache is stale when max-age is positive and cache time is past max-age", () => {
const cache_time = new Date();
cache_time.setSeconds(cache_time.getSeconds() - 15);
const response = new Response("");
response.headers.append("Cache-Control", "max-age=10");
response.headers.append("Date", cache_time.toUTCString());
const result = isCacheStale(response.headers);
assertEquals(result, true);
});
Deno.test("isCacheStale - cache is stale with max-stale directive applied and time beyond max-age and max-stale", () => {
const cache_time = new Date();
cache_time.setSeconds(cache_time.getSeconds() - 35);
const response = new Response("");
response.headers.append("Cache-Control", "max-age=10, max-stale=20");
response.headers.append("Date", cache_time.toUTCString());
const result = isCacheStale(response.headers);
assertEquals(result, true);
});