diff --git a/assert/http_assertions.go b/assert/http_assertions.go index 6cfb6a838..8b82ca8d2 100644 --- a/assert/http_assertions.go +++ b/assert/http_assertions.go @@ -12,7 +12,7 @@ import ( // an error if building a new request fails. func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) { w := httptest.NewRecorder() - req, err := http.NewRequest(method, url, nil) + req, err := http.NewRequest(method, url, http.NoBody) if err != nil { return -1, err } @@ -116,7 +116,7 @@ func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) s if len(values) > 0 { url += "?" + values.Encode() } - req, err := http.NewRequest(method, url, nil) + req, err := http.NewRequest(method, url, http.NoBody) if err != nil { return "" } diff --git a/assert/http_assertions_test.go b/assert/http_assertions_test.go index 413c9714f..8da117e28 100644 --- a/assert/http_assertions_test.go +++ b/assert/http_assertions_test.go @@ -2,6 +2,7 @@ package assert import ( "fmt" + "io" "net/http" "net/url" "testing" @@ -11,6 +12,12 @@ func httpOK(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } +func httpReadBody(w http.ResponseWriter, r *http.Request) { + _, _ = io.Copy(io.Discard, r.Body) + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("hello")) +} + func httpRedirect(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTemporaryRedirect) } @@ -41,6 +48,10 @@ func TestHTTPSuccess(t *testing.T) { mockT4 := new(testing.T) assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false) assert.True(mockT4.Failed()) + + mockT5 := new(testing.T) + assert.Equal(HTTPSuccess(mockT5, httpReadBody, "POST", "/", nil), true) + assert.False(mockT5.Failed()) } func TestHTTPRedirect(t *testing.T) { @@ -122,7 +133,7 @@ func TestHTTPStatusesWrapper(t *testing.T) { func httpHelloName(w http.ResponseWriter, r *http.Request) { name := r.FormValue("name") - _, _ = w.Write([]byte(fmt.Sprintf("Hello, %s!", name))) + _, _ = fmt.Fprintf(w, "Hello, %s!", name) } func TestHTTPRequestWithNoParams(t *testing.T) { @@ -165,6 +176,8 @@ func TestHttpBody(t *testing.T) { assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")) assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World")) assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world")) + + assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello")) } func TestHttpBodyWrappers(t *testing.T) { @@ -178,5 +191,4 @@ func TestHttpBodyWrappers(t *testing.T) { assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!")) assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World")) assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world")) - }