-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormData.spec.ts
More file actions
73 lines (70 loc) · 2.89 KB
/
FormData.spec.ts
File metadata and controls
73 lines (70 loc) · 2.89 KB
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
import { FormData } from "./FormData"
describe("FormData", () => {
const body = `--X-BOUNDARY\r
Content-Disposition: form-data; name="message"\r
\r
HejHopp\r
--X-BOUNDARY\r
Content-Disposition: form-data; name="value"\r
\r
42\r
--X-BOUNDARY\r
Content-Disposition: form-data; name="file"; filename="test.txt"\r
Content-Type: text/plain\r
\r
This is the content of the file.\r
It contains two lines of content.\r
--X-BOUNDARY--\r
`
it("parse", async () => {
const data = await FormData.parse(new TextEncoder().encode(body), "multipart/form-data; boundary=X-BOUNDARY")
expect(JSON.parse(JSON.stringify(data))).toEqual({
file: [
{
data: [
84, 104, 105, 115, 32, 105, 115, 32, 116, 104, 101, 32, 99, 111, 110, 116, 101, 110, 116, 32, 111, 102, 32,
116, 104, 101, 32, 102, 105, 108, 101, 46, 13, 10, 73, 116, 32, 99, 111, 110, 116, 97, 105, 110, 115, 32,
116, 119, 111, 32, 108, 105, 110, 101, 115, 32, 111, 102, 32, 99, 111, 110, 116, 101, 110, 116, 46,
],
lastModified: 0,
name: "test.txt",
type: "text/plain",
},
],
value: ["42"],
message: ["HejHopp"],
})
expect(new TextDecoder().decode(data.toArrayBuffer("X-BOUNDARY"))).toEqual(body)
})
it("toArrayBuffer", async () => {
const data = await FormData.parse(new TextEncoder().encode(body), "multipart/form-data; boundary=X-BOUNDARY")
const buffer = data.toArrayBuffer("X-BOUNDARY")
const text = new TextDecoder().decode(buffer)
expect(text).toEqual(body)
expect(await FormData.parse(new Uint8Array(buffer), "multipart/form-data; boundary=X-BOUNDARY")).toEqual(data)
})
it("toString", () => {
const exampleCsv =
"field1,field2,start date,end date,another date,created,gross,fee,net,currency\r\n" +
'"example1","exampleA","2020-02-01","2020-02-07","2020-03-02","2020-01-16","2","3","4","EUR"\r\n' +
'"example2","exampleA","2020-02-01","2020-02-07","2020-03-02","2020-01-16","2","3","4","EUR"\r\n' +
'"example3","exampleA","2020-02-01","2020-02-07","2020-03-02","2020-01-16","2","3","4","EUR"\r\n'
const data = new FormData()
data.set("Dates", `Information ranging from 2021-01-01 to 2021-03-31`)
data.set("FileExample", exampleCsv, "example.csv")
const output = data.toString("X-BOUNDARY")
expect(output.replace(/\s/g, "")).toEqual(
`--X-BOUNDARY
Content-Disposition: form-data; name="Dates"
Information ranging from 2021-01-01 to 2021-03-31
--X-BOUNDARY
Content-Disposition: form-data; name="FileExample"; filename="example.csv"
Content-Type:text/csv
field1,field2,start date,end date,another date,created,gross,fee,net,currency
"example1","exampleA","2020-02-01","2020-02-07","2020-03-02","2020-01-16","2","3","4","EUR"
"example2","exampleA","2020-02-01","2020-02-07","2020-03-02","2020-01-16","2","3","4","EUR"
"example3","exampleA","2020-02-01","2020-02-07","2020-03-02","2020-01-16","2","3","4","EUR"
--X-BOUNDARY--`.replace(/\s/g, "")
)
})
})