-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.ts
83 lines (71 loc) · 1.83 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
import { nel } from "./middleware.ts";
import {
assert,
assertIsError,
assertThrows,
describe,
equalsResponse,
Header,
it,
} from "./_dev_deps.ts";
import { NELPolicy } from "./mod.ts";
describe("nel", () => {
it("should return same response if the response include header", async () => {
const middleware = nel({ max_age: 0 });
const initResponse = new Response(null, {
headers: {
[Header.NEL]: "",
},
});
const response = await middleware(new Request("test:"), () => initResponse);
assert(response === initResponse);
});
it("should return response what include NEL header", async () => {
const middleware = nel({ max_age: 0 });
const response = await middleware(
new Request("test:"),
() => new Response(),
);
assert(
await equalsResponse(
response,
new Response(null, {
headers: {
[Header.NEL]: `{"max_age":0}`,
},
}),
true,
),
);
});
it("should throw error if the endpoints include invalid value", () => {
const table: NELPolicy[] = [
{ max_age: NaN },
{ max_age: 0, success_fraction: 1.1 },
{ max_age: 0, failure_fraction: -1.1 },
];
table.forEach((input) => {
assertThrows(() => nel(input));
});
});
it("should be error message if the value is not URI-reference", () => {
let err;
try {
nel({ max_age: NaN });
} catch (e) {
err = e;
} finally {
assertIsError(err, Error, `max_age must be non-negative integer. NaN`);
}
});
it("should be error message if the key is invalid", () => {
let err;
try {
nel({ max_age: 0, failure_fraction: NaN });
} catch (e) {
err = e;
} finally {
assertIsError(err, Error, `failure_fraction must be unit interval. NaN`);
}
});
});