@@ -85,6 +85,7 @@ type ExpectedRequest struct {
85
85
// Response defines expected properties of a response from a backend.
86
86
type Response struct {
87
87
StatusCode int
88
+ StatusCodes []int // alternative to StatusCode, allows multiple acceptable codes
88
89
Headers map [string ]string
89
90
AbsentHeaders []string
90
91
@@ -263,54 +264,88 @@ func CompareRequest(t *testing.T, req *roundtripper.Request, cReq *roundtripper.
263
264
return nil
264
265
}
265
266
}
266
- if expected .Response .StatusCode != cRes .StatusCode {
267
+
268
+ if len (expected .Response .StatusCodes ) > 0 {
269
+ matched := false
270
+ for _ , code := range expected .Response .StatusCodes {
271
+ if code == cRes .StatusCode {
272
+ matched = true
273
+ break
274
+ }
275
+ }
276
+ if ! matched {
277
+ return fmt .Errorf ("expected status code to be one of %v, got %d" , expected .Response .StatusCodes , cRes .StatusCode )
278
+ }
279
+ } else if expected .Response .StatusCode != cRes .StatusCode {
267
280
return fmt .Errorf ("expected status code to be %d, got %d" , expected .Response .StatusCode , cRes .StatusCode )
268
281
}
269
- if cRes .StatusCode == 200 {
270
- // The request expected to arrive at the backend is
271
- // the same as the request made, unless otherwise
272
- // specified.
273
- if expected .ExpectedRequest == nil {
274
- expected .ExpectedRequest = & ExpectedRequest {Request : expected .Request }
275
- }
276
282
277
- if expected .ExpectedRequest .Method == "" {
278
- expected .ExpectedRequest .Method = "GET"
279
- }
283
+ if cRes .StatusCode == 200 || cRes .StatusCode == 204 {
284
+ if cRes .StatusCode == 200 {
285
+ // The request expected to arrive at the backend is
286
+ // the same as the request made, unless otherwise
287
+ // specified.
288
+ if expected .ExpectedRequest == nil {
289
+ expected .ExpectedRequest = & ExpectedRequest {Request : expected .Request }
290
+ }
280
291
281
- if expected .ExpectedRequest .Host != "" && expected . ExpectedRequest . Host != cReq . Host {
282
- return fmt . Errorf ( " expected host to be %s, got %s" , expected .ExpectedRequest .Host , cReq . Host )
283
- }
292
+ if expected .ExpectedRequest .Method == "" {
293
+ expected .ExpectedRequest .Method = "GET"
294
+ }
284
295
285
- if expected .ExpectedRequest .Path != cReq .Path {
286
- return fmt .Errorf ("expected path to be %s, got %s" , expected .ExpectedRequest .Path , cReq .Path )
287
- }
288
- if expected .ExpectedRequest .Method != cReq .Method {
289
- return fmt .Errorf ("expected method to be %s, got %s" , expected .ExpectedRequest .Method , cReq .Method )
290
- }
291
- if expected .Namespace != cReq .Namespace {
292
- return fmt .Errorf ("expected namespace to be %s, got %s" , expected .Namespace , cReq .Namespace )
293
- }
294
- if expected .ExpectedRequest .Headers != nil {
295
- if cReq .Headers == nil {
296
- return fmt .Errorf ("no headers captured, expected %v" , len (expected .ExpectedRequest .Headers ))
296
+ if expected .ExpectedRequest .Host != "" && expected .ExpectedRequest .Host != cReq .Host {
297
+ return fmt .Errorf ("expected host to be %s, got %s" , expected .ExpectedRequest .Host , cReq .Host )
297
298
}
298
- for name , val := range cReq .Headers {
299
- cReq .Headers [strings .ToLower (name )] = val
299
+
300
+ if expected .ExpectedRequest .Path != cReq .Path {
301
+ return fmt .Errorf ("expected path to be %s, got %s" , expected .ExpectedRequest .Path , cReq .Path )
300
302
}
301
- for name , expectedVal := range expected .ExpectedRequest .Headers {
302
- actualVal , ok := cReq .Headers [strings .ToLower (name )]
303
- if ! ok {
304
- return fmt .Errorf ("expected %s header to be set, actual headers: %v" , name , cReq .Headers )
305
- } else if strings .Join (actualVal , "," ) != expectedVal {
306
- return fmt .Errorf ("expected %s header to be set to %s, got %s" , name , expectedVal , strings .Join (actualVal , "," ))
303
+ if expected .ExpectedRequest .Method != cReq .Method {
304
+ return fmt .Errorf ("expected method to be %s, got %s" , expected .ExpectedRequest .Method , cReq .Method )
305
+ }
306
+ if expected .Namespace != cReq .Namespace {
307
+ return fmt .Errorf ("expected namespace to be %s, got %s" , expected .Namespace , cReq .Namespace )
308
+ }
309
+ if expected .ExpectedRequest .Headers != nil {
310
+ if cReq .Headers == nil {
311
+ return fmt .Errorf ("no headers captured, expected %v" , len (expected .ExpectedRequest .Headers ))
312
+ }
313
+ for name , val := range cReq .Headers {
314
+ cReq .Headers [strings .ToLower (name )] = val
315
+ }
316
+ for name , expectedVal := range expected .ExpectedRequest .Headers {
317
+ actualVal , ok := cReq .Headers [strings .ToLower (name )]
318
+ if ! ok {
319
+ return fmt .Errorf ("expected %s header to be set, actual headers: %v" , name , cReq .Headers )
320
+ } else if strings .Join (actualVal , "," ) != expectedVal {
321
+ return fmt .Errorf ("expected %s header to be set to %s, got %s" , name , expectedVal , strings .Join (actualVal , "," ))
322
+ }
323
+ }
324
+ }
325
+
326
+ // Verify that headers expected *not* to be present on the
327
+ // request are actually not present.
328
+ if len (expected .ExpectedRequest .AbsentHeaders ) > 0 {
329
+ for name , val := range cReq .Headers {
330
+ cReq .Headers [strings .ToLower (name )] = val
307
331
}
332
+
333
+ for _ , name := range expected .ExpectedRequest .AbsentHeaders {
334
+ val , ok := cReq .Headers [strings .ToLower (name )]
335
+ if ok {
336
+ return fmt .Errorf ("expected %s header to not be set, got %s" , name , val )
337
+ }
338
+ }
339
+ }
340
+
341
+ if ! strings .HasPrefix (cReq .Pod , expected .Backend ) {
342
+ return fmt .Errorf ("expected pod name to start with %s, got %s" , expected .Backend , cReq .Pod )
308
343
}
309
344
}
310
345
311
346
if expected .Response .Headers != nil {
312
347
if cRes .Headers == nil {
313
- return fmt .Errorf ("no headers captured, expected %v" , len (expected .ExpectedRequest .Headers ))
348
+ return fmt .Errorf ("no headers captured, expected %v" , len (expected .Response .Headers ))
314
349
}
315
350
for name , val := range cRes .Headers {
316
351
cRes .Headers [strings .ToLower (name )] = val
@@ -344,25 +379,6 @@ func CompareRequest(t *testing.T, req *roundtripper.Request, cReq *roundtripper.
344
379
}
345
380
}
346
381
}
347
-
348
- // Verify that headers expected *not* to be present on the
349
- // request are actually not present.
350
- if len (expected .ExpectedRequest .AbsentHeaders ) > 0 {
351
- for name , val := range cReq .Headers {
352
- cReq .Headers [strings .ToLower (name )] = val
353
- }
354
-
355
- for _ , name := range expected .ExpectedRequest .AbsentHeaders {
356
- val , ok := cReq .Headers [strings .ToLower (name )]
357
- if ok {
358
- return fmt .Errorf ("expected %s header to not be set, got %s" , name , val )
359
- }
360
- }
361
- }
362
-
363
- if ! strings .HasPrefix (cReq .Pod , expected .Backend ) {
364
- return fmt .Errorf ("expected pod name to start with %s, got %s" , expected .Backend , cReq .Pod )
365
- }
366
382
} else if roundtripper .IsRedirect (cRes .StatusCode ) {
367
383
if expected .RedirectRequest == nil {
368
384
return nil
@@ -424,6 +440,11 @@ func (er *ExpectedResponse) GetTestCaseName(i int) string {
424
440
if er .Backend != "" {
425
441
return fmt .Sprintf ("%s should go to %s" , reqStr , er .Backend )
426
442
}
443
+
444
+ if len (er .Response .StatusCodes ) > 0 {
445
+ return fmt .Sprintf ("%s should receive one of %v" , reqStr , er .Response .StatusCodes )
446
+ }
447
+
427
448
return fmt .Sprintf ("%s should receive a %d" , reqStr , er .Response .StatusCode )
428
449
}
429
450
0 commit comments