request_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math/rand"
  9. "net/http"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/docker/docker/api/types/container"
  14. "github.com/docker/docker/errdefs"
  15. "gotest.tools/v3/assert"
  16. is "gotest.tools/v3/assert/cmp"
  17. )
  18. // TestSetHostHeader should set fake host for local communications, set real host
  19. // for normal communications.
  20. func TestSetHostHeader(t *testing.T) {
  21. const testEndpoint = "/test"
  22. testCases := []struct {
  23. host string
  24. expectedHost string
  25. expectedURLHost string
  26. }{
  27. {
  28. host: "unix:///var/run/docker.sock",
  29. expectedHost: DummyHost,
  30. expectedURLHost: "/var/run/docker.sock",
  31. },
  32. {
  33. host: "npipe:////./pipe/docker_engine",
  34. expectedHost: DummyHost,
  35. expectedURLHost: "//./pipe/docker_engine",
  36. },
  37. {
  38. host: "tcp://0.0.0.0:4243",
  39. expectedHost: "",
  40. expectedURLHost: "0.0.0.0:4243",
  41. },
  42. {
  43. host: "tcp://localhost:4243",
  44. expectedHost: "",
  45. expectedURLHost: "localhost:4243",
  46. },
  47. }
  48. for _, tc := range testCases {
  49. tc := tc
  50. t.Run(tc.host, func(t *testing.T) {
  51. hostURL, err := ParseHostURL(tc.host)
  52. assert.Check(t, err)
  53. client := &Client{
  54. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  55. if !strings.HasPrefix(req.URL.Path, testEndpoint) {
  56. return nil, fmt.Errorf("expected URL %q, got %q", testEndpoint, req.URL)
  57. }
  58. if req.Host != tc.expectedHost {
  59. return nil, fmt.Errorf("wxpected host %q, got %q", tc.expectedHost, req.Host)
  60. }
  61. if req.URL.Host != tc.expectedURLHost {
  62. return nil, fmt.Errorf("expected URL host %q, got %q", tc.expectedURLHost, req.URL.Host)
  63. }
  64. return &http.Response{
  65. StatusCode: http.StatusOK,
  66. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  67. }, nil
  68. }),
  69. proto: hostURL.Scheme,
  70. addr: hostURL.Host,
  71. basePath: hostURL.Path,
  72. }
  73. _, err = client.sendRequest(context.Background(), http.MethodGet, testEndpoint, nil, nil, nil)
  74. assert.Check(t, err)
  75. })
  76. }
  77. }
  78. // TestPlainTextError tests the server returning an error in plain text for
  79. // backwards compatibility with API versions <1.24. All other tests use
  80. // errors returned as JSON
  81. func TestPlainTextError(t *testing.T) {
  82. client := &Client{
  83. client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
  84. }
  85. _, err := client.ContainerList(context.Background(), container.ListOptions{})
  86. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  87. }
  88. func TestInfiniteError(t *testing.T) {
  89. infinitR := rand.New(rand.NewSource(42))
  90. client := &Client{
  91. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  92. resp := &http.Response{
  93. StatusCode: http.StatusInternalServerError,
  94. Header: http.Header{},
  95. Body: io.NopCloser(infinitR),
  96. }
  97. return resp, nil
  98. }),
  99. }
  100. _, err := client.Ping(context.Background())
  101. assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
  102. }
  103. func TestCanceledContext(t *testing.T) {
  104. const testEndpoint = "/test"
  105. client := &Client{
  106. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  107. assert.Check(t, is.ErrorType(req.Context().Err(), context.Canceled))
  108. return nil, context.Canceled
  109. }),
  110. }
  111. ctx, cancel := context.WithCancel(context.Background())
  112. cancel()
  113. _, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
  114. assert.Check(t, is.ErrorType(err, errdefs.IsCancelled))
  115. assert.Check(t, errors.Is(err, context.Canceled))
  116. }
  117. func TestDeadlineExceededContext(t *testing.T) {
  118. const testEndpoint = "/test"
  119. client := &Client{
  120. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  121. assert.Check(t, is.ErrorType(req.Context().Err(), context.DeadlineExceeded))
  122. return nil, context.DeadlineExceeded
  123. }),
  124. }
  125. ctx, cancel := context.WithDeadline(context.Background(), time.Now())
  126. defer cancel()
  127. <-ctx.Done()
  128. _, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
  129. assert.Check(t, is.ErrorType(err, errdefs.IsDeadline))
  130. assert.Check(t, errors.Is(err, context.DeadlineExceeded))
  131. }