-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipart-form-data.test.js
191 lines (160 loc) · 9.93 KB
/
multipart-form-data.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
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
import test from 'ava'
import { MultipartFormData, MultipartFormDataBuilder } from './multipart-form-data'
test('should create header/body with one property', t => {
const formDataBuilder = MultipartFormDataBuilder()
formDataBuilder.append('my_key', 'my_value')
const formData = formDataBuilder.build()
const actualContentType = formData.headers['Content-Type']
t.regex(actualContentType, /multipart\/form-Data; boundary=--.+/gi)
const actualBoundary = /multipart\/form-Data; boundary=--(.+)/gi.exec(actualContentType)[1]
t.is(formData.body.toString(), `\r\n----${actualBoundary}\r\nContent-Disposition: form-data; name="my_key"\r\n\r\nmy_value\r\n----${actualBoundary}--`)
})
test('should create header/body with two properties', t => {
const formDataBuilder = MultipartFormDataBuilder()
formDataBuilder.append('my_key_one', 'my_value_one')
formDataBuilder.append('my_key_two', 'my_value_two')
const formData = formDataBuilder.build()
const actualContentType = formData.headers['Content-Type']
t.regex(actualContentType, /multipart\/form-Data; boundary=--.+/gi)
const actualBoundary = /multipart\/form-Data; boundary=--(.+)/gi.exec(actualContentType)[1]
t.is(formData.body.toString(), `\r\n----${actualBoundary}\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n----${actualBoundary}\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n----${actualBoundary}--`)
})
test('should create header/body with two properties and a file', t => {
const formDataBuilder = MultipartFormDataBuilder()
formDataBuilder.append('my_key_one', 'my_value_one')
formDataBuilder.append('my_key_two', 'my_value_two')
formDataBuilder.appendFile('my_upload_file', Buffer.from('abc'), 'my-filename.txt', 'text/plain')
const formData = formDataBuilder.build()
const actualContentType = formData.headers['Content-Type']
t.regex(actualContentType, /multipart\/form-Data; boundary=--.+/gi)
const actualBoundary = /multipart\/form-Data; boundary=--(.+)/gi.exec(actualContentType)[1]
t.is(formData.body.toString(), `\r\n----${actualBoundary}\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n----${actualBoundary}\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n----${actualBoundary}\r\nContent-Disposition: form-data; name="my_upload_file"; filename="my-filename.txt"\r\nContent-Type: text/plain\r\n\r\nabc\r\n----${actualBoundary}--`)
})
test('should parse body with one property', t => {
const parsedBody = MultipartFormData(
{
'Content-Type': 'Multipart/Form-Data; boundary=--MySpecialBoundary'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key"\r\n\r\nmy_value\r\n--MySpecialBoundary--'
).parse()
t.is(parsedBody['my_key'], 'my_value')
})
test('should parse body with two properties', t => {
const parsedBody = MultipartFormData(
{
'Content-Type': 'Multipart/Form-Data; boundary=--MySpecialBoundary'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary--'
).parse()
t.is(parsedBody['my_key_one'], 'my_value_one')
t.is(parsedBody['my_key_two'], 'my_value_two')
})
test('should parse body with two properties and a file', t => {
const parsedBody = MultipartFormData(
{
'Content-Type': 'Multipart/Form-Data; boundary=--MySpecialBoundary'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_uploaded_file"; filename="README.md"\r\nContent-Type: text/markdown\r\n\r\n# simple multipart form data\r\n--MySpecialBoundary--'
).parse()
t.is(parsedBody['my_key_one'], 'my_value_one')
t.is(parsedBody['my_key_two'], 'my_value_two')
t.deepEqual(parsedBody['my_uploaded_file'], [{
filename: 'README.md',
mimeType: 'text/markdown',
data: Buffer.from('# simple multipart form data')
}])
})
test('should parse body with longer properties and a (pseudo) binary file', t => {
const parsedBody = MultipartFormData(
{
'Content-Type': 'Multipart/Form-Data; boundary=--MySpecialBoundary'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_recaptcha_response"\r\n\r\n03AOLTBLRdf41VsFrrPKInPVLLDkfNqixBfytxuKILrLh8HSIqlE0s9Zx5WZ8Jqqw6NQH8OiBEMHvO-0v6WI_RtDxmwEDHN8AXHw_OwJFus-cbdGJ1_Qcfmf0m2WZckiLa3YJm_iHoldWs_V92YLKIamPMNkLFmggMWe-ieikUWdCPiIEaGZxFvgScANKjtWxhCoFkAMTa9tsw0oK2Q8mijB_6b0uLahIgA4LdawVORWXju3IVMOYraT2feFVPmEnsnvh9M_4QZjwiLNt9PO8O4c3e36X6Oz_RXtA1_fCx3uzSILfhgaIU31JmAPuiJe1HjPehQYhsGLIf0vx4c-8PN5K_EOu0lzNxVbiKtQkVFlp79bPI5V1M2MRVjSZssdTrQY7PPslxTJzSJQgAoyBF8hJm_hHNmVUr62fd2TaqPkAOwSL_1gAftwj3_BtESXAPiQWSPQZg7C1O\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_uploaded_file"; filename="image.png"\r\nContent-Type: image/png\r\n\r\n\t\0\n\r\r\n--MySpecialBoundary--'
).parse()
t.is(parsedBody['my_key_one'], 'my_value_one')
t.is(parsedBody['my_key_two'], 'my_value_two')
t.is(parsedBody['my_recaptcha_response'], '03AOLTBLRdf41VsFrrPKInPVLLDkfNqixBfytxuKILrLh8HSIqlE0s9Zx5WZ8Jqqw6NQH8OiBEMHvO-0v6WI_RtDxmwEDHN8AXHw_OwJFus-cbdGJ1_Qcfmf0m2WZckiLa3YJm_iHoldWs_V92YLKIamPMNkLFmggMWe-ieikUWdCPiIEaGZxFvgScANKjtWxhCoFkAMTa9tsw0oK2Q8mijB_6b0uLahIgA4LdawVORWXju3IVMOYraT2feFVPmEnsnvh9M_4QZjwiLNt9PO8O4c3e36X6Oz_RXtA1_fCx3uzSILfhgaIU31JmAPuiJe1HjPehQYhsGLIf0vx4c-8PN5K_EOu0lzNxVbiKtQkVFlp79bPI5V1M2MRVjSZssdTrQY7PPslxTJzSJQgAoyBF8hJm_hHNmVUr62fd2TaqPkAOwSL_1gAftwj3_BtESXAPiQWSPQZg7C1O')
t.deepEqual(parsedBody['my_uploaded_file'], [{
filename: 'image.png',
mimeType: 'image/png',
data: Buffer.from('\t\0\n\r')
}])
})
test('should parse body with two properties and two files in one property', t => {
const parsedBody = MultipartFormData(
{
'Content-Type': 'Multipart/Form-Data; boundary=--MySpecialBoundary'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_uploaded_file"; filename="README.md"\r\nContent-Type: text/markdown\r\n\r\n# simple multipart form data\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_uploaded_file"; filename="LICENSE.txt"\r\nContent-Type: text/plain\r\n\r\nApache License 2.0\r\n--MySpecialBoundary--'
).parse()
t.is(parsedBody['my_key_one'], 'my_value_one')
t.is(parsedBody['my_key_two'], 'my_value_two')
t.deepEqual(parsedBody['my_uploaded_file'], [{
filename: 'README.md',
mimeType: 'text/markdown',
data: Buffer.from('# simple multipart form data')
}, {
filename: 'LICENSE.txt',
mimeType: 'text/plain',
data: Buffer.from('Apache License 2.0')
}])
})
test('should parse body with two properties and two files in two properties', t => {
const parsedBody = MultipartFormData(
{
'Content-Type': 'Multipart/Form-Data; boundary=--MySpecialBoundary'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_uploaded_readme"; filename="README.md"\r\nContent-Type: text/markdown\r\n\r\n# simple multipart form data\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_uploaded_license"; filename="LICENSE.txt"\r\nContent-Type: text/plain\r\n\r\nApache License 2.0\r\n--MySpecialBoundary--'
).parse()
t.is(parsedBody['my_key_one'], 'my_value_one')
t.is(parsedBody['my_key_two'], 'my_value_two')
t.deepEqual(parsedBody['my_uploaded_readme'], [{
filename: 'README.md',
mimeType: 'text/markdown',
data: Buffer.from('# simple multipart form data')
}])
t.deepEqual(parsedBody['my_uploaded_license'], [{
filename: 'LICENSE.txt',
mimeType: 'text/plain',
data: Buffer.from('Apache License 2.0')
}])
})
test('should create header/body with two properties and a file and parse it afterwards', t => {
const formDataBuilder = MultipartFormDataBuilder()
formDataBuilder.append('my_key_one', 'my_value_one')
formDataBuilder.append('my_key_two', 'my_value_two')
formDataBuilder.appendFile('my_upload_file', Buffer.from('abc'), 'my-filename.txt', 'text/plain')
const formData = formDataBuilder.build()
const parsedFormData = MultipartFormData(formData.headers, formData.body)
.parse()
t.deepEqual(
parsedFormData,
{
my_key_one: 'my_value_one',
my_key_two: 'my_value_two',
my_upload_file: [
{
filename: 'my-filename.txt',
data: Buffer.from('abc'),
mimeType: 'text/plain'
}
]
}
)
})
test('should not parse headers without content-type', t => {
const formData = MultipartFormData(
{},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary--'
)
t.throws(() => { formData.parse() }, 'Content-Type missing in headers')
})
test('should not parse headers with incorrect content-type', t => {
const formData = MultipartFormData(
{
'Content-Type': 'application/json'
},
'--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_one"\r\n\r\nmy_value_one\r\n--MySpecialBoundary\r\nContent-Disposition: form-data; name="my_key_two"\r\n\r\nmy_value_two\r\n--MySpecialBoundary--'
)
t.throws(() => { formData.parse() }, 'Unexpected Content-Type \'application/json\' (expecting: \'multipart/form-data\')')
})