Added check to handle nil request body in nethttpadaptor (#218)

This commit is contained in:
Joni Collinge 2020-02-10 20:36:52 +00:00 committed by GitHub
parent df6c3c3e32
commit e67ea01fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 5 deletions

View File

@ -17,12 +17,14 @@ func NewNetHTTPHandlerFunc(h fasthttp.RequestHandler) http.HandlerFunc {
remoteAddr := net.IPAddr{remoteIP, ""} //nolint
c.Init(&fasthttp.Request{}, &remoteAddr, nil)
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("error reading request body, %+v", err)
return
if r.Body != nil {
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorf("error reading request body, %+v", err)
return
}
c.Request.SetBody(reqBody)
}
c.Request.SetBody(reqBody)
c.Request.SetRequestURI(r.URL.RequestURI())
c.Request.URI().SetScheme(r.URL.Scheme)
c.Request.SetHost(r.Host)

View File

@ -286,6 +286,18 @@ func TestNewNetHTTPHandlerFuncRequests(t *testing.T) {
}
},
},
{
"nil body is handled",
func() *http.Request {
req, _ := http.NewRequest("GET", "https://localhost:8080", nil)
return req
},
func(t *testing.T) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
assert.Equal(t, 0, len(ctx.Request.Body()))
}
},
},
}
for _, tt := range tests {