-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_test.ts
110 lines (92 loc) · 3 KB
/
transform_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
import {
assert,
assertEquals,
assertSpyCalls,
describe,
equalsResponse,
it,
RepresentationHeader,
spy,
} from "./_dev_deps.ts";
import { withEtag } from "./transform.ts";
describe("withEtag", () => {
it("should return with etag header field if the response does not have etag field", async () => {
const digest = spy(() => `""` as const);
const initResponse = new Response("ok");
const response = await withEtag(initResponse, digest);
assertSpyCalls(digest, 1);
assert(
await equalsResponse(
response,
new Response("ok", { headers: { [RepresentationHeader.ETag]: `""` } }),
true,
),
);
});
it("should process custom digest what return strong etag object", async () => {
const digest = spy(() => ({ weak: false, tag: "abc" }));
const initResponse = new Response("ok");
const response = await withEtag(initResponse, digest);
assertSpyCalls(digest, 1);
assert(
await equalsResponse(
response,
new Response("ok", {
headers: {
[RepresentationHeader.ETag]: `"abc"`,
},
}),
true,
),
);
});
it("should process custom digest what return weak etag object", async () => {
const digest = spy(() => ({ weak: true, tag: "abc" }));
const initResponse = new Response("ok");
const response = await withEtag(initResponse, digest);
assertSpyCalls(digest, 1);
assert(
await equalsResponse(
response,
new Response("ok", {
headers: {
[RepresentationHeader.ETag]: `W/"abc"`,
},
}),
true,
),
);
});
it("should return same response if the response status code is not 2xx", async () => {
const initResponse = new Response("", { status: 404 });
const digest = spy(() => `""` as const);
const response = await withEtag(initResponse, digest);
assertSpyCalls(digest, 0);
assertEquals(initResponse, response);
});
it("should return same response if the response body does not exist", async () => {
const initResponse = new Response();
const digest = spy(() => `""` as const);
const response = await withEtag(initResponse, digest);
assertSpyCalls(digest, 0);
assertEquals(initResponse, response);
});
it("should return same response if the response has been read", async () => {
const initResponse = new Response("ok");
const digest = spy(() => `""` as const);
await initResponse.text();
const response = await withEtag(initResponse, digest);
assert(initResponse.bodyUsed);
assertSpyCalls(digest, 0);
assertEquals(initResponse, response);
});
it("should return same response if the response has etag header", async () => {
const initResponse = new Response("ok", {
headers: { [RepresentationHeader.ETag]: "" },
});
const digest = spy(() => `""` as const);
const response = await withEtag(initResponse, digest);
assertSpyCalls(digest, 0);
assertEquals(initResponse, response);
});
});