container_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "net/http"
  4. "runtime"
  5. "testing"
  6. "github.com/docker/docker/testutil/request"
  7. "gotest.tools/v3/assert"
  8. is "gotest.tools/v3/assert/cmp"
  9. )
  10. // TestContainerInvalidJSON tests that POST endpoints that expect a body return
  11. // the correct error when sending invalid JSON requests.
  12. func TestContainerInvalidJSON(t *testing.T) {
  13. t.Cleanup(setupTest(t))
  14. // POST endpoints that accept / expect a JSON body;
  15. endpoints := []string{
  16. "/commit",
  17. "/containers/create",
  18. "/containers/foobar/exec",
  19. "/containers/foobar/update",
  20. "/exec/foobar/start",
  21. }
  22. // windows doesnt support API < v1.24
  23. if runtime.GOOS != "windows" {
  24. endpoints = append(
  25. endpoints,
  26. "/v1.23/containers/foobar/copy", // deprecated since 1.8 (API v1.20), errors out since 1.12 (API v1.24)
  27. "/v1.23/containers/foobar/start", // accepts a body on API < v1.24
  28. )
  29. }
  30. for _, ep := range endpoints {
  31. ep := ep
  32. t.Run(ep[1:], func(t *testing.T) {
  33. t.Parallel()
  34. t.Run("invalid content type", func(t *testing.T) {
  35. res, body, err := request.Post(ep, request.RawString("{}"), request.ContentType("text/plain"))
  36. assert.NilError(t, err)
  37. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  38. buf, err := request.ReadBody(body)
  39. assert.NilError(t, err)
  40. assert.Check(t, is.Contains(string(buf), "unsupported Content-Type header (text/plain): must be 'application/json'"))
  41. })
  42. t.Run("invalid JSON", func(t *testing.T) {
  43. res, body, err := request.Post(ep, request.RawString("{invalid json"), request.JSON)
  44. assert.NilError(t, err)
  45. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  46. buf, err := request.ReadBody(body)
  47. assert.NilError(t, err)
  48. assert.Check(t, is.Contains(string(buf), "invalid JSON: invalid character 'i' looking for beginning of object key string"))
  49. })
  50. t.Run("extra content after JSON", func(t *testing.T) {
  51. res, body, err := request.Post(ep, request.RawString(`{} trailing content`), request.JSON)
  52. assert.NilError(t, err)
  53. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  54. buf, err := request.ReadBody(body)
  55. assert.NilError(t, err)
  56. assert.Check(t, is.Contains(string(buf), "unexpected content after JSON"))
  57. })
  58. t.Run("empty body", func(t *testing.T) {
  59. // empty body should not produce an 500 internal server error, or
  60. // any 5XX error (this is assuming the request does not produce
  61. // an internal server error for another reason, but it shouldn't)
  62. res, _, err := request.Post(ep, request.RawString(``), request.JSON)
  63. assert.NilError(t, err)
  64. assert.Check(t, res.StatusCode < http.StatusInternalServerError)
  65. })
  66. })
  67. }
  68. }