integration-cli/docker_api_test.go
f7e417ea
 package main
 
 import (
fe6b88e3
 	"fmt"
d69d4799
 	"io/ioutil"
f7e417ea
 	"net/http"
29d21388
 	"runtime"
910322a8
 	"strconv"
 	"strings"
e25352a4
 	"testing"
f7e417ea
 
910322a8
 	"github.com/docker/docker/api"
e4408318
 	"github.com/docker/docker/api/types/versions"
42f6fdf0
 	"github.com/docker/docker/internal/test/request"
6345208b
 	"gotest.tools/assert"
f7e417ea
 )
 
64a928a3
 func (s *DockerSuite) TestAPIOptionsRoute(c *testing.T) {
b11ba123
 	resp, _, err := request.Do("/", request.Method(http.MethodOptions))
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, resp.StatusCode, http.StatusOK)
f7e417ea
 }
 
64a928a3
 func (s *DockerSuite) TestAPIGetEnabledCORS(c *testing.T) {
b11ba123
 	res, body, err := request.Get("/version")
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, res.StatusCode, http.StatusOK)
18faf6f9
 	body.Close()
f7e417ea
 	// TODO: @runcom incomplete tests, why old integration tests had this headers
 	// and here none of the headers below are in the response?
 	//c.Log(res.Header)
6345208b
 	//assert.Equal(c, res.Header.Get("Access-Control-Allow-Origin"), "*")
 	//assert.Equal(c, res.Header.Get("Access-Control-Allow-Headers"), "Origin, X-Requested-With, Content-Type, Accept, X-Registry-Auth")
f7e417ea
 }
7fcf8497
 
64a928a3
 func (s *DockerSuite) TestAPIClientVersionOldNotSupported(c *testing.T) {
18a771a7
 	if testEnv.OSType != runtime.GOOS {
29d21388
 		c.Skip("Daemon platform doesn't match test platform")
 	}
 	if api.MinVersion == api.DefaultVersion {
 		c.Skip("API MinVersion==DefaultVersion")
 	}
7534f172
 	v := strings.Split(api.MinVersion, ".")
910322a8
 	vMinInt, err := strconv.Atoi(v[1])
6345208b
 	assert.NilError(c, err)
910322a8
 	vMinInt--
 	v[1] = strconv.Itoa(vMinInt)
 	version := strings.Join(v, ".")
 
b11ba123
 	resp, body, err := request.Get("/v" + version + "/version")
6345208b
 	assert.NilError(c, err)
d69d4799
 	defer body.Close()
6345208b
 	assert.Equal(c, resp.StatusCode, http.StatusBadRequest)
fe6b88e3
 	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)
d69d4799
 	content, err := ioutil.ReadAll(body)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, strings.TrimSpace(string(content)), expected)
910322a8
 }
6287ec90
 
64a928a3
 func (s *DockerSuite) TestAPIErrorJSON(c *testing.T) {
b11ba123
 	httpResp, body, err := request.Post("/containers/create", request.JSONBody(struct{}{}))
6345208b
 	assert.NilError(c, err)
e4408318
 	if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
6345208b
 		assert.Equal(c, httpResp.StatusCode, http.StatusInternalServerError)
e4408318
 	} else {
6345208b
 		assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
e4408318
 	}
6345208b
 	assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
4f304e72
 	b, err := request.ReadBody(body)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, getErrorMessage(c, b), "Config cannot be empty in order to create a container")
322e2a7d
 }
 
64a928a3
 func (s *DockerSuite) TestAPIErrorPlainText(c *testing.T) {
f811d5b1
 	// Windows requires API 1.25 or later. This test is validating a behaviour which was present
 	// in v1.23, but changed in 1.24, hence not applicable on Windows. See apiVersionSupportsJSONErrors
 	testRequires(c, DaemonIsLinux)
b11ba123
 	httpResp, body, err := request.Post("/v1.23/containers/create", request.JSONBody(struct{}{}))
6345208b
 	assert.NilError(c, err)
e4408318
 	if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
6345208b
 		assert.Equal(c, httpResp.StatusCode, http.StatusInternalServerError)
e4408318
 	} else {
6345208b
 		assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
e4408318
 	}
6345208b
 	assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
4f304e72
 	b, err := request.ReadBody(body)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, strings.TrimSpace(string(b)), "Config cannot be empty in order to create a container")
322e2a7d
 }
 
64a928a3
 func (s *DockerSuite) TestAPIErrorNotFoundJSON(c *testing.T) {
322e2a7d
 	// 404 is a different code path to normal errors, so test separately
b11ba123
 	httpResp, body, err := request.Get("/notfound", request.JSON)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, httpResp.StatusCode, http.StatusNotFound)
 	assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
4f304e72
 	b, err := request.ReadBody(body)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, getErrorMessage(c, b), "page not found")
322e2a7d
 }
 
64a928a3
 func (s *DockerSuite) TestAPIErrorNotFoundPlainText(c *testing.T) {
b11ba123
 	httpResp, body, err := request.Get("/v1.23/notfound", request.JSON)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, httpResp.StatusCode, http.StatusNotFound)
 	assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
4f304e72
 	b, err := request.ReadBody(body)
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, strings.TrimSpace(string(b)), "page not found")
322e2a7d
 }