docker_api_test.go 4.1 KB

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