-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
83 lines (68 loc) · 2.7 KB
/
test.js
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
'use strict';
const assert = require('assert');
const parser = require('./index');
const middleware = parser({
dest: './',
mapFields: (fields) => {
Object.keys(fields).forEach(fieldKey => {
fields[fieldKey] = fields[fieldKey].data.toString();
});
return fields;
}
});
const demoBoundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
const demoData = () => {
const boundary = () => '--' + demoBoundary;
const endBoundary = () => boundary() + '--';
const contentDisposition = (name, filename) => 'Content-Disposition: form-data; name="' + name + '"' + (filename ? (' filename="' + filename + '"') : '');
const contentType = type => 'Content-Type: ' + type;
const newLine = () => '';
const contentTransferEncoding = enc => 'Content-Transfer-Encoding: ' + enc;
const field = (name, data) => [
boundary(),
contentDisposition(name),
newLine(),
data
];
const file = (name, filename, data, type, enc) => {
const value = [
boundary(),
contentType(type),
contentDisposition(name, filename)
];
if (enc) {
value.push(contentTransferEncoding(enc));
}
value.push(newLine());
value.push(data);
return value;
};
const body = [
'trash',
...field('testMessage', 'test message 123456 ÄÖÜäöüß'),
...file('upload', 'singlefile.txt', 'This is a single file upload with umlaut:\r\näöüÄÖÜß\r\n\r\nSee?', 'text/plain'),
...file('uploads[]', 'A.txt', `@11X111Y\r\n111Z\rCCCC\nCCCC\r\nCCCCC@\r\n`, 'text/plain'),
...file('uploads[]', 'testenc.txt', '@CCCCCCY\r\nCCCZ\rCCCW\nCCC0\r\n666@', 'text/plain', 'binary'),
endBoundary()
].join('\r\n') + '\r\n';
return Buffer.from(body, 'utf8');
};
const req = {
// request data
headers: {
'content-type': 'Content-Type: multipart/form-data; boundary=' + demoBoundary
},
body: demoData()
};
middleware(req, {
// response data
}, () => {
assert.strictEqual(req.fields.testMessage, 'test message 123456 ÄÖÜäöüß');
assert.strictEqual(req.fields.upload, undefined);
assert.strictEqual(req.fields['uploads[]'], undefined);
assert.strictEqual(req.files.testMessage, undefined);
assert.strictEqual(req.files.upload.data.toString(), 'This is a single file upload with umlaut:\r\näöüÄÖÜß\r\n\r\nSee?');
assert.strictEqual(req.files['uploads[]'].length, 2);
assert.strictEqual(req.files['uploads[]'][0].data.toString(), '@11X111Y\r\n111Z\rCCCC\nCCCC\r\nCCCCC@\r\n');
assert.strictEqual(req.files['uploads[]'][1].data.toString(), '@CCCCCCY\r\nCCCZ\rCCCW\nCCC0\r\n666@');
});