api: remove plain-text error-responses (api < v1.24)

Commit 322e2a7d05 changed the format of errors
returned by the API to be in JSON format for API v1.24. Older versions of
the API returned errors in plain-text format.

API v1.23 and older are deprecated, so we can remove support for plain-text
error responses.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-01-21 16:08:40 +01:00
parent b3a0ff9944
commit ffd877f948
3 changed files with 12 additions and 70 deletions

View File

@@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"io"
"net/http"
"runtime"
"strconv"
@@ -62,9 +61,9 @@ func (s *DockerAPISuite) TestAPIClientVersionOldNotSupported(c *testing.T) {
defer body.Close()
assert.Equal(c, resp.StatusCode, http.StatusBadRequest)
expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, testEnv.DaemonVersion.MinAPIVersion)
content, err := io.ReadAll(body)
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(string(content)), expected)
assert.Equal(c, getErrorMessage(c, b), expected)
}
func (s *DockerAPISuite) TestAPIErrorJSON(c *testing.T) {
@@ -77,19 +76,6 @@ func (s *DockerAPISuite) TestAPIErrorJSON(c *testing.T) {
assert.Equal(c, getErrorMessage(c, b), runconfig.ErrEmptyConfig.Error())
}
func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
// 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)
httpResp, body, err := request.Post(testutil.GetContext(c), "/v1.23/containers/create", request.JSONBody(struct{}{}))
assert.NilError(c, err)
assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(string(b)), runconfig.ErrEmptyConfig.Error())
}
func (s *DockerAPISuite) TestAPIErrorNotFoundJSON(c *testing.T) {
// 404 is a different code path to normal errors, so test separately
httpResp, body, err := request.Get(testutil.GetContext(c), "/notfound", request.JSON)
@@ -100,13 +86,3 @@ func (s *DockerAPISuite) TestAPIErrorNotFoundJSON(c *testing.T) {
assert.NilError(c, err)
assert.Equal(c, getErrorMessage(c, b), "page not found")
}
func (s *DockerAPISuite) TestAPIErrorNotFoundPlainText(c *testing.T) {
httpResp, body, err := request.Get(testutil.GetContext(c), "/v1.23/notfound", request.JSON)
assert.NilError(c, err)
assert.Equal(c, httpResp.StatusCode, http.StatusNotFound)
assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(string(b)), "page not found")
}