@@ -29,98 +29,66 @@ import (
29
29
admissionv1beta1 "k8s.io/api/admission/v1beta1"
30
30
)
31
31
32
- var _ = Describe ("admission webhook http handler" , func () {
33
- var w * httptest.ResponseRecorder
34
- BeforeEach (func (done Done ) {
35
- w = & httptest.ResponseRecorder {
36
- Body : bytes .NewBuffer (nil ),
32
+ var _ = Describe ("Admission Webhooks" , func () {
33
+
34
+ Describe ("HTTP Handler" , func () {
35
+ var respRecorder * httptest.ResponseRecorder
36
+ BeforeEach (func () {
37
+ respRecorder = & httptest.ResponseRecorder {
38
+ Body : bytes .NewBuffer (nil ),
39
+ }
40
+ })
41
+ webhook := & Webhook {
42
+ Handler : nil ,
37
43
}
38
- close (done )
39
- })
40
44
41
- Describe ("empty request body" , func () {
42
- req := & http.Request {Body : nil }
43
- wh := & Webhook {
44
- Handlers : []Handler {},
45
- }
45
+ It ("should return bad-request when given an empty body" , func () {
46
+ req := & http.Request {Body : nil }
46
47
47
- expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
48
+ expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
48
49
` )
49
- It ("should return an error with bad-request status code" , func () {
50
- wh .ServeHTTP (w , req )
51
- Expect (w .Body .Bytes ()).To (Equal (expected ))
50
+ webhook .ServeHTTP (respRecorder , req )
51
+ Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
52
52
})
53
- })
54
53
55
- Describe ("wrong content type" , func () {
56
- req := & http.Request {
57
- Header : http.Header {"Content-Type" : []string {"application/foo" }},
58
- Body : nopCloser {Reader : bytes .NewBuffer (nil )},
59
- }
60
- wh := & Webhook {
61
- Handlers : []Handler {},
62
- }
63
- expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"contentType=application/foo, expect application/json","code":400}}}
64
- ` )
65
- It ("should return an error with bad-request status code" , func () {
66
- wh .ServeHTTP (w , req )
67
- Expect (w .Body .Bytes ()).To (Equal (expected ))
54
+ It ("should return bad-request when given the wrong content-type" , func () {
55
+ req := & http.Request {
56
+ Header : http.Header {"Content-Type" : []string {"application/foo" }},
57
+ Body : nopCloser {Reader : bytes .NewBuffer (nil )},
58
+ }
68
59
69
- })
70
- })
71
-
72
- Describe ("can't decode body" , func () {
73
- req := & http.Request {
74
- Header : http.Header {"Content-Type" : []string {"application/json" }},
75
- Body : nopCloser {Reader : bytes .NewBufferString ("{" )},
76
- }
77
- wh := & Webhook {
78
- Type : MutatingWebhook ,
79
- Handlers : []Handler {},
80
- }
81
- expected := []byte (
82
- `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"couldn't get version/kind; json parse error: unexpected end of JSON input","code":400}}}
60
+ expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"contentType=application/foo, expected application/json","code":400}}}
83
61
` )
84
- It ("should return an error with bad-request status code" , func () {
85
- wh .ServeHTTP (w , req )
86
- Expect (w .Body .Bytes ()).To (Equal (expected ))
87
-
62
+ webhook .ServeHTTP (respRecorder , req )
63
+ Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
88
64
})
89
- })
90
65
91
- Describe ("no webhook type" , func () {
92
- req := & http.Request {
93
- Header : http.Header {"Content-Type" : []string {"application/json" }},
94
- Body : nopCloser {Reader : bytes .NewBufferString (`{"request":{}}` )},
95
- }
96
- wh := & Webhook {
97
- Handlers : []Handler {},
98
- }
99
- expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"you must specify your webhook type","code":500}}}
100
- ` )
101
- It ("should return an error with internal-error status code" , func () {
102
- wh .ServeHTTP (w , req )
103
- Expect (w .Body .Bytes ()).To (Equal (expected ))
66
+ It ("should return bad-request when given an undecodable body" , func () {
67
+ req := & http.Request {
68
+ Header : http.Header {"Content-Type" : []string {"application/json" }},
69
+ Body : nopCloser {Reader : bytes .NewBufferString ("{" )},
70
+ }
104
71
72
+ expected := []byte (
73
+ `{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"couldn't get version/kind; json parse error: unexpected end of JSON input","code":400}}}
74
+ ` )
75
+ webhook .ServeHTTP (respRecorder , req )
76
+ Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
105
77
})
106
- })
107
78
108
- Describe ("handler can be invoked" , func () {
109
- req := & http.Request {
110
- Header : http.Header {"Content-Type" : []string {"application/json" }},
111
- Body : nopCloser {Reader : bytes .NewBufferString (`{"request":{}}` )},
112
- }
113
- h := & fakeHandler {}
114
- wh := & Webhook {
115
- Type : ValidatingWebhook ,
116
- Handlers : []Handler {h },
117
- }
118
- expected := []byte (`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
79
+ It ("should return the response given by the handler" , func () {
80
+ req := & http.Request {
81
+ Header : http.Header {"Content-Type" : []string {"application/json" }},
82
+ Body : nopCloser {Reader : bytes .NewBufferString (`{"request":{}}` )},
83
+ }
84
+ webhook := & Webhook {
85
+ Handler : & fakeHandler {},
86
+ }
87
+
88
+ expected := []byte (`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"code":200}}}
119
89
` )
120
- It ("should return a response successfully" , func () {
121
- wh .ServeHTTP (w , req )
122
- Expect (w .Body .Bytes ()).To (Equal (expected ))
123
- Expect (h .invoked ).To (BeTrue ())
90
+ webhook .ServeHTTP (respRecorder , req )
91
+ Expect (respRecorder .Body .Bytes ()).To (Equal (expected ))
124
92
})
125
93
})
126
94
})
0 commit comments