forked from andreasantillana/parse-multipart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
89 lines (73 loc) · 2.79 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
84
85
86
87
88
89
/* jshint node: true */
'use strict';
/**
Unit testing, run with `node test`
*/
const assert = require('assert');
const multipart = require('./multipart.js');
const demoData = () => {
const body = [
`trash1`,
`------WebKitFormBoundaryvef1fLxmoUdYZWXp`,
`Content-Disposition: form-data; name="uploads[]"; filename="A.txt"`,
`Content-Type: text/plain`,
``,
`@11X111Y`,
`111Z\rCCCC\nCCCC`,
`CCCCC@`,
``,
`------WebKitFormBoundaryvef1fLxmoUdYZWXp`,
`Content-Disposition: form-data; name="testMessage";`,
``,
`test message 123456 ÄÖÜäöüß`,
`------WebKitFormBoundaryvef1fLxmoUdYZWXp`,
`Content-Disposition: form-data; name="uploads[]"; filename="C.txt"`,
`Content-Type: text/plain`,
``,
`@CCCCCCY`,
`CCCZ\rCCCW\nCCC0`,
`666@`,
`------WebKitFormBoundaryvef1fLxmoUdYZWXp--`
].join('\r\n') + '\r\n';
return Buffer.from(body, 'utf8');
};
const testParts = (parts) => {
assert.strictEqual(parts.length, 3);
assert.strictEqual(parts[0].filename, 'A.txt');
assert.strictEqual(parts[1].filename, undefined);
assert.strictEqual(parts[2].filename, 'C.txt');
assert.strictEqual(parts[0].data.toString(), '@11X111Y\r\n111Z\rCCCC\nCCCC\r\nCCCCC@\r\n');
assert.strictEqual(parts[1].data, 'test message 123456 ÄÖÜäöüß');
assert.strictEqual(parts[2].data.toString(), '@CCCCCCY\r\nCCCZ\rCCCW\nCCC0\r\n666@');
assert.strictEqual(parts[0].name, 'uploads[]');
assert.strictEqual(parts[1].name, 'testMessage');
assert.strictEqual(parts[2].name, 'uploads[]');
};
// Test getBoundary
const boundaryTest = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
const contentType = 'Content-Type: multipart/form-data; boundary=' + boundaryTest;
const boundaryResult = multipart.getBoundary(contentType);
assert.strictEqual(boundaryResult, boundaryTest);
// Test parse
const body = demoData();
const demoBoundary = '----WebKitFormBoundaryvef1fLxmoUdYZWXp';
const parts = multipart.parse(body, demoBoundary);
testParts(parts);
// Test middleware
const req = {
headers: {
'content-type': 'Content-Type: multipart/form-data; boundary=' + demoBoundary
},
body: demoData()
};
const res = {};
multipart.middleware({
dest: false
})(req, res, () => {
assert.strictEqual(req.fields['uploads[]'], undefined);
assert.strictEqual(req.fields.testMessage, 'test message 123456 ÄÖÜäöüß');
assert.strictEqual(req.files.testMessage, undefined);
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@');
});