2018-11-05 13:50:33 +00:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-04-05 09:43:06 +00:00
|
|
|
"runtime"
|
2018-11-05 13:50:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-08-29 20:52:40 +00:00
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-11-05 13:50:33 +00:00
|
|
|
)
|
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
// TestContainerInvalidJSON tests that POST endpoints that expect a body return
|
|
|
|
// the correct error when sending invalid JSON requests.
|
2018-11-05 13:50:33 +00:00
|
|
|
func TestContainerInvalidJSON(t *testing.T) {
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
// POST endpoints that accept / expect a JSON body;
|
2018-11-05 13:50:33 +00:00
|
|
|
endpoints := []string{
|
2022-04-08 21:27:50 +00:00
|
|
|
"/commit",
|
|
|
|
"/containers/create",
|
2018-11-05 13:50:33 +00:00
|
|
|
"/containers/foobar/exec",
|
2022-04-05 09:43:06 +00:00
|
|
|
"/containers/foobar/update",
|
2018-11-05 13:50:33 +00:00
|
|
|
"/exec/foobar/start",
|
|
|
|
}
|
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
// windows doesnt support API < v1.24
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
endpoints = append(
|
|
|
|
endpoints,
|
2022-04-08 21:27:50 +00:00
|
|
|
"/v1.23/containers/foobar/copy", // deprecated since 1.8 (API v1.20), errors out since 1.12 (API v1.24)
|
|
|
|
"/v1.23/containers/foobar/start", // accepts a body on API < v1.24
|
2022-04-05 09:43:06 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:50:33 +00:00
|
|
|
for _, ep := range endpoints {
|
2021-07-22 20:41:01 +00:00
|
|
|
ep := ep
|
2022-04-05 09:43:06 +00:00
|
|
|
t.Run(ep[1:], func(t *testing.T) {
|
2018-11-05 13:50:33 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
t.Run("invalid content type", func(t *testing.T) {
|
|
|
|
res, body, err := request.Post(ep, request.RawString("{}"), request.ContentType("text/plain"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
|
|
|
|
|
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "unsupported Content-Type header (text/plain): must be 'application/json'"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid JSON", func(t *testing.T) {
|
|
|
|
res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
|
|
|
|
|
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "invalid JSON: invalid character 'i' looking for beginning of object key string"))
|
|
|
|
})
|
2018-11-05 13:50:33 +00:00
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
t.Run("extra content after JSON", func(t *testing.T) {
|
|
|
|
res, body, err := request.Post(ep, request.RawString(`{} trailing content`), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
|
2018-11-05 13:50:33 +00:00
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
buf, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(buf), "unexpected content after JSON"))
|
|
|
|
})
|
2018-11-05 13:50:33 +00:00
|
|
|
|
2022-04-05 09:43:06 +00:00
|
|
|
t.Run("empty body", func(t *testing.T) {
|
|
|
|
// empty body should not produce an 500 internal server error, or
|
|
|
|
// any 5XX error (this is assuming the request does not produce
|
|
|
|
// an internal server error for another reason, but it shouldn't)
|
|
|
|
res, _, err := request.Post(ep, request.RawString(``), request.JSON)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, res.StatusCode < http.StatusInternalServerError)
|
|
|
|
})
|
2018-11-05 13:50:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|