This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultipart-form-data.spec.js
59 lines (51 loc) · 2.03 KB
/
multipart-form-data.spec.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
const { encodePayload } = require('../src/converter')
test('Test encodePayload for multipart/form-data', () => {
const multiPartFormData = {
name: 'Tom',
surname: 'Trailer',
image: Buffer.from('image').toString('base64'),
}
const content = {
image: {
contentType: 'image/png',
},
}
let encodedMultipart = '--956888039105887155673143\r\n'
encodedMultipart += 'Content-Disposition: form-data; name="name"\r\n\r\n'
encodedMultipart += 'Tom\r\n'
encodedMultipart += '--956888039105887155673143\r\n'
encodedMultipart += 'Content-Disposition: form-data; name="surname"\r\n\r\n'
encodedMultipart += 'Trailer\r\n'
encodedMultipart += '--956888039105887155673143\r\n'
const encodedMultipartComplexExpected =
encodedMultipart +
'Content-Disposition: form-data; name="image"; filename="image"\r\n' +
'Content-Type: image/png\r\n' +
'Content-Transfer-Encoding: base64\r\n\r\niVBORw0KGgo=\r\n--956888039105887155673143--'
const multipartFormDataComplexEncoded = encodePayload(
multiPartFormData,
'multipart/form-data',
content
)
expect(multipartFormDataComplexEncoded.mimeType).toEqual(
'multipart/form-data; boundary=956888039105887155673143'
)
expect(multipartFormDataComplexEncoded.text).toEqual(encodedMultipartComplexExpected)
})
test('Test encodePayload for multipart/form-data', () => {
const multiPartFormData = {
person: {
name: 'John',
surname: 'Doe',
},
}
let encodedMultipartExpected = '--956888039105887155673143\r\n'
encodedMultipartExpected += 'Content-Disposition: form-data; name="person"\r\n'
encodedMultipartExpected += 'Content-Type: application/json\r\n\r\n'
encodedMultipartExpected += '{"name":"John","surname":"Doe"}\r\n--956888039105887155673143--'
const multipartFormDataEncoded = encodePayload(multiPartFormData, 'multipart/form-data')
expect(multipartFormDataEncoded.mimeType).toEqual(
'multipart/form-data; boundary=956888039105887155673143'
)
expect(multipartFormDataEncoded.text).toEqual(encodedMultipartExpected)
})