Skip to content

Commit

Permalink
fix: WriteMultipartFormFile will fail when the Reader return EOF at f…
Browse files Browse the repository at this point in the history
…irst Read (#748)
  • Loading branch information
wzekin committed Apr 27, 2023
1 parent 2800a9b commit a8a426e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/protocol/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ func WriteMultipartFormFile(w *multipart.Writer, fieldName, fileName string, r i
// Auto detect actual multipart content type
cbuf := make([]byte, 512)
size, err := r.Read(cbuf)
if err != nil {
if err != nil && err != io.EOF {
return err
}

partWriter, err := w.CreatePart(CreateMultipartHeader(fieldName, fileName, http.DetectContentType(cbuf)))
partWriter, err := w.CreatePart(CreateMultipartHeader(fieldName, fileName, http.DetectContentType(cbuf[:size])))
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/protocol/multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func TestWriteMultipartFormFile(t *testing.T) {
t.Fatalf("write multipart error: %s", err)
}
assert.False(t, strings.Contains(bodyBuffer.String(), f3.Name()))

// test empty file
assert.Nil(t, WriteMultipartFormFile(w, "empty_test", "test.data", bytes.NewBuffer(nil)))
}

func TestMarshalMultipartForm(t *testing.T) {
Expand Down

0 comments on commit a8a426e

Please sign in to comment.