From eb7cac754d03db5084699dc9748e9db08b5c481b Mon Sep 17 00:00:00 2001 From: Jeevanandam M Date: Sat, 2 Mar 2024 15:45:21 -0800 Subject: [PATCH] fix: trailing NULL bytes in buffer while detecting a content type --- middleware_test.go | 6 +++--- util.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/middleware_test.go b/middleware_test.go index f4e5c698..f5924cbe 100644 --- a/middleware_test.go +++ b/middleware_test.go @@ -756,7 +756,7 @@ func Test_parseRequestBody(t *testing.T) { expectedContentLength: "744", }, { - name: "mulipart fields", + name: "multipart fields", init: func(c *Client, r *Request) { r.SetMultipartFields( &MultipartField{ @@ -776,7 +776,7 @@ func Test_parseRequestBody(t *testing.T) { expectedContentLength: "344", }, { - name: "mulipart files", + name: "multipart files", init: func(c *Client, r *Request) { r.SetFileReader("foo", "foo.txt", strings.NewReader("1")). SetFileReader("bar", "bar.txt", strings.NewReader("2")). @@ -784,7 +784,7 @@ func Test_parseRequestBody(t *testing.T) { }, expectedBodyBuf: []byte(`{"bar":"2","foo":"1"}`), expectedContentType: "multipart/form-data; boundary=", - expectedContentLength: "412", + expectedContentLength: "414", }, { name: "body with errorReader", diff --git a/util.go b/util.go index 27b466dc..5a69e4fc 100644 --- a/util.go +++ b/util.go @@ -216,7 +216,7 @@ func writeMultipartFormFile(w *multipart.Writer, fieldName, fileName string, r i 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 }