Skip to content

Commit

Permalink
assert: fix httpCode and HTTPBody occur panic when http.Handler read …
Browse files Browse the repository at this point in the history
…body
  • Loading branch information
hidu authored and dolmen committed Oct 15, 2023
1 parent 8992013 commit ce5c2b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions assert/http_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 ""
}
Expand Down
16 changes: 14 additions & 2 deletions assert/http_assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assert

import (
"fmt"
"io"
"net/http"
"net/url"
"testing"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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"))

}

0 comments on commit ce5c2b6

Please sign in to comment.