Skip to content

Commit ce5c2b6

Browse files
hidudolmen
authored andcommitted
assert: fix httpCode and HTTPBody occur panic when http.Handler read body
1 parent 8992013 commit ce5c2b6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

assert/http_assertions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// an error if building a new request fails.
1313
func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) {
1414
w := httptest.NewRecorder()
15-
req, err := http.NewRequest(method, url, nil)
15+
req, err := http.NewRequest(method, url, http.NoBody)
1616
if err != nil {
1717
return -1, err
1818
}
@@ -116,7 +116,7 @@ func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) s
116116
if len(values) > 0 {
117117
url += "?" + values.Encode()
118118
}
119-
req, err := http.NewRequest(method, url, nil)
119+
req, err := http.NewRequest(method, url, http.NoBody)
120120
if err != nil {
121121
return ""
122122
}

assert/http_assertions_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package assert
22

33
import (
44
"fmt"
5+
"io"
56
"net/http"
67
"net/url"
78
"testing"
@@ -11,6 +12,12 @@ func httpOK(w http.ResponseWriter, r *http.Request) {
1112
w.WriteHeader(http.StatusOK)
1213
}
1314

15+
func httpReadBody(w http.ResponseWriter, r *http.Request) {
16+
_, _ = io.Copy(io.Discard, r.Body)
17+
w.WriteHeader(http.StatusOK)
18+
_, _ = w.Write([]byte("hello"))
19+
}
20+
1421
func httpRedirect(w http.ResponseWriter, r *http.Request) {
1522
w.WriteHeader(http.StatusTemporaryRedirect)
1623
}
@@ -41,6 +48,10 @@ func TestHTTPSuccess(t *testing.T) {
4148
mockT4 := new(testing.T)
4249
assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false)
4350
assert.True(mockT4.Failed())
51+
52+
mockT5 := new(testing.T)
53+
assert.Equal(HTTPSuccess(mockT5, httpReadBody, "POST", "/", nil), true)
54+
assert.False(mockT5.Failed())
4455
}
4556

4657
func TestHTTPRedirect(t *testing.T) {
@@ -122,7 +133,7 @@ func TestHTTPStatusesWrapper(t *testing.T) {
122133

123134
func httpHelloName(w http.ResponseWriter, r *http.Request) {
124135
name := r.FormValue("name")
125-
_, _ = w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
136+
_, _ = fmt.Fprintf(w, "Hello, %s!", name)
126137
}
127138

128139
func TestHTTPRequestWithNoParams(t *testing.T) {
@@ -165,6 +176,8 @@ func TestHttpBody(t *testing.T) {
165176
assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
166177
assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
167178
assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
179+
180+
assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello"))
168181
}
169182

170183
func TestHttpBodyWrappers(t *testing.T) {
@@ -178,5 +191,4 @@ func TestHttpBodyWrappers(t *testing.T) {
178191
assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
179192
assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
180193
assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
181-
182194
}

0 commit comments

Comments
 (0)