diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index 2b2decfa67c2cf7da1e3d6d7f8dae8ca0754dd3a..c3413432fcc0537ff6779d622fe833044e0f95cd 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -323,24 +323,44 @@ func TestHeaders(t *testing.T) { } func TestPost__EmptyBody(t *testing.T) { - r, _ := http.NewRequest("POST", "/post", nil) - w := httptest.NewRecorder() - handler.ServeHTTP(w, r) + var tests = []struct { + contentType string + }{ + {""}, + {"application/json; charset=utf-8"}, + {"application/x-www-form-urlencoded"}, + {"multipart/form-data; foo"}, + } + for _, test := range tests { + t.Run("content type/"+test.contentType, func(t *testing.T) { + r, _ := http.NewRequest("POST", "/post", nil) + r.Header.Set("Content-Type", test.contentType) + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) - assertStatusCode(t, w, http.StatusOK) - assertContentType(t, w, jsonContentType) + assertStatusCode(t, w, http.StatusOK) + assertContentType(t, w, jsonContentType) - var resp *bodyResponse - err := json.Unmarshal(w.Body.Bytes(), &resp) - if err != nil { - t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err) - } + var resp *bodyResponse + err := json.Unmarshal(w.Body.Bytes(), &resp) + if err != nil { + t.Fatalf("failed to unmarshal body %s from JSON: %s", w.Body, err) + } - if len(resp.Args) > 0 { - t.Fatalf("expected no query params, got %#v", resp.Args) - } - if len(resp.Form) > 0 { - t.Fatalf("expected no form data, got %#v", resp.Form) + if resp.Data != "" { + t.Fatalf("expected empty response data, got %#v", resp.Data) + } + if resp.JSON != nil { + t.Fatalf("expected nil response json, got %#v", resp.JSON) + } + + if len(resp.Args) > 0 { + t.Fatalf("expected no query params, got %#v", resp.Args) + } + if len(resp.Form) > 0 { + t.Fatalf("expected no form data, got %#v", resp.Form) + } + }) } } diff --git a/httpbin/helpers.go b/httpbin/helpers.go index ba65116184d17b64a84eb794090555014efdb375..889f40b52c6f0526cd42ad15cf71e2806bd385ef 100644 --- a/httpbin/helpers.go +++ b/httpbin/helpers.go @@ -121,7 +121,8 @@ func parseBody(w http.ResponseWriter, r *http.Request, resp *bodyResponse) error } resp.Form = r.PostForm case strings.HasPrefix(ct, "application/json"): - if err := json.NewDecoder(r.Body).Decode(&resp.JSON); err != nil { + err := json.NewDecoder(r.Body).Decode(&resp.JSON) + if err != nil && err != io.EOF { return err } }