Skip to content

Commit

Permalink
Handle EOF from multipart form file readers (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Sep 13, 2021
1 parent b8f7c11 commit 3eedf3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util.go
Expand Up @@ -202,7 +202,7 @@ 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
}

Expand Down
16 changes: 16 additions & 0 deletions util_test.go
Expand Up @@ -5,6 +5,8 @@
package resty

import (
"bytes"
"mime/multipart"
"testing"
)

Expand Down Expand Up @@ -79,3 +81,17 @@ func TestIsXMLType(t *testing.T) {
}
}
}

func TestWriteMultipartFormFileReaderEmpty(t *testing.T) {
w := multipart.NewWriter(bytes.NewBuffer(nil))
defer func() { _ = w.Close() }()
if err := writeMultipartFormFile(w, "foo", "bar", bytes.NewReader(nil)); err != nil {
t.Errorf("Got unexpected error: %v", err)
}
}

func TestWriteMultipartFormFileReaderError(t *testing.T) {
err := writeMultipartFormFile(nil, "", "", &brokenReadCloser{})
assertNotNil(t, err)
assertEqual(t, "read error", err.Error())
}

0 comments on commit 3eedf3b

Please sign in to comment.