forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers_test.ts
108 lines (92 loc) · 2.79 KB
/
headers_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
// Copyright 2018-2023 the oak authors. All rights reserved. MIT license.
import { assertEquals, assertRejects, Buffer } from "./test_deps.ts";
import { BufReader } from "./buf_reader.ts";
import { readHeaders, toParamRegExp, unquote } from "./headers.ts";
const { test } = Deno;
test({
name: "headers - toParamRegExp()",
fn() {
assertEquals(`foo=bar`.match(toParamRegExp("foo"))![1], "bar");
assertEquals(`foo="bar"`.match(toParamRegExp("foo"))![1], `"bar"`);
assertEquals(`bar="baz"; foo=bar`.match(toParamRegExp("foo"))![1], "bar");
assertEquals(`foobar=bar`.match(toParamRegExp("foo")), null);
assertEquals(`foo1=bar`.match(toParamRegExp("foo[0-9]"))![1], "bar");
},
});
const fixture = `Content-Disposition: form-data; name="foo"; filename="foo.ts"
Content-Type: application/typescript
console.log("hello");
`;
const malformedFixture =
`Content-Disposition: form-data; name="foo"; filename="foo.ts"
Content-Type: application/typescript
foobar
console.log("hello");
`;
const invalidHeaderKeyFixture =
`Content-Disposition: form-data; name="foo"; filename="foo.ts"
Content-Type: application/typescript
:bar
console.log("hello");
`;
const unexpectedEndFixture =
`Content-Disposition: form-data; name="foo"; filename="foo.ts"
Content-Type: application/typescript
`;
test({
name: "headers - readHeaders()",
async fn() {
const body = new BufReader(
new Buffer(new TextEncoder().encode(fixture)),
);
const actual = await readHeaders(body);
assertEquals(
actual["content-disposition"],
`form-data; name="foo"; filename="foo.ts"`,
);
assertEquals(actual["content-type"], `application/typescript`);
assertEquals(
new TextDecoder().decode((await body.readLine())?.bytes),
`console.log("hello");`,
);
},
});
test({
name: "headers - readHeaders() Malformed header",
async fn() {
const body = new BufReader(
new Buffer(new TextEncoder().encode(malformedFixture)),
);
await assertRejects(() => readHeaders(body), Error, "Malformed header:");
},
});
test({
name: "headers - readHeaders() Invalid header key",
async fn() {
const body = new BufReader(
new Buffer(new TextEncoder().encode(invalidHeaderKeyFixture)),
);
await assertRejects(() => readHeaders(body), Error, "Invalid header key");
},
});
test({
name: "headers - readHeaders() Unexpected end of body reached",
async fn() {
const body = new BufReader(
new Buffer(new TextEncoder().encode(unexpectedEndFixture)),
);
await assertRejects(
() => readHeaders(body),
Error,
"Unexpected end of body reached.",
);
},
});
test({
name: "headers - unquote()",
fn() {
assertEquals(unquote("bar"), "bar");
assertEquals(unquote(`"bar"`), "bar");
assertEquals(unquote(`\"bar\"`), "bar");
},
});