@@ -133,14 +133,28 @@ func TestRESTHelperDelete(t *testing.T) {
133
133
}
134
134
135
135
func TestRESTHelperCreate (t * testing.T ) {
136
+ expectPost := func (req * http.Request ) bool {
137
+ if req .Method != "POST" {
138
+ t .Errorf ("unexpected method: %#v" , req )
139
+ return false
140
+ }
141
+ if req .URL .Query ().Get ("namespace" ) != "bar" {
142
+ t .Errorf ("url doesn't contain namespace: %#v" , req )
143
+ return false
144
+ }
145
+ return true
146
+ }
147
+
136
148
tests := []struct {
137
- Resp * http.Response
138
- HttpErr error
139
- Object runtime.Object
149
+ Resp * http.Response
150
+ RespFunc httpClientFunc
151
+ HttpErr error
152
+ Modify bool
153
+ Object runtime.Object
140
154
141
- Err bool
142
- Data [] byte
143
- Req func (* http.Request ) bool
155
+ ExpectObject runtime. Object
156
+ Err bool
157
+ Req func (* http.Request ) bool
144
158
}{
145
159
{
146
160
HttpErr : errors .New ("failure" ),
@@ -158,48 +172,65 @@ func TestRESTHelperCreate(t *testing.T) {
158
172
StatusCode : http .StatusOK ,
159
173
Body : objBody (& api.Status {Status : api .StatusSuccess }),
160
174
},
161
- Object : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" }},
162
- Req : func (req * http.Request ) bool {
163
- if req .Method != "POST" {
164
- t .Errorf ("unexpected method: %#v" , req )
165
- return false
166
- }
167
- if req .URL .Query ().Get ("namespace" ) != "bar" {
168
- t .Errorf ("url doesn't contain namespace: %#v" , req )
169
- return false
170
- }
171
- return true
172
- },
175
+ Object : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" }},
176
+ ExpectObject : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" }},
177
+ Req : expectPost ,
178
+ },
179
+ {
180
+ Modify : false ,
181
+ Object : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" , ResourceVersion : "10" }},
182
+ ExpectObject : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" , ResourceVersion : "10" }},
183
+ Resp : & http.Response {StatusCode : http .StatusOK , Body : objBody (& api.Status {Status : api .StatusSuccess })},
184
+ Req : expectPost ,
185
+ },
186
+ {
187
+ Modify : true ,
188
+ Object : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" , ResourceVersion : "10" }},
189
+ ExpectObject : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" }},
190
+ Resp : & http.Response {StatusCode : http .StatusOK , Body : objBody (& api.Status {Status : api .StatusSuccess })},
191
+ Req : expectPost ,
173
192
},
174
193
}
175
- for _ , test := range tests {
194
+ for i , test := range tests {
176
195
client := & FakeRESTClient {
177
196
Resp : test .Resp ,
178
197
Err : test .HttpErr ,
179
198
}
199
+ if test .RespFunc != nil {
200
+ client .Client = test .RespFunc
201
+ }
180
202
modifier := & RESTHelper {
181
203
RESTClient : client ,
204
+ Codec : testapi .Codec (),
205
+ Versioner : testapi .MetadataAccessor (),
182
206
}
183
- data := test . Data
207
+ data := [] byte {}
184
208
if test .Object != nil {
185
209
data = []byte (runtime .EncodeOrDie (testapi .Codec (), test .Object ))
186
210
}
187
- err := modifier .Create ("bar" , data )
211
+ err := modifier .Create ("bar" , test . Modify , data )
188
212
if (err != nil ) != test .Err {
189
- t .Errorf ("unexpected error: %f %v" , test .Err , err )
213
+ t .Errorf ("%d: unexpected error: %f %v" , i , test .Err , err )
190
214
}
191
215
if err != nil {
192
216
continue
193
217
}
194
218
if test .Req != nil && ! test .Req (client .Req ) {
195
- t .Errorf ("unexpected request: %#v" , client .Req )
219
+ t .Errorf ("%d: unexpected request: %#v" , i , client .Req )
196
220
}
197
- if test .Data != nil {
198
- body , _ := ioutil .ReadAll (client .Req .Body )
199
- if ! reflect .DeepEqual (test .Data , body ) {
200
- t .Errorf ("unexpected body: %s" , string (body ))
201
- }
221
+ body , err := ioutil .ReadAll (client .Req .Body )
222
+ if err != nil {
223
+ t .Fatalf ("%d: unexpected error: %#v" , i , err )
224
+ }
225
+ t .Logf ("got body: %s" , string (body ))
226
+ expect := []byte {}
227
+ if test .ExpectObject != nil {
228
+ expect = []byte (runtime .EncodeOrDie (testapi .Codec (), test .ExpectObject ))
202
229
}
230
+ if ! reflect .DeepEqual (expect , body ) {
231
+ t .Errorf ("%d: unexpected body: %s" , i , string (body ))
232
+ }
233
+
203
234
}
204
235
}
205
236
@@ -268,6 +299,22 @@ func TestRESTHelperGet(t *testing.T) {
268
299
}
269
300
270
301
func TestRESTHelperUpdate (t * testing.T ) {
302
+ expectPut := func (req * http.Request ) bool {
303
+ if req .Method != "PUT" {
304
+ t .Errorf ("unexpected method: %#v" , req )
305
+ return false
306
+ }
307
+ if ! strings .HasSuffix (req .URL .Path , "/foo" ) {
308
+ t .Errorf ("url doesn't contain name: %#v" , req )
309
+ return false
310
+ }
311
+ if req .URL .Query ().Get ("namespace" ) != "bar" {
312
+ t .Errorf ("url doesn't contain namespace: %#v" , req )
313
+ return false
314
+ }
315
+ return true
316
+ }
317
+
271
318
tests := []struct {
272
319
Resp * http.Response
273
320
RespFunc httpClientFunc
@@ -298,21 +345,7 @@ func TestRESTHelperUpdate(t *testing.T) {
298
345
StatusCode : http .StatusOK ,
299
346
Body : objBody (& api.Status {Status : api .StatusSuccess }),
300
347
},
301
- Req : func (req * http.Request ) bool {
302
- if req .Method != "PUT" {
303
- t .Errorf ("unexpected method: %#v" , req )
304
- return false
305
- }
306
- if ! strings .HasSuffix (req .URL .Path , "/foo" ) {
307
- t .Errorf ("url doesn't contain name: %#v" , req )
308
- return false
309
- }
310
- if req .URL .Query ().Get ("namespace" ) != "bar" {
311
- t .Errorf ("url doesn't contain namespace: %#v" , req )
312
- return false
313
- }
314
- return true
315
- },
348
+ Req : expectPut ,
316
349
},
317
350
{
318
351
Object : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" }},
@@ -325,41 +358,13 @@ func TestRESTHelperUpdate(t *testing.T) {
325
358
}
326
359
return & http.Response {StatusCode : http .StatusOK , Body : objBody (& api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" , ResourceVersion : "10" }})}, nil
327
360
},
328
- Req : func (req * http.Request ) bool {
329
- if req .Method != "PUT" {
330
- t .Errorf ("unexpected method: %#v" , req )
331
- return false
332
- }
333
- if ! strings .HasSuffix (req .URL .Path , "/foo" ) {
334
- t .Errorf ("url doesn't contain name: %#v" , req )
335
- return false
336
- }
337
- if req .URL .Query ().Get ("namespace" ) != "bar" {
338
- t .Errorf ("url doesn't contain namespace: %#v" , req )
339
- return false
340
- }
341
- return true
342
- },
361
+ Req : expectPut ,
343
362
},
344
363
{
345
364
Object : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" , ResourceVersion : "10" }},
346
365
ExpectObject : & api.Pod {ObjectMeta : api.ObjectMeta {Name : "foo" , ResourceVersion : "10" }},
347
366
Resp : & http.Response {StatusCode : http .StatusOK , Body : objBody (& api.Status {Status : api .StatusSuccess })},
348
- Req : func (req * http.Request ) bool {
349
- if req .Method != "PUT" {
350
- t .Errorf ("unexpected method: %#v" , req )
351
- return false
352
- }
353
- if ! strings .HasSuffix (req .URL .Path , "/foo" ) {
354
- t .Errorf ("url doesn't contain name: %#v" , req )
355
- return false
356
- }
357
- if req .URL .Query ().Get ("namespace" ) != "bar" {
358
- t .Errorf ("url doesn't contain namespace: %#v" , req )
359
- return false
360
- }
361
- return true
362
- },
367
+ Req : expectPut ,
363
368
},
364
369
}
365
370
for i , test := range tests {
0 commit comments