@@ -35,6 +35,11 @@ import (
3535
3636var _ = Describe ("Admission Webhooks" , func () {
3737
38+ const (
39+ gvkJSONv1 = `"kind":"AdmissionReview","apiVersion":"admission.k8s.io/v1"`
40+ gvkJSONv1beta1 = `"kind":"AdmissionReview","apiVersion":"admission.k8s.io/v1beta1"`
41+ )
42+
3843 Describe ("HTTP Handler" , func () {
3944 var respRecorder * httptest.ResponseRecorder
4045 webhook := & Webhook {
@@ -51,10 +56,10 @@ var _ = Describe("Admission Webhooks", func() {
5156 It ("should return bad-request when given an empty body" , func () {
5257 req := & http.Request {Body : nil }
5358
54- expected := [] byte ( `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
55- ` )
59+ expected := `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
60+ `
5661 webhook .ServeHTTP (respRecorder , req )
57- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
62+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
5863 })
5964
6065 It ("should return bad-request when given the wrong content-type" , func () {
@@ -63,10 +68,11 @@ var _ = Describe("Admission Webhooks", func() {
6368 Body : nopCloser {Reader : bytes .NewBuffer (nil )},
6469 }
6570
66- expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"contentType=application/foo, expected application/json","code":400}}}
67- ` )
71+ expected :=
72+ `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"contentType=application/foo, expected application/json","code":400}}}
73+ `
6874 webhook .ServeHTTP (respRecorder , req )
69- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
75+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
7076 })
7177
7278 It ("should return bad-request when given an undecodable body" , func () {
@@ -75,14 +81,14 @@ var _ = Describe("Admission Webhooks", func() {
7581 Body : nopCloser {Reader : bytes .NewBufferString ("{" )},
7682 }
7783
78- expected := [] byte (
84+ expected :=
7985 `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"couldn't get version/kind; json parse error: unexpected end of JSON input","code":400}}}
80- ` )
86+ `
8187 webhook .ServeHTTP (respRecorder , req )
82- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
88+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
8389 })
8490
85- It ("should return the response given by the handler" , func () {
91+ It ("should return the response given by the handler with version defaulted to v1 " , func () {
8692 req := & http.Request {
8793 Header : http.Header {"Content-Type" : []string {"application/json" }},
8894 Body : nopCloser {Reader : bytes .NewBufferString (`{"request":{}}` )},
@@ -92,10 +98,42 @@ var _ = Describe("Admission Webhooks", func() {
9298 log : logf .RuntimeLog .WithName ("webhook" ),
9399 }
94100
95- expected := []byte (`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
96- ` )
101+ expected := fmt .Sprintf (`{%s,"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
102+ ` , gvkJSONv1 )
103+ webhook .ServeHTTP (respRecorder , req )
104+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
105+ })
106+
107+ It ("should return the v1 response given by the handler" , func () {
108+ req := & http.Request {
109+ Header : http.Header {"Content-Type" : []string {"application/json" }},
110+ Body : nopCloser {Reader : bytes .NewBufferString (fmt .Sprintf (`{%s,"request":{}}` , gvkJSONv1 ))},
111+ }
112+ webhook := & Webhook {
113+ Handler : & fakeHandler {},
114+ log : logf .RuntimeLog .WithName ("webhook" ),
115+ }
116+
117+ expected := fmt .Sprintf (`{%s,"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
118+ ` , gvkJSONv1 )
119+ webhook .ServeHTTP (respRecorder , req )
120+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
121+ })
122+
123+ It ("should return the v1beta1 response given by the handler" , func () {
124+ req := & http.Request {
125+ Header : http.Header {"Content-Type" : []string {"application/json" }},
126+ Body : nopCloser {Reader : bytes .NewBufferString (fmt .Sprintf (`{%s,"request":{}}` , gvkJSONv1beta1 ))},
127+ }
128+ webhook := & Webhook {
129+ Handler : & fakeHandler {},
130+ log : logf .RuntimeLog .WithName ("webhook" ),
131+ }
132+
133+ expected := fmt .Sprintf (`{%s,"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
134+ ` , gvkJSONv1beta1 )
97135 webhook .ServeHTTP (respRecorder , req )
98- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
136+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
99137 })
100138
101139 It ("should present the Context from the HTTP request, if any" , func () {
@@ -116,13 +154,13 @@ var _ = Describe("Admission Webhooks", func() {
116154 log : logf .RuntimeLog .WithName ("webhook" ),
117155 }
118156
119- expected := [] byte ( fmt .Sprintf (`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"reason":%q,"code":200}}}
120- ` , value ) )
157+ expected := fmt .Sprintf (`{%s, "response":{"uid":"","allowed":true,"status":{"metadata":{},"reason":%q,"code":200}}}
158+ ` , gvkJSONv1 , value )
121159
122160 ctx , cancel := context .WithCancel (context .WithValue (context .Background (), key , value ))
123161 cancel ()
124162 webhook .ServeHTTP (respRecorder , req .WithContext (ctx ))
125- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
163+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
126164 })
127165 })
128166})
0 commit comments