forked from cojs/busboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
163 lines (149 loc) · 6.12 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
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
var co = require('co')
var Stream = require('stream')
var assert = require('assert')
var busboy = require('./')
describe('Co Busboy', function () {
it('should work without autofields', function (done) {
co(function*(){
var parts = busboy(request());
var part;
var fields = 0;
var streams = 0;
while (part = yield parts) {
if (part.length) {
fields++
} else {
streams++
part.resume()
}
}
assert.equal(fields, 4)
assert.equal(streams, 2)
})(done)
})
it('should work with autofields', function (done) {
co(function*(){
var parts = busboy(request(), {
autoFields: true
});
var part;
var fields = 0;
var streams = 0;
while (part = yield parts) {
if (part.length) {
fields++
} else {
streams++
part.resume()
}
}
assert.equal(fields, 0)
assert.equal(streams, 2)
assert.equal(parts.fields.length, 4)
assert.equal(Object.keys(parts.field).length, 2)
})(done)
})
it('should work with autofields and arrays', function (done) {
co(function*(){
var parts = busboy(request(), {
autoFields: true
});
var part;
while (part = yield parts) {
part.resume()
}
assert.equal(Object.keys(parts.field).length, 2)
assert.equal(parts.field['file_name_0'].length, 2)
})(done)
})
it('should work with delays', function (done) {
co(function*(){
var parts = busboy(request(), {
autoFields: true
});
var part;
var streams = 0;
while (part = yield parts) {
streams++
part.resume()
yield wait(10)
}
assert.equal(streams, 2)
})(done)
})
it('should not overwrite prototypes', function (done) {
co(function*(){
var parts = busboy(request(), {
autoFields: true
});
var part;
while (part = yield parts) {
if (!part.length) part.resume()
};
assert.equal(parts.field.hasOwnProperty, Object.prototype.hasOwnProperty);
})(done)
})
it('should throw error when the files limit is reached', function (done) {
co(function*(){
var parts = busboy(request(), {
limits: {
files: 1
}
});
var part;
var error;
try {
while (part = yield parts) {
if (!part.length) part.resume()
}
} catch (e) {
error = e
}
assert.equal(error.status, 413)
assert.equal(error.code, 'Request_files_limit')
assert.equal(error.message, 'Reach files limit')
})(done)
})
})
function wait(ms) {
return function (done) {
setTimeout(done, ms)
}
}
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.end([
'-----------------------------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_1"',
'',
'super gamma file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="hasOwnProperty"',
'',
'super bad file',
'-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
'Content-Type: application/octet-stream',
'',
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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