-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.ts
185 lines (159 loc) · 4.45 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { reportingEndpoints, reportTo } from "./middleware.ts";
import {
assert,
assertIsError,
assertThrows,
describe,
equalsResponse,
it,
ReportingHeader,
} from "./_dev_deps.ts";
import { EndpointGroup } from "./types.ts";
describe("reportingEndpoints", () => {
it("should return same response if the response include header", async () => {
const middleware = reportingEndpoints({});
const initResponse = new Response(null, {
headers: {
[ReportingHeader.ReportingEndpoints]: "",
},
});
const response = await middleware(new Request("test:"), () => initResponse);
assert(response === initResponse);
});
it("should return response what include reporting-endpoints header", async () => {
const key = "default";
const endpoints = "https://test.test";
const middleware = reportingEndpoints({ [key]: endpoints });
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(
await equalsResponse(
response,
new Response(null, {
headers: {
[ReportingHeader.ReportingEndpoints]: `${key}="${endpoints}"`,
},
}),
true,
),
);
});
it("should throw error if the endpoints include invalid value", () => {
const table: (Record<string, string> | [string, string][])[] = [
{ "": "" },
{ "a": ":" },
[["a", ":"], ["", ""]],
];
table.forEach((input) => {
assertThrows(() => reportingEndpoints(input));
});
});
it("should be error message if the value is not URI-reference", () => {
let err;
try {
reportingEndpoints([["a", ":"], ["", ""]]);
} catch (e) {
err = e;
} finally {
assertIsError(err, Error, `invalid <URI-reference> format. ":"`);
}
});
it("should be error message if the key is invalid", () => {
let err;
try {
reportingEndpoints([["", ""]]);
} catch (e) {
err = e;
} finally {
assertIsError(err, Error, `invalid <key> format. key of ""`);
}
});
});
describe("reportTo", () => {
it("should return same response if the response include header", async () => {
const middleware = reportTo([]);
const initResponse = new Response(null, {
headers: { [ReportingHeader.ReportTo]: "" },
});
const response = await middleware(new Request("test:"), () => initResponse);
assert(response === initResponse);
});
it("should return response what include report-to header", async () => {
const middleware = reportTo([{
max_age: 86400,
endpoints: [{ url: "https://test.test" }],
}]);
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(
await equalsResponse(
response,
new Response(null, {
headers: {
[ReportingHeader.ReportTo]:
`{"max_age":86400,"endpoints":[{"url":"https://test.test"}]}`,
},
}),
true,
),
);
});
it("should throw error if the endpoint groups include invalid field", () => {
const table: EndpointGroup[][] = [
[{ max_age: NaN, endpoints: [] }],
[{ max_age: -1, endpoints: [] }],
[{ max_age: 1.1, endpoints: [] }],
[{ max_age: 0, endpoints: [{ url: "", priority: -1 }] }],
[{ max_age: 0, endpoints: [{ url: "", weight: -1 }] }],
];
table.forEach((input) => {
assertThrows(() => reportTo(input));
});
});
it("should be error message if the max_age is invalid", () => {
let err;
try {
reportTo([{ max_age: NaN, endpoints: [] }]);
} catch (e) {
err = e;
} finally {
assertIsError(
err,
Error,
"group.max_age must be non-negative integer. NaN",
);
}
});
it("should be error message if the endpoint.priority is invalid", () => {
let err;
try {
reportTo([{ max_age: 0, endpoints: [{ url: "", priority: NaN }] }]);
} catch (e) {
err = e;
} finally {
assertIsError(
err,
Error,
"endpoint.priority must be non-negative integer. NaN",
);
}
});
it("should be error message if the endpoint.weight is invalid", () => {
let err;
try {
reportTo([{ max_age: 0, endpoints: [{ url: "", weight: NaN }] }]);
} catch (e) {
err = e;
} finally {
assertIsError(
err,
Error,
"endpoint.weight must be non-negative integer. NaN",
);
}
});
});