@@ -35,6 +35,11 @@ import (
35
35
36
36
var _ = Describe ("Admission Webhooks" , func () {
37
37
38
+ const (
39
+ gvkJSONv1 = `"kind":"AdmissionReview","apiVersion":"admission.k8s.io/v1"`
40
+ gvkJSONv1beta1 = `"kind":"AdmissionReview","apiVersion":"admission.k8s.io/v1beta1"`
41
+ )
42
+
38
43
Describe ("HTTP Handler" , func () {
39
44
var respRecorder * httptest.ResponseRecorder
40
45
webhook := & Webhook {
@@ -51,10 +56,10 @@ var _ = Describe("Admission Webhooks", func() {
51
56
It ("should return bad-request when given an empty body" , func () {
52
57
req := & http.Request {Body : nil }
53
58
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
+ `
56
61
webhook .ServeHTTP (respRecorder , req )
57
- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
62
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
58
63
})
59
64
60
65
It ("should return bad-request when given the wrong content-type" , func () {
@@ -63,10 +68,11 @@ var _ = Describe("Admission Webhooks", func() {
63
68
Body : nopCloser {Reader : bytes .NewBuffer (nil )},
64
69
}
65
70
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
+ `
68
74
webhook .ServeHTTP (respRecorder , req )
69
- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
75
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
70
76
})
71
77
72
78
It ("should return bad-request when given an undecodable body" , func () {
@@ -75,14 +81,14 @@ var _ = Describe("Admission Webhooks", func() {
75
81
Body : nopCloser {Reader : bytes .NewBufferString ("{" )},
76
82
}
77
83
78
- expected := [] byte (
84
+ expected :=
79
85
`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"couldn't get version/kind; json parse error: unexpected end of JSON input","code":400}}}
80
- ` )
86
+ `
81
87
webhook .ServeHTTP (respRecorder , req )
82
- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
88
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
83
89
})
84
90
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 () {
86
92
req := & http.Request {
87
93
Header : http.Header {"Content-Type" : []string {"application/json" }},
88
94
Body : nopCloser {Reader : bytes .NewBufferString (`{"request":{}}` )},
@@ -92,10 +98,42 @@ var _ = Describe("Admission Webhooks", func() {
92
98
log : logf .RuntimeLog .WithName ("webhook" ),
93
99
}
94
100
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 )
97
135
webhook .ServeHTTP (respRecorder , req )
98
- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
136
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
99
137
})
100
138
101
139
It ("should present the Context from the HTTP request, if any" , func () {
@@ -116,13 +154,13 @@ var _ = Describe("Admission Webhooks", func() {
116
154
log : logf .RuntimeLog .WithName ("webhook" ),
117
155
}
118
156
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 )
121
159
122
160
ctx , cancel := context .WithCancel (context .WithValue (context .Background (), key , value ))
123
161
cancel ()
124
162
webhook .ServeHTTP (respRecorder , req .WithContext (ctx ))
125
- Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
163
+ Expect (respRecorder .Body .String ()).To (Equal (expected ))
126
164
})
127
165
})
128
166
})
0 commit comments