-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
body_parser_test.ts
66 lines (65 loc) 路 1.99 KB
/
body_parser_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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.
import {
MultipartWriter,
} from "./vendor/https/deno.land/std/mime/multipart.ts";
import {
assert,
assertEquals,
assertThrowsAsync,
} from "./vendor/https/deno.land/std/testing/asserts.ts";
import { parserMultipartRequest } from "./body_parser.ts";
import * as fs from "./vendor/https/deno.land/std/fs/mod.ts";
import Buffer = Deno.Buffer;
import { group } from "./test_util.ts";
group("multipart", ({ test }) => {
test("basic", async () => {
const buf = new Buffer();
const w = new MultipartWriter(buf);
await w.writeField("deno", "land");
const f = await Deno.open("./README.md");
await w.writeFile("file", "README.md", f);
await w.close();
f.close();
const m = await parserMultipartRequest(
{
headers: new Headers({
"content-type": w.formDataContentType(),
}),
body: buf,
},
1000,
);
assertEquals(m.value("hoge"), undefined);
assertEquals(m.value("deno"), "land");
const mfile = m.file("file")!;
assertEquals(mfile.filename, "README.md");
assert(mfile.tempfile !== undefined, "temp file should be created");
await m.removeAll();
assertEquals(await fs.exists(mfile.tempfile!), false);
});
test("should throw if content-type is invalid", async () => {
const body = new Buffer();
await assertThrowsAsync(async () => {
await parserMultipartRequest({
headers: new Headers(),
body,
});
}, Error, "is not multipart");
await assertThrowsAsync(async () => {
await parserMultipartRequest({
headers: new Headers({
"content-type": "application/json",
}),
body,
});
}, Error, "is not multipart");
await assertThrowsAsync(async () => {
await parserMultipartRequest({
headers: new Headers({
"content-type": "multipart/form-data; ",
}),
body,
});
}, Error, "doesn't have boundary");
});
});