github.com/jingleWang/moby@v1.13.1/integration-cli/docker_api_test.go (about)

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