Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 799867c

Browse filesBrowse files
author
anonx
committed
Replaced MakeMultipartRequest by PostFile
1 parent d8389d2 commit 799867c
Copy full SHA for 799867c

File tree

Expand file treeCollapse file tree

1 file changed

+74
-19
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+74
-19
lines changed

‎tests.go

Copy file name to clipboardExpand all lines: tests.go
+74-19Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import (
55
"fmt"
66
"io"
77
"io/ioutil"
8+
"mime"
89
"mime/multipart"
910
"net/http"
1011
"net/http/cookiejar"
12+
"net/textproto"
1113
"net/url"
14+
"os"
15+
"path/filepath"
1216
"regexp"
1317
"strings"
1418

@@ -62,8 +66,8 @@ func (t *TestSuite) WebSocketUrl() string {
6266
return "ws://" + t.Host()
6367
}
6468

65-
// Issue a GET request to the given path and store the result in Request and
66-
// RequestBody.
69+
// Issue a GET request to the given path and store the result in Response and
70+
// ResponseBody.
6771
func (t *TestSuite) Get(path string) {
6872
t.GetCustom(t.BaseUrl() + path).Send()
6973
}
@@ -80,8 +84,8 @@ func (t *TestSuite) GetCustom(uri string) *TestRequest {
8084
}
8185
}
8286

83-
// Issue a DELETE request to the given path and store the result in Request and
84-
// RequestBody.
87+
// Issue a DELETE request to the given path and store the result in Response and
88+
// ResponseBody.
8589
func (t *TestSuite) Delete(path string) {
8690
t.DeleteCustom(t.BaseUrl() + path).Send()
8791
}
@@ -99,7 +103,7 @@ func (t *TestSuite) DeleteCustom(uri string) *TestRequest {
99103
}
100104

101105
// Issue a POST request to the given path, sending the given Content-Type and
102-
// data, and store the result in Request and RequestBody. "data" may be nil.
106+
// data, and store the result in Response and ResponseBody. "data" may be nil.
103107
func (t *TestSuite) Post(path string, contentType string, reader io.Reader) {
104108
t.PostCustom(t.BaseUrl()+path, contentType, reader).Send()
105109
}
@@ -119,7 +123,7 @@ func (t *TestSuite) PostCustom(uri string, contentType string, reader io.Reader)
119123
}
120124

121125
// Issue a POST request to the given path as a form post of the given key and
122-
// values, and store the result in Request and RequestBody.
126+
// values, and store the result in Response and ResponseBody.
123127
func (t *TestSuite) PostForm(path string, data url.Values) {
124128
t.PostFormCustom(t.BaseUrl()+path, data).Send()
125129
}
@@ -130,23 +134,34 @@ func (t *TestSuite) PostFormCustom(uri string, data url.Values) *TestRequest {
130134
return t.PostCustom(uri, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
131135
}
132136

133-
// Issue a multipart request for the method & fields given and read the response.
134-
// If successful, the caller may examine the Response and ResponseBody properties.
135-
func (t *TestSuite) MakeMultipartRequest(method string, path string, fields map[string]string) {
136-
var b bytes.Buffer
137-
w := multipart.NewWriter(&b)
137+
// Issue a multipart request to the given path sending given params and files,
138+
// and store the result in Response and ResponseBody.
139+
func (t *TestSuite) PostFile(path string, params url.Values, filePaths url.Values) {
140+
t.PostFileCustom(t.BaseUrl()+path, params, filePaths).Send()
141+
}
142+
143+
// Return a multipart request to the given uri in a form of its wrapper
144+
// with the given params and files.
145+
func (t *TestSuite) PostFileCustom(uri string, params url.Values, filePaths url.Values) *TestRequest {
146+
body := &bytes.Buffer{}
147+
writer := multipart.NewWriter(body)
138148

139-
for key, value := range fields {
140-
w.WriteField(key, value)
149+
for key, values := range filePaths {
150+
for _, value := range values {
151+
createFormFile(writer, key, value)
152+
}
141153
}
142-
w.Close() //adds the terminating boundary
143154

144-
req, err := http.NewRequest(method, t.BaseUrl()+path, &b)
145-
if err != nil {
146-
panic(err)
155+
for key, values := range params {
156+
for _, value := range values {
157+
err := writer.WriteField(key, value)
158+
t.AssertEqual(nil, err)
159+
}
147160
}
148-
req.Header.Set("Content-Type", w.FormDataContentType())
161+
err := writer.Close()
162+
t.AssertEqual(nil, err)
149163

164+
return t.PostCustom(uri, writer.FormDataContentType(), body)
150165
}
151166

152167
// Issue any request and read the response. If successful, the caller may
@@ -251,11 +266,51 @@ func (t *TestSuite) AssertNotContains(s string) {
251266
}
252267
}
253268

254-
// Assert that the response matches the given regular expression.BUG
269+
// Assert that the response matches the given regular expression.
255270
func (t *TestSuite) AssertContainsRegex(regex string) {
256271
r := regexp.MustCompile(regex)
257272

258273
if !r.Match(t.ResponseBody) {
259274
panic(fmt.Errorf("Assertion failed. Expected response to match regexp %s", regex))
260275
}
261276
}
277+
278+
func createFormFile(writer *multipart.Writer, fieldname, filename string) {
279+
// Try to open the file.
280+
file, err := os.Open(filename)
281+
if err != nil {
282+
panic(err)
283+
}
284+
defer file.Close()
285+
286+
// Create a new form-data header with the provided field name and file name.
287+
// Determine Content-Type of the file by its extension.
288+
h := textproto.MIMEHeader{}
289+
h.Set("Content-Disposition", fmt.Sprintf(
290+
`form-data; name="%s"; filename="%s"`,
291+
escapeQuotes(fieldname),
292+
escapeQuotes(filepath.Base(filename)),
293+
))
294+
h.Set("Content-Type", "application/octet-stream")
295+
if ct := mime.TypeByExtension(filepath.Ext(filename)); ct != "" {
296+
h.Set("Content-Type", ct)
297+
}
298+
part, err := writer.CreatePart(h)
299+
if err != nil {
300+
panic(err)
301+
}
302+
303+
// Copy the content of the file we have opened not reading the whole
304+
// file into memory.
305+
_, err = io.Copy(part, file)
306+
if err != nil {
307+
panic(err)
308+
}
309+
}
310+
311+
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
312+
313+
// This function was borrowed from mime/multipart package.
314+
func escapeQuotes(s string) string {
315+
return quoteEscaper.Replace(s)
316+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.