github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration-cli/docker_api_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"runtime"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/docker/docker/api"
    12  	"github.com/docker/docker/integration-cli/checker"
    13  	"github.com/docker/docker/integration-cli/request"
    14  	"github.com/go-check/check"
    15  )
    16  
    17  func (s *DockerSuite) TestAPIOptionsRoute(c *check.C) {
    18  	resp, _, err := request.Do("/", request.Method(http.MethodOptions))
    19  	c.Assert(err, checker.IsNil)
    20  	c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
    21  }
    22  
    23  func (s *DockerSuite) TestAPIGetEnabledCORS(c *check.C) {
    24  	res, body, err := request.Get("/version")
    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 testEnv.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  	resp, body, err := request.Get("/v" + version + "/version")
    50  	c.Assert(err, checker.IsNil)
    51  	defer body.Close()
    52  	c.Assert(resp.StatusCode, checker.Equals, http.StatusBadRequest)
    53  	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)
    54  	content, err := ioutil.ReadAll(body)
    55  	c.Assert(err, checker.IsNil)
    56  	c.Assert(strings.TrimSpace(string(content)), checker.Contains, expected)
    57  }
    58  
    59  func (s *DockerSuite) TestAPIErrorJSON(c *check.C) {
    60  	httpResp, body, err := request.Post("/containers/create", request.JSONBody(struct{}{}))
    61  	c.Assert(err, checker.IsNil)
    62  	c.Assert(httpResp.StatusCode, checker.Equals, http.StatusBadRequest)
    63  	c.Assert(httpResp.Header.Get("Content-Type"), checker.Equals, "application/json")
    64  	b, err := request.ReadBody(body)
    65  	c.Assert(err, checker.IsNil)
    66  	c.Assert(getErrorMessage(c, b), checker.Equals, "Config cannot be empty in order to create a container")
    67  }
    68  
    69  func (s *DockerSuite) TestAPIErrorPlainText(c *check.C) {
    70  	// Windows requires API 1.25 or later. This test is validating a behaviour which was present
    71  	// in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors
    72  	testRequires(c, DaemonIsLinux)
    73  	httpResp, body, err := request.Post("/v1.23/containers/create", request.JSONBody(struct{}{}))
    74  	c.Assert(err, checker.IsNil)
    75  	c.Assert(httpResp.StatusCode, checker.Equals, http.StatusBadRequest)
    76  	c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain")
    77  	b, err := request.ReadBody(body)
    78  	c.Assert(err, checker.IsNil)
    79  	c.Assert(strings.TrimSpace(string(b)), checker.Equals, "Config cannot be empty in order to create a container")
    80  }
    81  
    82  func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *check.C) {
    83  	// 404 is a different code path to normal errors, so test separately
    84  	httpResp, body, err := request.Get("/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.Equals, "application/json")
    88  	b, err := request.ReadBody(body)
    89  	c.Assert(err, checker.IsNil)
    90  	c.Assert(getErrorMessage(c, b), checker.Equals, "page not found")
    91  }
    92  
    93  func (s *DockerSuite) TestAPIErrorNotFoundPlainText(c *check.C) {
    94  	httpResp, body, err := request.Get("/v1.23/notfound", request.JSON)
    95  	c.Assert(err, checker.IsNil)
    96  	c.Assert(httpResp.StatusCode, checker.Equals, http.StatusNotFound)
    97  	c.Assert(httpResp.Header.Get("Content-Type"), checker.Contains, "text/plain")
    98  	b, err := request.ReadBody(body)
    99  	c.Assert(err, checker.IsNil)
   100  	c.Assert(strings.TrimSpace(string(b)), checker.Equals, "page not found")
   101  }