request_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "math/rand"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. // TestSetHostHeader should set fake host for local communications, set real host
  17. // for normal communications.
  18. func TestSetHostHeader(t *testing.T) {
  19. testURL := "/test"
  20. testCases := []struct {
  21. host string
  22. expectedHost string
  23. expectedURLHost string
  24. }{
  25. {
  26. "unix:///var/run/docker.sock",
  27. "docker",
  28. "/var/run/docker.sock",
  29. },
  30. {
  31. "npipe:////./pipe/docker_engine",
  32. "docker",
  33. "//./pipe/docker_engine",
  34. },
  35. {
  36. "tcp://0.0.0.0:4243",
  37. "",
  38. "0.0.0.0:4243",
  39. },
  40. {
  41. "tcp://localhost:4243",
  42. "",
  43. "localhost:4243",
  44. },
  45. }
  46. for c, test := range testCases {
  47. hostURL, err := ParseHostURL(test.host)
  48. assert.NilError(t, err)
  49. client := &Client{
  50. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  51. if !strings.HasPrefix(req.URL.Path, testURL) {
  52. return nil, fmt.Errorf("Test Case #%d: Expected URL %q, got %q", c, testURL, req.URL)
  53. }
  54. if req.Host != test.expectedHost {
  55. return nil, fmt.Errorf("Test Case #%d: Expected host %q, got %q", c, test.expectedHost, req.Host)
  56. }
  57. if req.URL.Host != test.expectedURLHost {
  58. return nil, fmt.Errorf("Test Case #%d: Expected URL host %q, got %q", c, test.expectedURLHost, req.URL.Host)
  59. }
  60. return &http.Response{
  61. StatusCode: http.StatusOK,
  62. Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  63. }, nil
  64. }),
  65. proto: hostURL.Scheme,
  66. addr: hostURL.Host,
  67. basePath: hostURL.Path,
  68. }
  69. _, err = client.sendRequest(context.Background(), http.MethodGet, testURL, nil, nil, nil)
  70. assert.NilError(t, err)
  71. }
  72. }
  73. // TestPlainTextError tests the server returning an error in plain text for
  74. // backwards compatibility with API versions <1.24. All other tests use
  75. // errors returned as JSON
  76. func TestPlainTextError(t *testing.T) {
  77. client := &Client{
  78. client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
  79. }
  80. _, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
  81. if !errdefs.IsSystem(err) {
  82. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  83. }
  84. }
  85. func TestInfiniteError(t *testing.T) {
  86. infinitR := rand.New(rand.NewSource(42))
  87. client := &Client{
  88. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  89. resp := &http.Response{StatusCode: http.StatusInternalServerError}
  90. resp.Header = http.Header{}
  91. resp.Body = ioutil.NopCloser(infinitR)
  92. return resp, nil
  93. }),
  94. }
  95. _, err := client.Ping(context.Background())
  96. assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
  97. }