Skip to content

Commit

Permalink
Make sure WithFiles does not add duplicates to multipart form data
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonna committed Dec 12, 2020
1 parent 486d9f1 commit 0c2364d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
8 changes: 4 additions & 4 deletions client/withfilesoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func WithFiles() Option {
for i, fileData := range filesGroup {
mapDataFiles = append(
mapDataFiles,
fmt.Sprintf(`"%d":[%s]`, i, strings.Join(collect(fileData, wrapMapKeyInQuotes)[:], ",")),
fmt.Sprintf(`"%d":[%s]`, i, strings.Join(collect(fileData, wrapMapKeyInQuotes), ",")),
)
}

Expand All @@ -105,10 +105,10 @@ func WithFiles() Option {
// or
// Content-Type: application/octet-stream
//
for i, fileData := range filesData {
for i, fileData := range filesGroup {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%d"; filename="%s"`, i, fileData.file.Name()))
b, _ := ioutil.ReadFile(fileData.file.Name())
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%d"; filename="%s"`, i, fileData[0].file.Name()))
b, _ := ioutil.ReadFile(fileData[0].file.Name())
h.Set("Content-Type", http.DetectContentType(b))
ff, _ := bodyWriter.CreatePart(h)
ff.Write(b)
Expand Down
1 change: 1 addition & 0 deletions client/withfilesoption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func TestWithFiles(t *testing.T) {
require.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
require.Equal(t, []byte(`hello world`), slurp)
}
require.False(t, regexp.MustCompile(`form-data; name="2"; filename=.*`).MatchString(contentDisposition))
}
w.Write([]byte(`{}`))
})
Expand Down

0 comments on commit 0c2364d

Please sign in to comment.