github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_api_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "net/http/httputil" 8 "os/exec" 9 "strconv" 10 "strings" 11 "time" 12 13 "github.com/docker/docker/api" 14 "github.com/docker/docker/pkg/integration/checker" 15 "github.com/go-check/check" 16 ) 17 18 func (s *DockerSuite) TestApiOptionsRoute(c *check.C) { 19 status, _, err := sockRequest("OPTIONS", "/", nil) 20 c.Assert(err, checker.IsNil) 21 c.Assert(status, checker.Equals, http.StatusOK) 22 } 23 24 func (s *DockerSuite) TestApiGetEnabledCors(c *check.C) { 25 res, body, err := sockRequestRaw("GET", "/version", nil, "") 26 c.Assert(err, checker.IsNil) 27 c.Assert(res.StatusCode, checker.Equals, http.StatusOK) 28 body.Close() 29 // TODO: @runcom incomplete tests, why old integration tests had this headers 30 // and here none of the headers below are in the response? 31 //c.Log(res.Header) 32 //c.Assert(res.Header.Get("Access-Control-Allow-Origin"), check.Equals, "*") 33 //c.Assert(res.Header.Get("Access-Control-Allow-Headers"), check.Equals, "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth") 34 } 35 36 func (s *DockerSuite) TestApiVersionStatusCode(c *check.C) { 37 conn, err := sockConn(time.Duration(10*time.Second), "") 38 c.Assert(err, checker.IsNil) 39 40 client := httputil.NewClientConn(conn, nil) 41 defer client.Close() 42 43 req, err := http.NewRequest("GET", "/v999.0/version", nil) 44 c.Assert(err, checker.IsNil) 45 req.Header.Set("User-Agent", "Docker-Client/999.0 (os)") 46 47 res, err := client.Do(req) 48 c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest) 49 } 50 51 func (s *DockerSuite) TestApiClientVersionNewerThanServer(c *check.C) { 52 v := strings.Split(api.DefaultVersion, ".") 53 vMinInt, err := strconv.Atoi(v[1]) 54 c.Assert(err, checker.IsNil) 55 vMinInt++ 56 v[1] = strconv.Itoa(vMinInt) 57 version := strings.Join(v, ".") 58 59 status, body, err := sockRequest("GET", "/v"+version+"/version", nil) 60 c.Assert(err, checker.IsNil) 61 c.Assert(status, checker.Equals, http.StatusBadRequest) 62 expected := fmt.Sprintf("client is newer than server (client API version: %s, server API version: %s)", version, api.DefaultVersion) 63 c.Assert(getErrorMessage(c, body), checker.Equals, expected) 64 } 65 66 func (s *DockerSuite) TestApiClientVersionOldNotSupported(c *check.C) { 67 v := strings.Split(api.MinVersion, ".") 68 vMinInt, err := strconv.Atoi(v[1]) 69 c.Assert(err, checker.IsNil) 70 vMinInt-- 71 v[1] = strconv.Itoa(vMinInt) 72 version := strings.Join(v, ".") 73 74 status, body, err := sockRequest("GET", "/v"+version+"/version", nil) 75 c.Assert(err, checker.IsNil) 76 c.Assert(status, checker.Equals, http.StatusBadRequest) 77 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) 78 c.Assert(strings.TrimSpace(string(body)), checker.Equals, expected) 79 } 80 81 func (s *DockerSuite) TestApiDockerApiVersion(c *check.C) { 82 var svrVersion string 83 84 server := httptest.NewServer(http.HandlerFunc( 85 func(w http.ResponseWriter, r *http.Request) { 86 url := r.URL.Path 87 svrVersion = url 88 })) 89 defer server.Close() 90 91 // Test using the env var first 92 cmd := exec.Command(dockerBinary, "-H="+server.URL[7:], "version") 93 cmd.Env = appendBaseEnv(false, "DOCKER_API_VERSION=xxx") 94 out, _, _ := runCommandWithOutput(cmd) 95 96 c.Assert(svrVersion, check.Equals, "/vxxx/version") 97 98 if !strings.Contains(out, "API version: xxx") { 99 c.Fatalf("Out didn't have 'xxx' for the API version, had:\n%s", out) 100 } 101 } 102 103 func (s *DockerSuite) TestApiErrorJSON(c *check.C) { 104 httpResp, body, err := sockRequestRaw("POST", "/containers/create", strings.NewReader(`{}`), "application/json") 105 c.Assert(err, checker.IsNil) 106 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError) 107 c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json") 108 b, err := readBody(body) 109 c.Assert(err, checker.IsNil) 110 c.Assert(getErrorMessage(c, b), checker.Equals, "Config cannot be empty in order to create a container") 111 } 112 113 func (s *DockerSuite) TestApiErrorPlainText(c *check.C) { 114 httpResp, body, err := sockRequestRaw("POST", "/v1.23/containers/create", strings.NewReader(`{}`), "application/json") 115 c.Assert(err, checker.IsNil) 116 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusInternalServerError) 117 c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain") 118 b, err := readBody(body) 119 c.Assert(err, checker.IsNil) 120 c.Assert(strings.TrimSpace(string(b)), checker.Equals, "Config cannot be empty in order to create a container") 121 } 122 123 func (s *DockerSuite) TestApiErrorNotFoundJSON(c *check.C) { 124 // 404 is a different code path to normal errors, so test separately 125 httpResp, body, err := sockRequestRaw("GET", "/notfound", nil, "application/json") 126 c.Assert(err, checker.IsNil) 127 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound) 128 c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json") 129 b, err := readBody(body) 130 c.Assert(err, checker.IsNil) 131 c.Assert(getErrorMessage(c, b), checker.Equals, "page not found") 132 } 133 134 func (s *DockerSuite) TestApiErrorNotFoundPlainText(c *check.C) { 135 httpResp, body, err := sockRequestRaw("GET", "/v1.23/notfound", nil, "application/json") 136 c.Assert(err, checker.IsNil) 137 c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound) 138 c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain") 139 b, err := readBody(body) 140 c.Assert(err, checker.IsNil) 141 c.Assert(strings.TrimSpace(string(b)), checker.Equals, "page not found") 142 }