Skip to content

Commit 6e343b7

Browse files
committed
add missing NoBody handling
Signed-off-by: Tim Ramlot <[email protected]>
1 parent a169f8b commit 6e343b7

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

pkg/webhook/admission/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5050
ctx = wh.WithContextFunc(ctx, r)
5151
}
5252

53-
if r.Body == nil {
53+
if r.Body == nil || r.Body == http.NoBody {
5454
err := errors.New("request body is empty")
5555
wh.getLogger(nil).Error(err, "bad request")
5656
wh.writeResponse(w, Errored(http.StatusBadRequest, err))

pkg/webhook/admission/http_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ var _ = Describe("Admission Webhooks", func() {
8585
Expect(respRecorder.Body.String()).To(Equal(expected))
8686
})
8787

88+
It("should error when given a NoBody", func() {
89+
req := &http.Request{
90+
Header: http.Header{"Content-Type": []string{"application/json"}},
91+
Method: http.MethodPost,
92+
Body: http.NoBody,
93+
}
94+
95+
expected := `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
96+
`
97+
webhook.ServeHTTP(respRecorder, req)
98+
Expect(respRecorder.Body.String()).To(Equal(expected))
99+
})
100+
88101
It("should error when given an infinite body", func() {
89102
req := &http.Request{
90103
Header: http.Header{"Content-Type": []string{"application/json"}},

pkg/webhook/authentication/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5050
ctx = wh.WithContextFunc(ctx, r)
5151
}
5252

53-
if r.Body == nil {
53+
if r.Body == nil || r.Body == http.NoBody {
5454
err := errors.New("request body is empty")
5555
wh.getLogger(nil).Error(err, "bad request")
5656
wh.writeResponse(w, Errored(err))

pkg/webhook/authentication/http_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ var _ = Describe("Authentication Webhooks", func() {
9595
Expect(respRecorder.Body.String()).To(Equal(expected))
9696
})
9797

98+
It("should error when given a NoBody", func() {
99+
req := &http.Request{
100+
Header: http.Header{"Content-Type": []string{"application/json"}},
101+
Method: http.MethodPost,
102+
Body: http.NoBody,
103+
}
104+
105+
expected := `{"metadata":{"creationTimestamp":null},"spec":{},"status":{"user":{},"error":"request body is empty"}}
106+
`
107+
webhook.ServeHTTP(respRecorder, req)
108+
Expect(respRecorder.Body.String()).To(Equal(expected))
109+
})
110+
98111
It("should error when given an infinite body", func() {
99112
req := &http.Request{
100113
Header: http.Header{"Content-Type": []string{"application/json"}},

0 commit comments

Comments
 (0)