diff --git a/pkg/protocol/multipart.go b/pkg/protocol/multipart.go index 429049111..e926c947a 100644 --- a/pkg/protocol/multipart.go +++ b/pkg/protocol/multipart.go @@ -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 } diff --git a/pkg/protocol/multipart_test.go b/pkg/protocol/multipart_test.go index be93b8953..86995d28e 100644 --- a/pkg/protocol/multipart_test.go +++ b/pkg/protocol/multipart_test.go @@ -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) {