container_test.go 2.8 KB

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