Signed-off-by: Brian Goff <cpuguy83@gmail.com>
| ... | ... |
@@ -721,3 +721,43 @@ func TestContainerApiCreate(t *testing.T) {
|
| 721 | 721 |
|
| 722 | 722 |
logDone("containers REST API - POST /containers/create")
|
| 723 | 723 |
} |
| 724 |
+ |
|
| 725 |
+func TestContainerApiVerifyHeader(t *testing.T) {
|
|
| 726 |
+ defer deleteAllContainers() |
|
| 727 |
+ config := map[string]interface{}{
|
|
| 728 |
+ "Image": "busybox", |
|
| 729 |
+ } |
|
| 730 |
+ |
|
| 731 |
+ create := func(ct string) (int, io.ReadCloser, error) {
|
|
| 732 |
+ jsonData := bytes.NewBuffer(nil) |
|
| 733 |
+ if err := json.NewEncoder(jsonData).Encode(config); err != nil {
|
|
| 734 |
+ t.Fatal(err) |
|
| 735 |
+ } |
|
| 736 |
+ return sockRequestRaw("POST", "/containers/create", jsonData, ct)
|
|
| 737 |
+ } |
|
| 738 |
+ |
|
| 739 |
+ // Try with no content-type |
|
| 740 |
+ _, body, err := create("")
|
|
| 741 |
+ if err == nil {
|
|
| 742 |
+ b, _ := readBody(body) |
|
| 743 |
+ t.Fatalf("expected error when content-type is not set: %q", string(b))
|
|
| 744 |
+ } |
|
| 745 |
+ body.Close() |
|
| 746 |
+ // Try with wrong content-type |
|
| 747 |
+ _, body, err = create("application/xml")
|
|
| 748 |
+ if err == nil {
|
|
| 749 |
+ b, _ := readBody(body) |
|
| 750 |
+ t.Fatalf("expected error when content-type is not set: %q", string(b))
|
|
| 751 |
+ } |
|
| 752 |
+ body.Close() |
|
| 753 |
+ |
|
| 754 |
+ // now application/json |
|
| 755 |
+ _, body, err = create("application/json")
|
|
| 756 |
+ if err != nil && !strings.Contains(err.Error(), "200 OK: 201") {
|
|
| 757 |
+ b, _ := readBody(body) |
|
| 758 |
+ t.Fatalf("%v - %q", err, string(b))
|
|
| 759 |
+ } |
|
| 760 |
+ body.Close() |
|
| 761 |
+ |
|
| 762 |
+ logDone("containers REST API - verify create header")
|
|
| 763 |
+} |
| ... | ... |
@@ -329,10 +329,9 @@ func sockRequestRaw(method, endpoint string, data io.Reader, ct string) (int, io |
| 329 | 329 |
return -1, nil, fmt.Errorf("could not create new request: %v", err)
|
| 330 | 330 |
} |
| 331 | 331 |
|
| 332 |
- if ct == "" {
|
|
| 333 |
- ct = "application/json" |
|
| 332 |
+ if ct != "" {
|
|
| 333 |
+ req.Header.Set("Content-Type", ct)
|
|
| 334 | 334 |
} |
| 335 |
- req.Header.Set("Content-Type", ct)
|
|
| 336 | 335 |
|
| 337 | 336 |
resp, err := client.Do(req) |
| 338 | 337 |
if err != nil {
|
| ... | ... |
@@ -22,44 +22,6 @@ import ( |
| 22 | 22 |
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" |
| 23 | 23 |
) |
| 24 | 24 |
|
| 25 |
-func TestPostJsonVerify(t *testing.T) {
|
|
| 26 |
- eng := NewTestEngine(t) |
|
| 27 |
- defer mkDaemonFromEngine(eng, t).Nuke() |
|
| 28 |
- |
|
| 29 |
- configJSON, err := json.Marshal(&runconfig.Config{
|
|
| 30 |
- Image: unitTestImageID, |
|
| 31 |
- Cmd: runconfig.NewCommand("touch", "/test"),
|
|
| 32 |
- }) |
|
| 33 |
- if err != nil {
|
|
| 34 |
- t.Fatal(err) |
|
| 35 |
- } |
|
| 36 |
- |
|
| 37 |
- req, err := http.NewRequest("POST", "/containers/create", bytes.NewReader(configJSON))
|
|
| 38 |
- if err != nil {
|
|
| 39 |
- t.Fatal(err) |
|
| 40 |
- } |
|
| 41 |
- |
|
| 42 |
- r := httptest.NewRecorder() |
|
| 43 |
- |
|
| 44 |
- server.ServeRequest(eng, api.APIVERSION, r, req) |
|
| 45 |
- |
|
| 46 |
- // Don't add Content-Type header |
|
| 47 |
- // req.Header.Set("Content-Type", "application/json")
|
|
| 48 |
- |
|
| 49 |
- server.ServeRequest(eng, api.APIVERSION, r, req) |
|
| 50 |
- if r.Code != http.StatusInternalServerError || !strings.Contains(((*r.Body).String()), "application/json") {
|
|
| 51 |
- t.Fatal("Create should have failed due to no Content-Type header - got:", r)
|
|
| 52 |
- } |
|
| 53 |
- |
|
| 54 |
- // Now add header but with wrong type and retest |
|
| 55 |
- req.Header.Set("Content-Type", "application/xml")
|
|
| 56 |
- |
|
| 57 |
- server.ServeRequest(eng, api.APIVERSION, r, req) |
|
| 58 |
- if r.Code != http.StatusInternalServerError || !strings.Contains(((*r.Body).String()), "application/json") {
|
|
| 59 |
- t.Fatal("Create should have failed due to wrong Content-Type header - got:", r)
|
|
| 60 |
- } |
|
| 61 |
-} |
|
| 62 |
- |
|
| 63 | 25 |
// Issue 7941 - test to make sure a "null" in JSON is just ignored. |
| 64 | 26 |
// W/o this fix a null in JSON would be parsed into a string var as "null" |
| 65 | 27 |
func TestPostCreateNull(t *testing.T) {
|