| 347 | } |
| 348 | |
| 349 | func TestWrappers(t *testing.T) { |
| 350 | resp1 := &http.Response{} |
| 351 | wrapperResp1 := func(rt http.RoundTripper) http.RoundTripper { |
| 352 | return &fakeRoundTripper{Resp: resp1} |
| 353 | } |
| 354 | resp2 := &http.Response{} |
| 355 | wrapperResp2 := func(rt http.RoundTripper) http.RoundTripper { |
| 356 | return &fakeRoundTripper{Resp: resp2} |
| 357 | } |
| 358 | |
| 359 | tests := []struct { |
| 360 | name string |
| 361 | fns []WrapperFunc |
| 362 | wantNil bool |
| 363 | want func(*http.Response) bool |
| 364 | }{ |
| 365 | {fns: []WrapperFunc{}, wantNil: true}, |
| 366 | {fns: []WrapperFunc{nil, nil}, wantNil: true}, |
| 367 | {fns: []WrapperFunc{nil}, wantNil: false}, |
| 368 | |
| 369 | {fns: []WrapperFunc{nil, wrapperResp1}, want: func(resp *http.Response) bool { return resp == resp1 }}, |
| 370 | {fns: []WrapperFunc{wrapperResp1, nil}, want: func(resp *http.Response) bool { return resp == resp1 }}, |
| 371 | {fns: []WrapperFunc{nil, wrapperResp1, nil}, want: func(resp *http.Response) bool { return resp == resp1 }}, |
| 372 | {fns: []WrapperFunc{nil, wrapperResp1, wrapperResp2}, want: func(resp *http.Response) bool { return resp == resp2 }}, |
| 373 | {fns: []WrapperFunc{wrapperResp1, wrapperResp2}, want: func(resp *http.Response) bool { return resp == resp2 }}, |
| 374 | {fns: []WrapperFunc{wrapperResp2, wrapperResp1}, want: func(resp *http.Response) bool { return resp == resp1 }}, |
| 375 | |
| 376 | {fns: []WrapperFunc{testChain("1")}, want: func(resp *http.Response) bool { return resp.Header.Get("Value") == "1" }}, |
| 377 | {fns: []WrapperFunc{testChain("1"), testChain("2")}, want: func(resp *http.Response) bool { return resp.Header.Get("Value") == "12" }}, |
| 378 | {fns: []WrapperFunc{testChain("2"), testChain("1")}, want: func(resp *http.Response) bool { return resp.Header.Get("Value") == "21" }}, |
| 379 | {fns: []WrapperFunc{testChain("1"), testChain("2"), testChain("3")}, want: func(resp *http.Response) bool { return resp.Header.Get("Value") == "123" }}, |
| 380 | } |
| 381 | for _, tt := range tests { |
| 382 | t.Run(tt.name, func(t *testing.T) { |
| 383 | got := Wrappers(tt.fns...) |
| 384 | if got == nil != tt.wantNil { |
| 385 | t.Errorf("Wrappers() = %v", got) |
| 386 | return |
| 387 | } |
| 388 | if got == nil { |
| 389 | return |
| 390 | } |
| 391 | |
| 392 | rt := &fakeRoundTripper{Resp: &http.Response{}} |
| 393 | nested := got(rt) |
| 394 | req := &http.Request{} |
| 395 | resp, _ := nested.RoundTrip(req) |
| 396 | if tt.want != nil && !tt.want(resp) { |
| 397 | t.Errorf("unexpected response: %#v", resp) |
| 398 | } |
| 399 | }) |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | func Test_contextCanceller_RoundTrip(t *testing.T) { |
| 404 | tests := []struct { |