@@ -21,6 +21,7 @@ import (
21
21
"context"
22
22
"io"
23
23
"net/http"
24
+ "net/http/httptest"
24
25
25
26
. "github.com/onsi/ginkgo"
26
27
. "github.com/onsi/gomega"
@@ -30,18 +31,25 @@ import (
30
31
)
31
32
32
33
var _ = Describe ("admission webhook http handler" , func () {
34
+ var w * httptest.ResponseRecorder
35
+ BeforeEach (func (done Done ) {
36
+ w = & httptest.ResponseRecorder {
37
+ Body : bytes .NewBuffer (nil ),
38
+ }
39
+ close (done )
40
+ })
41
+
33
42
Describe ("empty request body" , func () {
34
43
req := & http.Request {Body : nil }
35
44
wh := & Webhook {
36
45
Handlers : []Handler {},
37
46
}
38
- w := & fakeResponseWriter {}
47
+
39
48
expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"request body is empty","code":400}}}
40
49
` )
41
- It ("should return a response with an error " , func () {
50
+ It ("should return an error with bad-request status code " , func () {
42
51
wh .ServeHTTP (w , req )
43
- Expect (w .response ).NotTo (BeNil ())
44
- Expect (w .response ).To (Equal (expected ))
52
+ Expect (w .Body .Bytes ()).To (Equal (expected ))
45
53
})
46
54
})
47
55
@@ -53,13 +61,12 @@ var _ = Describe("admission webhook http handler", func() {
53
61
wh := & Webhook {
54
62
Handlers : []Handler {},
55
63
}
56
- w := & fakeResponseWriter {}
57
64
expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"contentType=application/foo, expect application/json","code":400}}}
58
65
` )
59
- It ("should return a response with an error " , func () {
66
+ It ("should return an error with bad-request status code " , func () {
60
67
wh .ServeHTTP (w , req )
61
- Expect (w .response ). NotTo ( BeNil ( ))
62
- Expect ( w . response ). To ( Equal ( expected ))
68
+ Expect (w .Body . Bytes ()). To ( Equal ( expected ))
69
+
63
70
})
64
71
})
65
72
@@ -72,14 +79,13 @@ var _ = Describe("admission webhook http handler", func() {
72
79
Type : types .WebhookTypeMutating ,
73
80
Handlers : []Handler {},
74
81
}
75
- w := & fakeResponseWriter {}
76
82
expected := []byte (
77
83
`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"couldn't get version/kind; json parse error: unexpected end of JSON input","code":400}}}
78
84
` )
79
- It ("should return a response with an error " , func () {
85
+ It ("should return an error with bad-request status code " , func () {
80
86
wh .ServeHTTP (w , req )
81
- Expect (w .response ). NotTo ( BeNil ( ))
82
- Expect ( w . response ). To ( Equal ( expected ))
87
+ Expect (w .Body . Bytes ()). To ( Equal ( expected ))
88
+
83
89
})
84
90
})
85
91
@@ -92,13 +98,11 @@ var _ = Describe("admission webhook http handler", func() {
92
98
Type : types .WebhookTypeMutating ,
93
99
Handlers : []Handler {},
94
100
}
95
- w := & fakeResponseWriter {}
96
101
expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"got an empty AdmissionRequest","code":400}}}
97
102
` )
98
- It ("should return a response with an error " , func () {
103
+ It ("should return an error with bad-request status code " , func () {
99
104
wh .ServeHTTP (w , req )
100
- Expect (w .response ).NotTo (BeNil ())
101
- Expect (w .response ).To (Equal (expected ))
105
+ Expect (w .Body .Bytes ()).To (Equal (expected ))
102
106
})
103
107
})
104
108
@@ -110,13 +114,12 @@ var _ = Describe("admission webhook http handler", func() {
110
114
wh := & Webhook {
111
115
Handlers : []Handler {},
112
116
}
113
- w := & fakeResponseWriter {}
114
- expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"you must specify your webhook type","code":400}}}
117
+ expected := []byte (`{"response":{"uid":"","allowed":false,"status":{"metadata":{},"message":"you must specify your webhook type","code":500}}}
115
118
` )
116
- It ("should return a response with an error" , func () {
119
+ It ("should return an error with internal- error status code " , func () {
117
120
wh .ServeHTTP (w , req )
118
- Expect (w .response ). NotTo ( BeNil ( ))
119
- Expect ( w . response ). To ( Equal ( expected ))
121
+ Expect (w .Body . Bytes ()). To ( Equal ( expected ))
122
+
120
123
})
121
124
})
122
125
@@ -131,36 +134,17 @@ var _ = Describe("admission webhook http handler", func() {
131
134
Handlers : []Handler {h },
132
135
KVMap : map [string ]interface {}{"foo" : "bar" },
133
136
}
134
- w := & fakeResponseWriter {}
135
137
expected := []byte (`{"response":{"uid":"","allowed":true}}
136
138
` )
137
139
It ("should return a response successfully" , func () {
138
140
wh .ServeHTTP (w , req )
139
- Expect (w .response ).NotTo (BeNil ())
140
- Expect (w .response ).To (Equal (expected ))
141
+ Expect (w .Body .Bytes ()).To (Equal (expected ))
141
142
Expect (h .invoked ).To (BeTrue ())
142
143
Expect (h .valueFromContext ).To (Equal ("bar" ))
143
144
})
144
145
})
145
146
})
146
147
147
- type fakeResponseWriter struct {
148
- response []byte
149
- }
150
-
151
- var _ http.ResponseWriter = & fakeResponseWriter {}
152
-
153
- func (w * fakeResponseWriter ) Header () http.Header {
154
- return nil
155
- }
156
-
157
- func (w * fakeResponseWriter ) Write (resp []byte ) (int , error ) {
158
- w .response = append (w .response , resp ... )
159
- return len (resp ), nil
160
- }
161
-
162
- func (w * fakeResponseWriter ) WriteHeader (statusCode int ) {}
163
-
164
148
type nopCloser struct {
165
149
io.Reader
166
150
}
0 commit comments