mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 19:51:50 +00:00
Fix bug where a nil reader could crash the redis middleware logger (#49571)
This commit is contained in:
parent
13e8742972
commit
95e411ad09
@ -53,7 +53,7 @@ func redisLoggerMiddleware() Middleware {
|
||||
|
||||
// Read body
|
||||
var requestBody []byte
|
||||
if req != nil && req.Body != nil {
|
||||
if req != nil && req.GetBody != nil {
|
||||
body, _ := req.GetBody()
|
||||
if body != nil {
|
||||
var readErr error
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
package httpcli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -24,6 +26,7 @@ func TestRedisLoggerMiddleware(t *testing.T) {
|
||||
normalReq, _ := http.NewRequest("GET", "http://dev/null", strings.NewReader("horse"))
|
||||
complexReq, _ := http.NewRequest("PATCH", "http://test.aa?a=2", strings.NewReader("graph"))
|
||||
complexReq.Header.Set("Cache-Control", "no-cache")
|
||||
postReqEmptyBody, _ := http.NewRequest("POST", "http://dev/null", io.NopCloser(bytes.NewBuffer([]byte{})))
|
||||
|
||||
testCases := []struct {
|
||||
req *http.Request
|
||||
@ -68,6 +71,20 @@ func TestRedisLoggerMiddleware(t *testing.T) {
|
||||
}),
|
||||
err: "oh no",
|
||||
},
|
||||
{
|
||||
req: postReqEmptyBody,
|
||||
name: "post request with empty body",
|
||||
cli: newFakeClientWithHeaders(map[string][]string{"X-Test-Header": {"value1", "value2"}}, http.StatusOK, []byte(`{"permission":false}`), nil),
|
||||
err: "<nil>",
|
||||
want: &types.OutboundRequestLogItem{
|
||||
Method: postReqEmptyBody.Method,
|
||||
URL: postReqEmptyBody.URL.String(),
|
||||
RequestHeaders: map[string][]string{},
|
||||
RequestBody: "",
|
||||
StatusCode: http.StatusOK,
|
||||
ResponseHeaders: map[string][]string{"Content-Type": {"text/plain; charset=utf-8"}, "X-Test-Header": {"value1", "value2"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Enable feature
|
||||
|
||||
Loading…
Reference in New Issue
Block a user