docker_api_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "github.com/docker/docker/api"
  10. "github.com/docker/docker/integration-cli/checker"
  11. "github.com/docker/docker/integration-cli/request"
  12. "github.com/docker/docker/pkg/testutil"
  13. "github.com/go-check/check"
  14. )
  15. func (s *DockerSuite) TestAPIOptionsRoute(c *check.C) {
  16. resp, _, err := request.Do("/", request.Method(http.MethodOptions))
  17. c.Assert(err, checker.IsNil)
  18. c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
  19. }
  20. func (s *DockerSuite) TestAPIGetEnabledCORS(c *check.C) {
  21. res, body, err := request.Get("/version")
  22. c.Assert(err, checker.IsNil)
  23. c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
  24. body.Close()
  25. // TODO: @runcom incomplete tests, why old integration tests had this headers
  26. // and here none of the headers below are in the response?
  27. //c.Log(res.Header)
  28. //c.Assert(res.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
  29. //c.Assert(res.Header.Get("Access-Control-Allow-Headers"), check.Equals, "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
  30. }
  31. func (s *DockerSuite) TestAPIClientVersionOldNotSupported(c *check.C) {
  32. if testEnv.DaemonPlatform() != runtime.GOOS {
  33. c.Skip("Daemon platform doesn't match test platform")
  34. }
  35. if api.MinVersion == api.DefaultVersion {
  36. c.Skip("API MinVersion==DefaultVersion")
  37. }
  38. v := strings.Split(api.MinVersion, ".")
  39. vMinInt, err := strconv.Atoi(v[1])
  40. c.Assert(err, checker.IsNil)
  41. vMinInt--
  42. v[1] = strconv.Itoa(vMinInt)
  43. version := strings.Join(v, ".")
  44. resp, body, err := request.Get("/v" + version + "/version")
  45. c.Assert(err, checker.IsNil)
  46. defer body.Close()
  47. c.Assert(resp.StatusCode, checker.Equals, http.StatusBadRequest)
  48. expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, api.MinVersion)
  49. content, err := ioutil.ReadAll(body)
  50. c.Assert(err, checker.IsNil)
  51. c.Assert(strings.TrimSpace(string(content)), checker.Contains, expected)
  52. }
  53. func (s *DockerSuite) TestAPIErrorJSON(c *check.C) {
  54. httpResp, body, err := request.Post("/containers/create", request.JSONBody(struct{}{}))
  55. c.Assert(err, checker.IsNil)
  56. c.Assert(httpResp.StatusCode, checker.Equals, http.StatusBadRequest)
  57. c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json")
  58. b, err := testutil.ReadBody(body)
  59. c.Assert(err, checker.IsNil)
  60. c.Assert(getErrorMessage(c, b), checker.Equals, "Config cannot be empty in order to create a container")
  61. }
  62. func (s *DockerSuite) TestAPIErrorPlainText(c *check.C) {
  63. // Windows requires API 1.25 or later. This test is validating a behaviour which was present
  64. // in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors
  65. testRequires(c, DaemonIsLinux)
  66. httpResp, body, err := request.Post("/v1.23/containers/create", request.JSONBody(struct{}{}))
  67. c.Assert(err, checker.IsNil)
  68. c.Assert(httpResp.StatusCode, checker.Equals, http.StatusBadRequest)
  69. c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain")
  70. b, err := testutil.ReadBody(body)
  71. c.Assert(err, checker.IsNil)
  72. c.Assert(strings.TrimSpace(string(b)), checker.Equals, "Config cannot be empty in order to create a container")
  73. }
  74. func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *check.C) {
  75. // 404 is a different code path to normal errors, so test separately
  76. httpResp, body, err := request.Get("/notfound", request.JSON)
  77. c.Assert(err, checker.IsNil)
  78. c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound)
  79. c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json")
  80. b, err := testutil.ReadBody(body)
  81. c.Assert(err, checker.IsNil)
  82. c.Assert(getErrorMessage(c, b), checker.Equals, "page not found")
  83. }
  84. func (s *DockerSuite) TestAPIErrorNotFoundPlainText(c *check.C) {
  85. httpResp, body, err := request.Get("/v1.23/notfound", request.JSON)
  86. c.Assert(err, checker.IsNil)
  87. c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound)
  88. c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain")
  89. b, err := testutil.ReadBody(body)
  90. c.Assert(err, checker.IsNil)
  91. c.Assert(strings.TrimSpace(string(b)), checker.Equals, "page not found")
  92. }