container_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/docker/docker/testutil"
  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. ctx := 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. for _, ep := range endpoints {
  23. ep := ep
  24. t.Run(ep[1:], func(t *testing.T) {
  25. t.Parallel()
  26. t.Run("invalid content type", func(t *testing.T) {
  27. ctx := testutil.StartSpan(ctx, t)
  28. res, body, err := request.Post(ctx, ep, request.RawString("{}"), request.ContentType("text/plain"))
  29. assert.NilError(t, err)
  30. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  31. buf, err := request.ReadBody(body)
  32. assert.NilError(t, err)
  33. assert.Check(t, is.Contains(string(buf), "unsupported Content-Type header (text/plain): must be 'application/json'"))
  34. })
  35. t.Run("invalid JSON", func(t *testing.T) {
  36. ctx := testutil.StartSpan(ctx, t)
  37. res, body, err := request.Post(ctx, ep, request.RawString("{invalid json"), request.JSON)
  38. assert.NilError(t, err)
  39. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  40. buf, err := request.ReadBody(body)
  41. assert.NilError(t, err)
  42. assert.Check(t, is.Contains(string(buf), "invalid JSON: invalid character 'i' looking for beginning of object key string"))
  43. })
  44. t.Run("extra content after JSON", func(t *testing.T) {
  45. ctx := testutil.StartSpan(ctx, t)
  46. res, body, err := request.Post(ctx, ep, request.RawString(`{} trailing content`), request.JSON)
  47. assert.NilError(t, err)
  48. assert.Check(t, is.Equal(res.StatusCode, http.StatusBadRequest))
  49. buf, err := request.ReadBody(body)
  50. assert.NilError(t, err)
  51. assert.Check(t, is.Contains(string(buf), "unexpected content after JSON"))
  52. })
  53. t.Run("empty body", func(t *testing.T) {
  54. ctx := testutil.StartSpan(ctx, t)
  55. // empty body should not produce an 500 internal server error, or
  56. // any 5XX error (this is assuming the request does not produce
  57. // an internal server error for another reason, but it shouldn't)
  58. res, _, err := request.Post(ctx, ep, request.RawString(``), request.JSON)
  59. assert.NilError(t, err)
  60. assert.Check(t, res.StatusCode < http.StatusInternalServerError)
  61. })
  62. })
  63. }
  64. }