-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.ts
106 lines (95 loc) · 2.6 KB
/
middleware_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
import { coop } from "./middleware.ts";
import {
assert,
assertThrows,
CrossOriginOpenerPolicyValue,
describe,
equalsResponse,
it,
PolicyHeader,
} from "./_dev_deps.ts";
describe("coop", () => {
it("should return same response if the response include the header", async () => {
const middleware = coop();
const initResponse = new Response(null, {
headers: {
[PolicyHeader.CrossOriginOpenerPolicy]: "",
},
});
const response = await middleware(
new Request("test:"),
() => initResponse,
);
assert(response === initResponse);
});
it("should return response what include coop header and the value is same-origin by default", async () => {
const middleware = coop();
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(equalsResponse(
response,
new Response(null, {
headers: {
[PolicyHeader.CrossOriginOpenerPolicy]:
CrossOriginOpenerPolicyValue.SameOrigin,
},
}),
));
});
it("should change coop header via arg", async () => {
const middleware = coop({
policy: CrossOriginOpenerPolicyValue.SameOriginAllowPopups,
});
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(equalsResponse(
response,
new Response(null, {
headers: {
[PolicyHeader.CrossOriginOpenerPolicy]:
CrossOriginOpenerPolicyValue.SameOriginAllowPopups,
},
}),
));
});
it("should add report-to param via endpoint", async () => {
const reportTo = "default";
const middleware = coop({ reportTo });
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(equalsResponse(
response,
new Response(null, {
headers: {
[PolicyHeader.CrossOriginOpenerPolicy]:
`${CrossOriginOpenerPolicyValue.SameOrigin};report-to=${reportTo}`,
},
}),
));
});
it("should change to report only header", async () => {
const middleware = coop({ reportOnly: true });
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(equalsResponse(
response,
new Response(null, {
headers: {
[PolicyHeader.CrossOriginOpenerPolicyReportOnly]:
CrossOriginOpenerPolicyValue.SameOrigin,
},
}),
));
});
it("should throw error if the Cross origin opener policy is invalid", () => {
assertThrows(() => coop({ reportTo: "?" }));
});
});