-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.ts
167 lines (150 loc) · 3.71 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
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
import { normalizeFeatures, permissionsPolicy } from "./middleware.ts";
import {
assert,
assertEquals,
assertIsError,
assertThrows,
describe,
equalsResponse,
Header,
it,
type PermissionsPolicyFeatures,
type PolicyControlledFeatures,
} from "./_dev_deps.ts";
describe("normalizeFeatures", () => {
it("should return field name and value", () => {
const table: [PolicyControlledFeatures, PermissionsPolicyFeatures][] = [
[
{},
{},
],
[
{ accelerometer: "*", webShare: ["*"] },
{ accelerometer: "*", "web-share": ["*"] },
],
[
{ accelerometer: undefined },
{},
],
[
{
accelerometer: [],
ambientLightSensor: [],
autoplay: [],
battery: [],
bluetooth: [],
browsingTopics: [],
camera: [],
chUa: [],
chUaArch: [],
chUaBitness: [],
chUaFullVersion: [],
},
{
accelerometer: [],
"ambient-light-sensor": [],
autoplay: [],
battery: [],
bluetooth: [],
"browsing-topics": [],
camera: [],
"ch-ua": [],
"ch-ua-arch": [],
"ch-ua-bitness": [],
"ch-ua-full-version": [],
},
],
];
table.forEach(([input, expected]) => {
assertEquals(normalizeFeatures(input), expected);
});
});
});
describe("permissionsPolicy", () => {
it("should return same response if the response include header", async () => {
const middleware = permissionsPolicy({});
const initResponse = new Response(null, {
headers: {
[Header.PermissionsPolicy]: "",
},
});
const response = await middleware(new Request("test:"), () => initResponse);
assert(response === initResponse);
});
it("should return response what include Permissions-Policy header", async () => {
const middleware = permissionsPolicy({});
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(
await equalsResponse(
response,
new Response(null, {
headers: {
[Header.PermissionsPolicy]: ``,
},
}),
true,
),
);
});
it("should return response what include Permissions-Policy-Report-Only header", async () => {
const middleware = permissionsPolicy(
{ autoplay: "*" },
{
reportOnly: true,
reportTo: "default",
},
);
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(
await equalsResponse(
response,
new Response(null, {
headers: {
[Header.PermissionsPolicyReportOnly]:
`autoplay=*, report-to=default`,
},
}),
true,
),
);
});
it("should throw error if the features include invalid value", () => {
const table: PolicyControlledFeatures[] = [
{ accelerometer: "" },
{ chUaModel: [""] },
];
table.forEach((input) => {
assertThrows(() => permissionsPolicy(input));
});
});
it("should be error message if the feature value is invalid", () => {
let err;
try {
permissionsPolicy({ accelerometer: "" });
} catch (e) {
err = e;
} finally {
assertIsError(err, Error, `Invalid URL: ''`);
}
});
it("should be error message if the reportTo is invalid", () => {
let err;
try {
permissionsPolicy({}, { reportTo: "<invalid>" });
} catch (e) {
err = e;
} finally {
assertIsError(
err,
Error,
`invalid <sf-token> format. Token { "<invalid>" }`,
);
}
});
});