-
Notifications
You must be signed in to change notification settings - Fork 0
/
formcontent_test.go
175 lines (128 loc) · 4.57 KB
/
formcontent_test.go
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
package formcontent_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"os"
"github.com/fredwangwang/formcontent"
)
var _ = Describe("Formcontent", func() {
var form *formcontent.Form
Describe("AddFile", func() {
var (
fileWithContent1 string
fileWithContent2 string
)
BeforeEach(func() {
handle1, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())
_, err = handle1.WriteString("some content")
Expect(err).NotTo(HaveOccurred())
fileWithContent1 = handle1.Name()
handle2, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())
_, err = handle2.WriteString("some more content")
Expect(err).NotTo(HaveOccurred())
fileWithContent2 = handle2.Name()
form = formcontent.NewForm()
})
AfterEach(func() {
os.Remove(fileWithContent1)
os.Remove(fileWithContent2)
})
It("writes out the provided file as a multipart form using the writer", func() {
err := form.AddFile("something[file1]", fileWithContent1)
Expect(err).NotTo(HaveOccurred())
err = form.AddFile("something[file2]", fileWithContent2)
Expect(err).NotTo(HaveOccurred())
submission := form.Finalize()
content, err := ioutil.ReadAll(submission.Content)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(MatchRegexp(`^--\w+\r\nContent-Disposition: form-data; name=\"something\[file1\]\"; filename=\"\w+\"\r\n` +
`Content-Type: application/octet-stream\r\n\r\n` +
`some content` +
`\r\n--\w+\r\nContent-Disposition: form-data; name=\"something\[file2\]\"; filename=\"\w+\"\r\n` +
`Content-Type: application/octet-stream\r\n\r\n` +
`some more content` +
`\r\n--\w+--\r\n$`))
})
Context("when the file provided is empty", func() {
It("returns an error", func() {
emptyFile, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())
form := formcontent.NewForm()
err = form.AddFile("foo", emptyFile.Name())
Expect(err).To(MatchError("file provided has no content"))
})
})
Context("when an error occurs", func() {
Context("when the original file cannot be read", func() {
It("returns an error", func() {
form := formcontent.NewForm()
err := form.AddFile("foo", "/file/does/not/exist")
Expect(err).To(HaveOccurred())
})
})
})
})
Describe("AddField", func() {
BeforeEach(func() {
form = formcontent.NewForm()
})
It("writes out the provided fields into the multipart form using the writer", func() {
err := form.AddField("key1", "value1")
Expect(err).NotTo(HaveOccurred())
err = form.AddField("key2", "value2")
Expect(err).NotTo(HaveOccurred())
submission := form.Finalize()
content, err := ioutil.ReadAll(submission.Content)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(MatchRegexp(`^--\w+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1` +
`\r\n--\w+\r\nContent-Disposition: form-data; name="key2"\r\n\r\nvalue2` +
`\r\n--\w+--\r\n$`))
})
})
Describe("AddCombined", func() {
var fileWithContent1 string
BeforeEach(func() {
var err error
handle1, err := ioutil.TempFile("", "")
Expect(err).NotTo(HaveOccurred())
_, err = handle1.WriteString("some content")
Expect(err).NotTo(HaveOccurred())
fileWithContent1 = handle1.Name()
form = formcontent.NewForm()
})
AfterEach(func() {
os.Remove(fileWithContent1)
})
It("writes out the provided fields into the multipart form using the writer", func() {
err := form.AddField("key1", "value1")
Expect(err).NotTo(HaveOccurred())
err = form.AddFile("file1", fileWithContent1)
Expect(err).NotTo(HaveOccurred())
submission := form.Finalize()
content, err := ioutil.ReadAll(submission.Content)
Expect(err).NotTo(HaveOccurred())
Expect(submission.ContentLength).To(Equal(int64(373)))
Expect(string(content)).To(MatchRegexp(`^--\w+\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"\w+\"\r\n` +
`Content-Type: application/octet-stream\r\n\r\n` +
`some content` +
`\r\n--\w+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1` +
`\r\n--\w+--\r\n$`))
})
})
Describe("Finalize", func() {
var form *formcontent.Form
BeforeEach(func() {
form = formcontent.NewForm()
})
It("returns a content submission which includes the correct length and content type", func() {
err := form.AddField("key1", "value1")
Expect(err).NotTo(HaveOccurred())
submission := form.Finalize()
Expect(submission.ContentLength).To(Equal(int64(185)))
Expect(submission.ContentType).To(ContainSubstring("multipart/form-data"))
})
})
})