forked from m4nuC/async-busboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specs.js
205 lines (186 loc) · 9.49 KB
/
specs.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
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
'use strict';
const Stream = require('stream');
const expect = require('expect');
const path = require('path');
const fs = require('fs');
const formstream = require('formstream');
const asyncBusboy = require('./');
const fileContent = [
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
];
const readFileStreamPromise = (readStream) => new Promise((resolve, reject) => {
let buffers = [];
readStream.on("data", (buf) => buffers.push(buf));
readStream.on("end", () => resolve(Buffer.concat(buffers)));
readStream.on("error", reject);
});
describe('Async-busboy', () => {
it('should gather all fields and streams', (done) => {
asyncBusboy(request()).then(formData => {
expect(Object.keys(formData.files).length).toBe(3);
expect(Object.keys(formData.fields).length).toBe(5);
// Check file contents
const fileContentPromises = formData.files.map(readFileStreamPromise);
Promise.all(fileContentPromises)
.then((contentBufs) => {
contentBufs.forEach((content, index) =>
expect(content.toString()).toBe(fileContent[index])
);
done();
})
.catch(done);
}).catch(done);
});
it('should gather all fields and streams using custom file handler', (done) => {
const fileContentPromises = [];
const onFileHandler = (fieldname, file, filename, encoding, mimetype) => {
fileContentPromises.push(readFileStreamPromise(file));
};
asyncBusboy(request(), { onFile: onFileHandler }).then(formData => {
expect(Object.keys(formData.fields).length).toBe(5);
// Check file contents
Promise.all(fileContentPromises)
.then((contentBufs) => {
contentBufs.forEach((content, index) =>
expect(content.toString()).toBe(fileContent[index])
);
done();
})
.catch(done);
}).catch(done);
});
it('should return a valid collection', (done) => {
asyncBusboy(request())
.then(formData => {
var someCollection = formData.fields.someCollection;
expect(Array.isArray(someCollection)).toBe(true);
expect(someCollection[0]).toEqual({foo: 'foo', bar: 'bar'});
done();
})
.catch(done)
});
it('should return a valid array', (done) => {
asyncBusboy(request())
.then(formData => {
var fileName0 = formData.fields.file_name_0;
expect(Array.isArray(fileName0)).toBe(true);
expect(fileName0.length).toBe(3);
expect(fileName0[0]).toEqual('super alpha file');
expect(fileName0[1]).toEqual('super beta file');
expect(fileName0[2]).toEqual('super gamma file');
done();
})
.catch(done)
});
it('should not overwrite prototypes', (done) => {
asyncBusboy(request()).then(formData => {
expect(formData.fields.hasOwnProperty).toEqual(Object.prototype.hasOwnProperty)
done();
}).catch(done);
});
it('should throw error when the files limit is reached', (done) => {
asyncBusboy(request(), {limits: {
files: 1
}}).then(() => {
done(makeError('Request_files_limit was not thrown'))
},
e => {
expect(e.status).toBe(413);
expect(e.code).toBe('Request_files_limit');
expect(e.message).toBe('Reach files limit');
done()
});
});
it('should throw error when the fields limit is reached', (done) => {
asyncBusboy(request(), {limits: {
fields: 1
}}).then(() => {
done(makeError('Request_fields_limit was not thrown'))
},
e => {
expect(e.status).toBe(413);
expect(e.code).toBe('Request_fields_limit');
expect(e.message).toBe('Reach fields limit');
done()
});
});
});
function makeError(message) {
return new Error(message);
}
function request() {
// https://github.com/mscdex/busboy/blob/master/test/test-types-multipart.js
var stream = new Stream.PassThrough()
stream.headers = {
'content-type': 'multipart/form-data; boundary=---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k'
}
stream.write([
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="file_name_0"',
'',
'super alpha file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="file_name_0"',
'',
'super beta file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="file_name_0"',
'',
'super gamma file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="file_name_1"',
'',
'super gamma file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="_csrf"',
'',
'ooxx',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="hasOwnProperty"',
'',
'super bad file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="someCollection[0][foo]"',
'',
'foo',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="someCollection[0][bar]"',
'',
'bar',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="someCollection[1][0]"',
'',
'foo',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="someCollection[1][1]"',
'',
'bar',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="someField[foo]"',
'',
'foo',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="someField[bar]"',
'',
'bar',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
'Content-Type: application/octet-stream',
'',
fileContent[0],
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="upload_file_1"; filename="1k_b.dat"',
'Content-Type: application/octet-stream',
'',
fileContent[1],
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="upload_file_2"; filename="hack.exe"',
'Content-Type: application/octet-stream',
'',
fileContent[2],
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
].join('\r\n'))
return stream
}