github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/container/container_test.go (about)

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