(t *testing.T)
| 476 | } |
| 477 | |
| 478 | func TestClientRedirect(t *testing.T) { |
| 479 | client := &http.Client{ |
| 480 | CheckRedirect: CheckRedirect, |
| 481 | Transport: ensureBody(func(req *http.Request) (*http.Response, error) { |
| 482 | if req.URL.String() == "/bla" { |
| 483 | return mockResponse(http.StatusNotFound, nil, "")(req) |
| 484 | } |
| 485 | return mockResponse(http.StatusMovedPermanently, http.Header{"Location": {"/bla"}}, "")(req) |
| 486 | }), |
| 487 | } |
| 488 | |
| 489 | tests := []struct { |
| 490 | httpMethod string |
| 491 | expectedErr *url.Error |
| 492 | statusCode int |
| 493 | }{ |
| 494 | { |
| 495 | httpMethod: http.MethodGet, |
| 496 | statusCode: http.StatusMovedPermanently, |
| 497 | }, |
| 498 | { |
| 499 | httpMethod: http.MethodPost, |
| 500 | expectedErr: &url.Error{Op: "Post", URL: "/bla", Err: ErrRedirect}, |
| 501 | statusCode: http.StatusMovedPermanently, |
| 502 | }, |
| 503 | { |
| 504 | httpMethod: http.MethodPut, |
| 505 | expectedErr: &url.Error{Op: "Put", URL: "/bla", Err: ErrRedirect}, |
| 506 | statusCode: http.StatusMovedPermanently, |
| 507 | }, |
| 508 | { |
| 509 | httpMethod: http.MethodDelete, |
| 510 | expectedErr: &url.Error{Op: "Delete", URL: "/bla", Err: ErrRedirect}, |
| 511 | statusCode: http.StatusMovedPermanently, |
| 512 | }, |
| 513 | } |
| 514 | |
| 515 | for _, tc := range tests { |
| 516 | t.Run(tc.httpMethod, func(t *testing.T) { |
| 517 | req, err := http.NewRequest(tc.httpMethod, "/redirectme", http.NoBody) |
| 518 | assert.NilError(t, err) |
| 519 | resp, err := client.Do(req) |
| 520 | assert.Check(t, is.Equal(resp.StatusCode, tc.statusCode)) |
| 521 | if tc.expectedErr == nil { |
| 522 | assert.NilError(t, err) |
| 523 | } else { |
| 524 | assert.Check(t, is.ErrorType(err, &url.Error{})) |
| 525 | var urlError *url.Error |
| 526 | assert.Check(t, errors.As(err, &urlError), "%T is not *url.Error", err) |
| 527 | assert.Check(t, is.Equal(*urlError, *tc.expectedErr)) |
| 528 | } |
| 529 | }) |
| 530 | } |
| 531 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…