@@ -332,43 +332,72 @@ func CompareRoundTrip(t *testing.T, req *roundtripper.Request, cReq *roundtrippe
332
332
expected .ExpectedRequest = & ExpectedRequest {Request : expected .Request }
333
333
}
334
334
335
- if expected .ExpectedRequest .Method == "" {
336
- expected .ExpectedRequest .Method = "GET"
337
- }
335
+ if cRes .StatusCode == 200 || cRes .StatusCode == 204 {
336
+ if cRes .StatusCode == 200 {
337
+ // The request expected to arrive at the backend is
338
+ // the same as the request made, unless otherwise
339
+ // specified.
340
+ if expected .ExpectedRequest == nil {
341
+ expected .ExpectedRequest = & ExpectedRequest {Request : expected .Request }
342
+ }
338
343
339
- if expected .ExpectedRequest .Host != "" && expected . ExpectedRequest . Host != cReq . Host {
340
- return fmt . Errorf ( " expected host to be %s, got %s" , expected .ExpectedRequest .Host , cReq . Host )
341
- }
344
+ if expected .ExpectedRequest .Method == "" {
345
+ expected .ExpectedRequest .Method = "GET"
346
+ }
342
347
343
- if expected .ExpectedRequest .Path != cReq .Path {
344
- return fmt .Errorf ("expected path to be %s, got %s" , expected .ExpectedRequest .Path , cReq .Path )
345
- }
346
- if expected .ExpectedRequest .Method != cReq .Method {
347
- return fmt .Errorf ("expected method to be %s, got %s" , expected .ExpectedRequest .Method , cReq .Method )
348
- }
349
- if expected .Namespace != cReq .Namespace {
350
- return fmt .Errorf ("expected namespace to be %s, got %s" , expected .Namespace , cReq .Namespace )
351
- }
352
- if expected .ExpectedRequest .Headers != nil {
353
- if cReq .Headers == nil {
354
- return fmt .Errorf ("no headers captured, expected %v" , len (expected .ExpectedRequest .Headers ))
348
+ if expected .ExpectedRequest .Host != "" && expected .ExpectedRequest .Host != cReq .Host {
349
+ return fmt .Errorf ("expected host to be %s, got %s" , expected .ExpectedRequest .Host , cReq .Host )
355
350
}
356
- for name , val := range cReq .Headers {
357
- cReq .Headers [strings .ToLower (name )] = val
351
+
352
+ if expected .ExpectedRequest .Path != cReq .Path {
353
+ return fmt .Errorf ("expected path to be %s, got %s" , expected .ExpectedRequest .Path , cReq .Path )
358
354
}
359
- for name , expectedVal := range expected .ExpectedRequest .Headers {
360
- actualVal , ok := cReq .Headers [strings .ToLower (name )]
361
- if ! ok {
362
- return fmt .Errorf ("expected %s header to be set, actual headers: %v" , name , cReq .Headers )
363
- } else if strings .Join (actualVal , "," ) != expectedVal {
364
- return fmt .Errorf ("expected %s header to be set to %s, got %s" , name , expectedVal , strings .Join (actualVal , "," ))
355
+ if expected .ExpectedRequest .Method != cReq .Method {
356
+ return fmt .Errorf ("expected method to be %s, got %s" , expected .ExpectedRequest .Method , cReq .Method )
357
+ }
358
+ if expected .Namespace != cReq .Namespace {
359
+ return fmt .Errorf ("expected namespace to be %s, got %s" , expected .Namespace , cReq .Namespace )
360
+ }
361
+ if expected .ExpectedRequest .Headers != nil {
362
+ if cReq .Headers == nil {
363
+ return fmt .Errorf ("no headers captured, expected %v" , len (expected .ExpectedRequest .Headers ))
364
+ }
365
+ for name , val := range cReq .Headers {
366
+ cReq .Headers [strings .ToLower (name )] = val
365
367
}
368
+ for name , expectedVal := range expected .ExpectedRequest .Headers {
369
+ actualVal , ok := cReq .Headers [strings .ToLower (name )]
370
+ if ! ok {
371
+ return fmt .Errorf ("expected %s header to be set, actual headers: %v" , name , cReq .Headers )
372
+ } else if strings .Join (actualVal , "," ) != expectedVal {
373
+ return fmt .Errorf ("expected %s header to be set to %s, got %s" , name , expectedVal , strings .Join (actualVal , "," ))
374
+ }
375
+ }
376
+ }
377
+
378
+ // Verify that headers expected *not* to be present on the
379
+ // request are actually not present.
380
+ if len (expected .ExpectedRequest .AbsentHeaders ) > 0 {
381
+ for name , val := range cReq .Headers {
382
+ cReq .Headers [strings .ToLower (name )] = val
383
+ }
384
+
385
+ for _ , name := range expected .ExpectedRequest .AbsentHeaders {
386
+ val , ok := cReq .Headers [strings .ToLower (name )]
387
+ if ok {
388
+ return fmt .Errorf ("expected %s header to not be set, got %s" , name , val )
389
+ }
390
+ }
391
+ }
392
+
393
+ if ! strings .HasPrefix (cReq .Pod , expected .Backend ) {
394
+ return fmt .Errorf ("expected pod name to start with %s, got %s" , expected .Backend , cReq .Pod )
366
395
}
367
396
}
368
397
369
398
if expected .Response .Headers != nil {
370
399
if cRes .Headers == nil {
371
- return fmt .Errorf ("no headers captured, expected %v" , len (expected .ExpectedRequest .Headers ))
400
+ return fmt .Errorf ("no headers captured, expected %v" , len (expected .Response .Headers ))
372
401
}
373
402
for name , val := range cRes .Headers {
374
403
cRes .Headers [strings .ToLower (name )] = val
0 commit comments