Skip to content

Commit

Permalink
add missing NoBody handling
Browse files Browse the repository at this point in the history
  • Loading branch information
everpeace committed Jan 24, 2024
1 parent 0e9f8f9 commit 3615595
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/webhook/authorization/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx = wh.WithContextFunc(ctx, r)
}

if r.Body == nil {
if r.Body == nil || r.Body == http.NoBody {
err := errors.New("request body is empty")
wh.getLogger(nil).Error(err, "bad request")
wh.writeResponse(w, Errored(err))
Expand Down
13 changes: 13 additions & 0 deletions pkg/webhook/authorization/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ var _ = Describe("Authentication Webhooks", func() {
webhook.ServeHTTP(respRecorder, req.WithContext(ctx))
Expect(respRecorder.Body.String()).To(Equal(expected))
})

It("should error when given a NoBody", func() {
req := &http.Request{
Header: http.Header{"Content-Type": []string{"application/json"}},
Method: http.MethodPost,
Body: http.NoBody,
}

expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"request body is empty"}}
`
webhook.ServeHTTP(respRecorder, req)
Expect(respRecorder.Body.String()).To(Equal(expected))
})
})
})

Expand Down

0 comments on commit 3615595

Please sign in to comment.