Skip to content

Commit 39c56b9

Browse files
Libero-Devjeevatkm
andauthored
fix: enforce content length header when body is nil and client specifies SetContentLength true (#1003)
* fix: only override nil body if valid http verb * test: add testcase validating http server receives correct Content-Length Header * fix: update Test_parseRequestBody to accommodate nil body request with SetContentLength override scenario * fix: remove redundant conditional check --------- Co-authored-by: Jeevanandam M. <[email protected]>
1 parent 47780b1 commit 39c56b9

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

middleware.go

+5
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func parseRequestBody(c *Client, r *Request) error {
196196
}
197197
case len(c.FormData) > 0 || len(r.FormData) > 0: // Handling Form Data
198198
handleFormData(c, r)
199+
case r.Body == nil && r.bodyBuf == nil: // Handling Request body when nil body
200+
// Go http library omits Content-Length if body is nil; use http.NoBody to force it if SetContentLength is true
201+
r.Body = http.NoBody
202+
fallthrough
199203
case r.Body != nil: // Handling Request body
200204
handleContentType(c, r)
201205

@@ -315,6 +319,7 @@ func createCurlCmd(c *Client, r *Request) (err error) {
315319
}
316320
*r.resultCurlCmd = buildCurlRequest(r.RawRequest, c.httpClient.Jar)
317321
}
322+
318323
return nil
319324
}
320325

middleware_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,15 @@ func Test_parseRequestBody(t *testing.T) {
513513
r.SetContentLength(true)
514514
},
515515
expectedContentLength: "0",
516+
expectedBodyBuf: []byte{},
516517
},
517518
{
518519
name: "empty body with SetContentLength by client",
519520
init: func(c *Client, r *Request) {
520521
c.SetContentLength(true)
521522
},
522523
expectedContentLength: "0",
524+
expectedBodyBuf: []byte{},
523525
},
524526
{
525527
name: "string body",

request_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -2283,3 +2283,27 @@ func TestRequestGH917(t *testing.T) {
22832283
}
22842284
wg.Wait()
22852285
}
2286+
2287+
func TestSetContentLengthTrueWithNilBody(t *testing.T) {
2288+
ts := createPostServer(t)
2289+
defer ts.Close()
2290+
2291+
c := dc()
2292+
2293+
c.OnBeforeRequest(func(client *Client, request *Request) error {
2294+
request.
2295+
SetBody(nil).
2296+
SetContentLength(true)
2297+
2298+
return nil
2299+
})
2300+
2301+
c = c.SetBaseURL(ts.URL)
2302+
2303+
resp, err := c.R().Execute("POST", "/check-request-content-length")
2304+
2305+
assertNil(t, err)
2306+
2307+
// when body is nil and SetContentLength is true expect Content-Length == "0"
2308+
assertEqual(t, "0", resp.Header().Get("Request-Content-Length"))
2309+
}

resty_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ func createPostServer(t *testing.T) *httptest.Server {
326326
}
327327
http.SetCookie(w, &cookie)
328328
w.WriteHeader(http.StatusOK)
329+
case "/check-request-content-length":
330+
// test endpoint for checking the server receives the correct Content-Length Header from a request
331+
w.Header().Add("Request-Content-Length", r.Header.Get("Content-Length"))
332+
w.WriteHeader(http.StatusOK)
329333
}
330334
}
331335
})

0 commit comments

Comments
 (0)