github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/docker_api_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "runtime" 9 "strconv" 10 "strings" 11 12 "github.com/docker/docker/api" 13 "github.com/docker/docker/integration-cli/checker" 14 "github.com/docker/docker/integration-cli/cli" 15 "github.com/docker/docker/integration-cli/request" 16 "github.com/docker/docker/pkg/testutil" 17 icmd "github.com/docker/docker/pkg/testutil/cmd" 18 "github.com/go-check/check" 19 ) 20 21 func (s *DockerSuite) TestAPIOptionsRoute(c *check.C) { 22 resp, _, err := request.Do("/", request.Method(http.MethodOptions)) 23 c.Assert(err, checker.IsNil) 24 c.Assert(resp.StatusCode, checker.Equals, http.StatusOK) 25 } 26 27 func (s *DockerSuite) TestAPIGetEnabledCORS(c *check.C) { 28 res, body, err := request.Get("/version") 29 c.Assert(err, checker.IsNil) 30 c.Assert(res.StatusCode, checker.Equals, http.StatusOK) 31 body.Close() 32 // TODO: @runcom incomplete tests, why old integration tests had this headers 33 // and here none of the headers below are in the response? 34 //c.Log(res.Header) 35 //c.Assert(res.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*") 36 //c.Assert(res.Header.Get("Access-Control-Allow-Headers"), check.Equals, "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth") 37 } 38 39 func (s *DockerSuite) TestAPIClientVersionOldNotSupported(c *check.C) { 40 if testEnv.DaemonPlatform() != runtime.GOOS { 41 c.Skip("Daemon platform doesn't match test platform") 42 } 43 if api.MinVersion == api.DefaultVersion { 44 c.Skip("API MinVersion==DefaultVersion") 45 } 46 v := strings.Split(api.MinVersion, ".") 47 vMinInt, err := strconv.Atoi(v[1]) 48 c.Assert(err, checker.IsNil) 49 vMinInt-- 50 v[1] = strconv.Itoa(vMinInt) 51 version := strings.Join(v, ".") 52 53 resp, body, err := request.Get("/v" + version + "/version") 54 c.Assert(err, checker.IsNil) 55 defer body.Close() 56 c.Assert(resp.StatusCode, checker.Equals, http.StatusBadRequest) 57 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) 58 content, err := ioutil.ReadAll(body) 59 c.Assert(err, checker.IsNil) 60 c.Assert(strings.TrimSpace(string(content)), checker.Contains, expected) 61 } 62 63 func (s *DockerSuite) TestAPIDockerAPIVersion(c *check.C) { 64 var svrVersion string 65 66 server := httptest.NewServer(http.HandlerFunc( 67 func(w http.ResponseWriter, r *http.Request) { 68 w.Header().Set("API-Version", api.DefaultVersion) 69 url := r.URL.Path 70 svrVersion = url 71 })) 72 defer server.Close() 73 74 // Test using the env var first 75 result := cli.Docker(cli.Args("-H="+server.URL[7:], "version"), cli.WithEnvironmentVariables(appendBaseEnv(false, "DOCKER_API_VERSION=xxx")...)) 76 c.Assert(result, icmd.Matches, icmd.Expected{Out: "API version: xxx", ExitCode: 1}) 77 c.Assert(svrVersion, check.Equals, "/vxxx/version", check.Commentf("%s", result.Compare(icmd.Success))) 78 } 79 80 func (s *DockerSuite) TestAPIErrorJSON(c *check.C) { 81 httpResp, body, err := request.Post("/containers/create", request.JSONBody(struct{}{})) 82 c.Assert(err, checker.IsNil) 83 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError) 84 c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json") 85 b, err := testutil.ReadBody(body) 86 c.Assert(err, checker.IsNil) 87 c.Assert(getErrorMessage(c, b), checker.Equals, "Config cannot be empty in order to create a container") 88 } 89 90 func (s *DockerSuite) TestAPIErrorPlainText(c *check.C) { 91 // Windows requires API 1.25 or later. This test is validating a behaviour which was present 92 // in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors 93 testRequires(c, DaemonIsLinux) 94 httpResp, body, err := request.Post("/v1.23/containers/create", request.JSONBody(struct{}{})) 95 c.Assert(err, checker.IsNil) 96 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError) 97 c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain") 98 b, err := testutil.ReadBody(body) 99 c.Assert(err, checker.IsNil) 100 c.Assert(strings.TrimSpace(string(b)), checker.Equals, "Config cannot be empty in order to create a container") 101 } 102 103 func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *check.C) { 104 // 404 is a different code path to normal errors, so test separately 105 httpResp, body, err := request.Get("/notfound", request.JSON) 106 c.Assert(err, checker.IsNil) 107 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound) 108 c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json") 109 b, err := testutil.ReadBody(body) 110 c.Assert(err, checker.IsNil) 111 c.Assert(getErrorMessage(c, b), checker.Equals, "page not found") 112 } 113 114 func (s *DockerSuite) TestAPIErrorNotFoundPlainText(c *check.C) { 115 httpResp, body, err := request.Get("/v1.23/notfound", request.JSON) 116 c.Assert(err, checker.IsNil) 117 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound) 118 c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain") 119 b, err := testutil.ReadBody(body) 120 c.Assert(err, checker.IsNil) 121 c.Assert(strings.TrimSpace(string(b)), checker.Equals, "page not found") 122 }