Skip to content

Commit 1ca8834

Browse files
committed
add missing NoBody handling
Signed-off-by: Tim Ramlot <[email protected]>
1 parent 13c946d commit 1ca8834

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
@@ -47,7 +47,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4747
ctx = wh.WithContextFunc(ctx, r)
4848
}
4949

50-
if r.Body == nil {
50+
if r.Body == nil || r.Body == http.NoBody {
5151
err := errors.New("request body is empty")
5252
wh.getLogger(nil).Error(err, "bad request")
5353
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
@@ -84,6 +84,19 @@ var _ = Describe("Admission Webhooks", func() {
8484
Expect(respRecorder.Body.String()).To(Equal(expected))
8585
})
8686

87+
It("should error when given a NoBody", func() {
88+
req := &http.Request{
89+
Header: http.Header{"Content-Type": []string{"application/json"}},
90+
Method: http.MethodPost,
91+
Body: http.NoBody,
92+
}
93+
94+
expected := `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
95+
`
96+
webhook.ServeHTTP(respRecorder, req)
97+
Expect(respRecorder.Body.String()).To(Equal(expected))
98+
})
99+
87100
It("should return the response given by the handler with version defaulted to v1", func() {
88101
req := &http.Request{
89102
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
@@ -47,7 +47,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4747
ctx = wh.WithContextFunc(ctx, r)
4848
}
4949

50-
if r.Body == nil {
50+
if r.Body == nil || r.Body == http.NoBody {
5151
err := errors.New("request body is empty")
5252
wh.getLogger(nil).Error(err, "bad request")
5353
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
@@ -94,6 +94,19 @@ var _ = Describe("Authentication Webhooks", func() {
9494
Expect(respRecorder.Body.String()).To(Equal(expected))
9595
})
9696

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

0 commit comments

Comments
 (0)