github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/container/container_test.go (about)

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