deno.land / x / servest@v1.3.4 / body_parser_test.ts

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
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
// 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 { createBodyParser, parserMultipartRequest } from "./body_parser.ts";import { exists as fsExists } from "./vendor/https/deno.land/std/fs/exists.ts";import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";import { group } from "./_test_util.ts";import { StringReader } from "./vendor/https/deno.land/std/io/readers.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")!; assert(!Array.isArray(mfile)); assertEquals(mfile.filename, "README.md"); assert(mfile.tempfile !== undefined, "temp file should be created"); await m.removeAll(); assertEquals(await fsExists(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", ); });});
group("bodyParser", ({ test }) => { test("text()", async () => { const s = "denoland"; const r = new StringReader(s); const br = createBodyParser({ reader: r, contentType: "text/plain", }); assertEquals(await br.text(), s); }); test("text() should return empty string if body is empty", async () => { const s = ""; const r = new StringReader(s); const br = createBodyParser({ reader: r, contentType: "text/plain" }); assertEquals(await br.text(), ""); assertEquals(await br.text(), ""); }); test("json()", async () => { const j = { deno: "land" }; const s = JSON.stringify(j); const r = new StringReader(s); const br = createBodyParser({ reader: r, contentType: "application/json" }); assertEquals(await br.json(), j); assertEquals(await br.json(), j); }); test("json() should throw if input is invalid", async () => { const j = `{ deno: "land" `; const r = new StringReader(j); const br = createBodyParser({ reader: r, contentType: "application/json" }); await assertThrowsAsync( async () => { await br.json(); }, SyntaxError, "JSON", ); }); test("arrayBuffer()", async () => { const bin = new Buffer(new Uint8Array([0, 1, 2, 3])); const br = createBodyParser( { reader: bin, contentType: "application/octet-stream" }, ); assertEquals([...(await br.arrayBuffer()).values()], [0, 1, 2, 3]); assertEquals([...(await br.arrayBuffer()).values()], [0, 1, 2, 3]); }); test("formData(), urlencoded", async () => { const s = "deno=land&での=らんど&👉=🦕"; const e = encodeURIComponent(s); const r = new StringReader(e); const br = createBodyParser( { reader: r, contentType: "application/x-www-form-urlencoded" }, ); const f = await br.formData(); assertEquals(f.value("deno"), "land"); assertEquals(f.value("での"), "らんど"); assertEquals(f.value("👉"), "🦕"); }); test("formData() should throw if invalid content type", async () => { const br = createBodyParser( { reader: new StringReader("deno=land"), contentType: "text/plain" }, ); await assertThrowsAsync( async () => { await br.formData(); }, Error, "request is not multipart/form-data nor application/x-www-form-urlencoded", ); }); test("multi transforming", async () => { const s = `{"deno": "land"}`; const encoder = new TextEncoder(); const br = createBodyParser( { reader: new StringReader(s), contentType: "text/plain" }, ); assertEquals(await br.arrayBuffer(), encoder.encode(s)); assertEquals(await br.text(), s); assertEquals(await br.json(), JSON.parse(s)); });});
servest

Version Info

Tagged at
2 years ago