forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart_test.ts
225 lines (199 loc) · 7.04 KB
/
multipart_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrowsAsync, test } from "./test_deps.ts";
import { httpErrors } from "./httpError.ts";
import { FormDataFile, FormDataReader } from "./multipart.ts";
import { equal, lookup, parse } from "./deps.ts";
import { stripEol } from "./util.ts";
// const decoder = new TextDecoder();
const encoder = new TextEncoder();
const fixtureContentType = `multipart/form-data; boundary=OAK-SERVER-BOUNDARY`;
const fixture = `leading to be ignored
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="id"
555
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="title"
Hello World
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="author"
world, hello
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="fileb"; filename="mod2.ts"
Content-Type: video/mp2t
export { printHello } from "./print_hello.ts";
--OAK-SERVER-BOUNDARY--
trailing to be ignored
`;
const fixtureNoFields = `
--OAK-SERVER-BOUNDARY--
`;
const fixtureUtf8Filename = `
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="id"
555
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="filea"; filename="编写软件很难.ts"
Content-Type: video/mp2t
export { printHello } from "./print_hello.ts";
--OAK-SERVER-BOUNDARY--
`;
function createBody(value: string): Deno.Buffer {
return new Deno.Buffer(encoder.encode(value));
}
function createBodyFile(
name: string,
filename: string,
): [Uint8Array, Deno.Buffer] {
const fileData = Deno.readFileSync(filename);
const mediaType = lookup(filename);
const basename = parse(filename).base;
const pre = `
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="${name}"; filename="${basename}"
Content-Type: ${mediaType}
`;
const post = `\r\n--OAK-SERVER-BOUNDARY--\r\n`;
const buffer = new Deno.Buffer();
Deno.writeAllSync(buffer, encoder.encode(pre));
Deno.writeAllSync(buffer, fileData);
Deno.writeAllSync(buffer, encoder.encode(post));
return [fileData, buffer];
}
test({
name: "multipart - FormDataReader - .read() basic",
async fn() {
const body = createBody(fixture);
const fdr = new FormDataReader(fixtureContentType, body);
const actual = await fdr.read();
assertEquals(
actual.fields,
{ id: "555", title: "Hello World", author: "world, hello" },
);
assert(actual.files);
assertEquals(actual.files.length, 1);
assertEquals(actual.files[0].contentType, "video/mp2t");
assertEquals(actual.files[0].name, "fileb");
assertEquals(actual.files[0].originalName, "mod2.ts");
assert(actual.files[0].filename);
},
});
test({
name: "multipart - FormDataReader - .stream() basic",
async fn() {
const body = createBody(fixture);
const fdr = new FormDataReader(fixtureContentType, body);
const actual: [string, string | FormDataFile][] = [];
for await (const result of fdr.stream()) {
actual.push(result);
}
assertEquals(actual.length, 4);
assertEquals(
actual.map(([key]) => key),
["id", "title", "author", "fileb"],
);
assertEquals(
actual.map(([, value]) => typeof value),
["string", "string", "string", "object"],
);
},
});
test({
name: "multipart - FormDataReader - .stream() file default",
async fn() {
const [expected, body] = createBodyFile("fileA", "./fixtures/test.jpg");
const fdr = new FormDataReader(fixtureContentType, body);
const actual: [string, string | FormDataFile][] = [];
for await (const result of fdr.stream()) {
actual.push(result);
}
assertEquals(actual.length, 1);
assertEquals(actual[0][0], "fileA");
const [, actualItem] = actual[0];
assert(typeof actualItem === "object");
assertEquals(actualItem.content, undefined);
assertEquals(actualItem.contentType, "image/jpeg");
assertEquals(actualItem.name, "fileA");
assertEquals(actualItem.originalName, "test.jpg");
assert(actualItem.filename);
assert(actualItem.filename.endsWith(".jpeg"));
const actualFileData = await Deno.readFile(actualItem.filename);
assert(equal(stripEol(actualFileData), expected));
},
});
test({
name: "multipart - FormDataReader - .stream() file memory",
async fn() {
const [expected, body] = createBodyFile("fileA", "./fixtures/test.jpg");
const fdr = new FormDataReader(fixtureContentType, body);
const actual: [string, string | FormDataFile][] = [];
for await (const result of fdr.stream({ maxSize: 400000 })) {
actual.push(result);
}
assertEquals(actual.length, 1);
assertEquals(actual[0][0], "fileA");
const [, actualItem] = actual[0];
assert(typeof actualItem === "object");
assertEquals(actualItem.contentType, "image/jpeg");
assertEquals(actualItem.name, "fileA");
assertEquals(actualItem.originalName, "test.jpg");
assert(actualItem.content);
assert(equal(stripEol(actualItem.content), expected));
},
});
test({
name: "multipart - FormDataReader - .stream() file maxSize overflow",
async fn() {
const [expected, body] = createBodyFile("fileA", "./fixtures/test.jpg");
const fdr = new FormDataReader(fixtureContentType, body);
const actual: [string, string | FormDataFile][] = [];
for await (const result of fdr.stream({ maxSize: 100000 })) {
actual.push(result);
}
assertEquals(actual.length, 1);
assertEquals(actual[0][0], "fileA");
const [, actualItem] = actual[0];
assert(typeof actualItem === "object");
assertEquals(actualItem.content, undefined);
assertEquals(actualItem.contentType, "image/jpeg");
assertEquals(actualItem.name, "fileA");
assertEquals(actualItem.originalName, "test.jpg");
assert(actualItem.filename);
assert(actualItem.filename.endsWith(".jpeg"));
const actualFileData = await Deno.readFile(actualItem.filename);
assert(equal(stripEol(actualFileData), expected));
},
});
test({
name: "multipart - FormDataReader - .read() maxFileSize exceeded",
async fn() {
const [, body] = createBodyFile("fileA", "./fixtures/test.jpg");
const fdr = new FormDataReader(fixtureContentType, body);
await assertThrowsAsync(async () => {
await fdr.read({ maxFileSize: 100000 });
}, httpErrors.RequestEntityTooLarge);
},
});
test({
name: "multipart - FormDataReader - body with no fields",
async fn() {
const body = createBody(fixtureNoFields);
const fdr = new FormDataReader(fixtureContentType, body);
const value = await fdr.read();
assertEquals(Object.keys(value.fields).length, 0);
assertEquals(value.files, undefined);
},
});
test({
name: "multipart - FormDataReader - body with mbc filename part",
async fn() {
const body = createBody(fixtureUtf8Filename);
const fdr = new FormDataReader(fixtureContentType, body);
const actual = await fdr.read();
assertEquals(actual.fields, { id: "555" });
assert(actual.files);
assertEquals(actual.files.length, 1);
assertEquals(actual.files[0].contentType, "video/mp2t");
assertEquals(actual.files[0].name, "filea");
assertEquals(actual.files[0].originalName, "编写软件很难.ts");
},
});