request_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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"
  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. testURL := "/test"
  22. testCases := []struct {
  23. host string
  24. expectedHost string
  25. expectedURLHost string
  26. }{
  27. {
  28. "unix:///var/run/docker.sock",
  29. "docker",
  30. "/var/run/docker.sock",
  31. },
  32. {
  33. "npipe:////./pipe/docker_engine",
  34. "docker",
  35. "//./pipe/docker_engine",
  36. },
  37. {
  38. "tcp://0.0.0.0:4243",
  39. "",
  40. "0.0.0.0:4243",
  41. },
  42. {
  43. "tcp://localhost:4243",
  44. "",
  45. "localhost:4243",
  46. },
  47. }
  48. for c, test := range testCases {
  49. hostURL, err := ParseHostURL(test.host)
  50. assert.NilError(t, err)
  51. client := &Client{
  52. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  53. if !strings.HasPrefix(req.URL.Path, testURL) {
  54. return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
  55. }
  56. if req.Host != test.expectedHost {
  57. return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host)
  58. }
  59. if req.URL.Host != test.expectedURLHost {
  60. return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host)
  61. }
  62. return &http.Response{
  63. StatusCode: http.StatusOK,
  64. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  65. }, nil
  66. }),
  67. proto: hostURL.Scheme,
  68. addr: hostURL.Host,
  69. basePath: hostURL.Path,
  70. }
  71. _, err = client.sendRequest(context.Background(), http.MethodGet, testURL, nil, nil, nil)
  72. assert.NilError(t, err)
  73. }
  74. }
  75. // TestPlainTextError tests the server returning an error in plain text for
  76. // backwards compatibility with API versions <1.24. All other tests use
  77. // errors returned as JSON
  78. func TestPlainTextError(t *testing.T) {
  79. client := &Client{
  80. client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
  81. }
  82. _, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
  83. if !errdefs.IsSystem(err) {
  84. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  85. }
  86. }
  87. func TestInfiniteError(t *testing.T) {
  88. infinitR := rand.New(rand.NewSource(42))
  89. client := &Client{
  90. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  91. resp := &http.Response{StatusCode: http.StatusInternalServerError}
  92. resp.Header = http.Header{}
  93. resp.Body = io.NopCloser(infinitR)
  94. return resp, nil
  95. }),
  96. }
  97. _, err := client.Ping(context.Background())
  98. assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
  99. }
  100. func TestCanceledContext(t *testing.T) {
  101. testURL := "/test"
  102. client := &Client{
  103. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  104. assert.Equal(t, req.Context().Err(), context.Canceled)
  105. return &http.Response{}, context.Canceled
  106. }),
  107. }
  108. ctx, cancel := context.WithCancel(context.Background())
  109. cancel()
  110. _, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
  111. assert.Equal(t, true, errdefs.IsCancelled(err))
  112. assert.Equal(t, true, errors.Is(err, context.Canceled))
  113. }
  114. func TestDeadlineExceededContext(t *testing.T) {
  115. testURL := "/test"
  116. client := &Client{
  117. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  118. assert.Equal(t, req.Context().Err(), context.DeadlineExceeded)
  119. return &http.Response{}, context.DeadlineExceeded
  120. }),
  121. }
  122. ctx, cancel := context.WithDeadline(context.Background(), time.Now())
  123. defer cancel()
  124. <-ctx.Done()
  125. _, err := client.sendRequest(ctx, http.MethodGet, testURL, nil, nil, nil)
  126. assert.Equal(t, true, errdefs.IsDeadline(err))
  127. assert.Equal(t, true, errors.Is(err, context.DeadlineExceeded))
  128. }