forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart_test.ts
297 lines (263 loc) · 8.79 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2018-2023 the oak authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertRejects,
Buffer,
writeAllSync,
} from "./test_deps.ts";
import { FormDataFile, FormDataReader } from "./multipart.ts";
import { equals, errors, extname, parse, typeByExtension } from "./deps.ts";
import { isNode, stripEol } from "./util.ts";
const { test } = Deno;
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--
`;
const fixtureNoNewline = `
--OAK-SERVER-BOUNDARY
Content-Disposition: form-data; name="noNewline"; filename="noNewline.txt"
Content-Type: text/plain
555
--OAK-SERVER-BOUNDARY--
`;
function createBody(value: string): Buffer {
return new Buffer(encoder.encode(value));
}
function createBodyFile(
name: string,
filename: string,
contentType?: string,
): [Uint8Array, Buffer] {
const fileData = Deno.readFileSync(filename);
const mediaType = contentType ?? typeByExtension(extname(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 Buffer();
writeAllSync(buffer, encoder.encode(pre));
writeAllSync(buffer, fileData);
writeAllSync(buffer, encoder.encode(post));
return [fileData, buffer];
}
test({
name: "multipart - FormDataReader - .read() basic",
ignore: isNode(),
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",
ignore: isNode(),
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",
ignore: isNode(),
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(equals(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(equals(stripEol(actualItem.content), expected));
},
});
test({
name: "multipart - FormDataReader - .stream() file maxSize overflow",
ignore: isNode(),
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(equals(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 assertRejects(async () => {
await fdr.read({ maxFileSize: 100000 });
}, errors.RequestEntityTooLarge);
},
});
test({
name: "multipart - FormDataReader - .read() custom content type",
ignore: isNode(),
async fn() {
const [, body] = createBodyFile(
"fileA",
"./fixtures/test.txt",
"text/vnd.custom",
);
const fdr = new FormDataReader(fixtureContentType, body);
const actual = await fdr.read({
customContentTypes: { "text/vnd.custom": "txt" },
});
assertEquals(actual.files?.length, 1);
assertEquals(actual.files?.[0].contentType, "text/vnd.custom");
assert(actual.files?.[0].filename?.endsWith(".txt"));
},
});
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",
ignore: isNode(),
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");
},
});
test({
name:
"multipart - FormDataReader - .read() no extra CRLF at the end of result file if origin file doesn't end with newline",
ignore: isNode(),
async fn() {
const body = createBody(fixtureNoNewline);
const fdr = new FormDataReader(fixtureContentType, body);
const actual = await fdr.read();
assert(actual.files);
assertEquals(actual.files.length, 1);
assertEquals(actual.files[0].contentType, "text/plain");
assertEquals(actual.files[0].name, "noNewline");
assertEquals(actual.files[0].originalName, "noNewline.txt");
assert(actual.files[0].filename);
const file = await Deno.stat(actual.files[0].filename);
assertEquals(file.size, 3);
},
});
test({
name: "FormDataReader - inspecting",
fn() {
const body = createBody(fixture);
assertEquals(
Deno.inspect(new FormDataReader(fixtureContentType, body)),
`FormDataReader {}`,
);
},
});