diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index cbfd99eee3..cf18dcde2b 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -721,3 +721,43 @@ func TestContainerApiCreate(t *testing.T) { logDone("containers REST API - POST /containers/create") } + +func TestContainerApiVerifyHeader(t *testing.T) { + defer deleteAllContainers() + config := map[string]interface{}{ + "Image": "busybox", + } + + create := func(ct string) (int, io.ReadCloser, error) { + jsonData := bytes.NewBuffer(nil) + if err := json.NewEncoder(jsonData).Encode(config); err != nil { + t.Fatal(err) + } + return sockRequestRaw("POST", "/containers/create", jsonData, ct) + } + + // Try with no content-type + _, body, err := create("") + if err == nil { + b, _ := readBody(body) + t.Fatalf("expected error when content-type is not set: %q", string(b)) + } + body.Close() + // Try with wrong content-type + _, body, err = create("application/xml") + if err == nil { + b, _ := readBody(body) + t.Fatalf("expected error when content-type is not set: %q", string(b)) + } + body.Close() + + // now application/json + _, body, err = create("application/json") + if err != nil && !strings.Contains(err.Error(), "200 OK: 201") { + b, _ := readBody(body) + t.Fatalf("%v - %q", err, string(b)) + } + body.Close() + + logDone("containers REST API - verify create header") +} diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index ab200e1248..367e518094 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -329,10 +329,9 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io return -1, nil, fmt.Errorf("could not create new request: %v", err) } - if ct == "" { - ct = "application/json" + if ct != "" { + req.Header.Set("Content-Type", ct) } - req.Header.Set("Content-Type", ct) resp, err := client.Do(req) if err != nil { diff --git a/integration/api_test.go b/integration/api_test.go index 318978e5ad..1663f41690 100644 --- a/integration/api_test.go +++ b/integration/api_test.go @@ -22,44 +22,6 @@ import ( "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" ) -func TestPostJsonVerify(t *testing.T) { - eng := NewTestEngine(t) - defer mkDaemonFromEngine(eng, t).Nuke() - - configJSON, err := json.Marshal(&runconfig.Config{ - Image: unitTestImageID, - Cmd: runconfig.NewCommand("touch", "/test"), - }) - if err != nil { - t.Fatal(err) - } - - req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON)) - if err != nil { - t.Fatal(err) - } - - r := httptest.NewRecorder() - - server.ServeRequest(eng, api.APIVERSION, r, req) - - // Don't add Content-Type header - // req.Header.Set("Content-Type", "application/json") - - server.ServeRequest(eng, api.APIVERSION, r, req) - if r.Code != http.StatusInternalServerError || !strings.Contains(((*r.Body).String()), "application/json") { - t.Fatal("Create should have failed due to no Content-Type header - got:", r) - } - - // Now add header but with wrong type and retest - req.Header.Set("Content-Type", "application/xml") - - server.ServeRequest(eng, api.APIVERSION, r, req) - if r.Code != http.StatusInternalServerError || !strings.Contains(((*r.Body).String()), "application/json") { - t.Fatal("Create should have failed due to wrong Content-Type header - got:", r) - } -} - // Issue 7941 - test to make sure a "null" in JSON is just ignored. // W/o this fix a null in JSON would be parsed into a string var as "null" func TestPostCreateNull(t *testing.T) {