MCPcopy Index your code
hub / github.com/moby/moby / TestClientRedirect

Function TestClientRedirect

client/client_test.go:478–531  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

476}
477
478func 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}

Callers

nothing calls this directly

Calls 8

ensureBodyFunction · 0.85
mockResponseFunction · 0.85
CheckMethod · 0.80
EqualMethod · 0.80
StringMethod · 0.65
RunMethod · 0.65
NewRequestMethod · 0.65
DoMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…