Skip to content

Commit 0a813b5

Browse files
gohargasparyanboyan-soubachov
authored andcommitted
allow body for HTTPBodyContains and HTTPBodyNotContains for POST
1 parent ca8e08c commit 0a813b5

File tree

4 files changed

+60
-37
lines changed

4 files changed

+60
-37
lines changed

assert/assertion_format.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package assert
77

88
import (
9+
"io"
910
http "net/http"
1011
url "net/url"
1112
time "time"
@@ -201,11 +202,11 @@ func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, arg
201202
// assert.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
202203
//
203204
// Returns whether the assertion was successful (true) or not (false).
204-
func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
205+
func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msg string, args ...interface{}) bool {
205206
if h, ok := t.(tHelper); ok {
206207
h.Helper()
207208
}
208-
return HTTPBodyContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...)
209+
return HTTPBodyContains(t, handler, method, url, values, body, str, append([]interface{}{msg}, args...)...)
209210
}
210211

211212
// HTTPBodyNotContainsf asserts that a specified handler returns a
@@ -214,11 +215,11 @@ func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url
214215
// assert.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
215216
//
216217
// Returns whether the assertion was successful (true) or not (false).
217-
func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
218+
func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msg string, args ...interface{}) bool {
218219
if h, ok := t.(tHelper); ok {
219220
h.Helper()
220221
}
221-
return HTTPBodyNotContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...)
222+
return HTTPBodyNotContains(t, handler, method, url, values, body, str, append([]interface{}{msg}, args...)...)
222223
}
223224

224225
// HTTPErrorf asserts that a specified handler returns an error status code.

assert/assertion_forward.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package assert
77

88
import (
9+
"io"
910
http "net/http"
1011
url "net/url"
1112
time "time"
@@ -385,11 +386,11 @@ func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args .
385386
// a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
386387
//
387388
// Returns whether the assertion was successful (true) or not (false).
388-
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
389+
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msgAndArgs ...interface{}) bool {
389390
if h, ok := a.t.(tHelper); ok {
390391
h.Helper()
391392
}
392-
return HTTPBodyContains(a.t, handler, method, url, values, str, msgAndArgs...)
393+
return HTTPBodyContains(a.t, handler, method, url, values, body, str, msgAndArgs...)
393394
}
394395

395396
// HTTPBodyContainsf asserts that a specified handler returns a
@@ -398,11 +399,11 @@ func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, u
398399
// a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
399400
//
400401
// Returns whether the assertion was successful (true) or not (false).
401-
func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
402+
func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msg string, args ...interface{}) bool {
402403
if h, ok := a.t.(tHelper); ok {
403404
h.Helper()
404405
}
405-
return HTTPBodyContainsf(a.t, handler, method, url, values, str, msg, args...)
406+
return HTTPBodyContainsf(a.t, handler, method, url, values, body, str, msg, args...)
406407
}
407408

408409
// HTTPBodyNotContains asserts that a specified handler returns a
@@ -411,11 +412,11 @@ func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string,
411412
// a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
412413
//
413414
// Returns whether the assertion was successful (true) or not (false).
414-
func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
415+
func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msgAndArgs ...interface{}) bool {
415416
if h, ok := a.t.(tHelper); ok {
416417
h.Helper()
417418
}
418-
return HTTPBodyNotContains(a.t, handler, method, url, values, str, msgAndArgs...)
419+
return HTTPBodyNotContains(a.t, handler, method, url, values, body, str, msgAndArgs...)
419420
}
420421

421422
// HTTPBodyNotContainsf asserts that a specified handler returns a
@@ -424,11 +425,11 @@ func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string
424425
// a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
425426
//
426427
// Returns whether the assertion was successful (true) or not (false).
427-
func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
428+
func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msg string, args ...interface{}) bool {
428429
if h, ok := a.t.(tHelper); ok {
429430
h.Helper()
430431
}
431-
return HTTPBodyNotContainsf(a.t, handler, method, url, values, str, msg, args...)
432+
return HTTPBodyNotContainsf(a.t, handler, method, url, values, body, str, msg, args...)
432433
}
433434

434435
// HTTPError asserts that a specified handler returns an error status code.

assert/http_assertions.go

Lines changed: 13 additions & 8 deletions
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/http/httptest"
78
"net/url"
@@ -111,9 +112,13 @@ func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, va
111112

112113
// HTTPBody is a helper that returns HTTP body of the response. It returns
113114
// empty string if building a new request fails.
114-
func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
115+
func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values, body io.Reader) string {
115116
w := httptest.NewRecorder()
116-
req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
117+
118+
if values !=nil {
119+
url = url+"?"+values.Encode()
120+
}
121+
req, err := http.NewRequest(method, url, body)
117122
if err != nil {
118123
return ""
119124
}
@@ -127,13 +132,13 @@ func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) s
127132
// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
128133
//
129134
// Returns whether the assertion was successful (true) or not (false).
130-
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
135+
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, body io.Reader, str interface{}, msgAndArgs ...interface{}) bool {
131136
if h, ok := t.(tHelper); ok {
132137
h.Helper()
133138
}
134-
body := HTTPBody(handler, method, url, values)
139+
httpBody := HTTPBody(handler, method, url, values, body)
135140

136-
contains := strings.Contains(body, fmt.Sprint(str))
141+
contains := strings.Contains(httpBody, fmt.Sprint(str))
137142
if !contains {
138143
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
139144
}
@@ -147,13 +152,13 @@ func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string,
147152
// assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
148153
//
149154
// Returns whether the assertion was successful (true) or not (false).
150-
func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
155+
func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, body io.Reader, str interface{}, msgAndArgs ...interface{}) bool {
151156
if h, ok := t.(tHelper); ok {
152157
h.Helper()
153158
}
154-
body := HTTPBody(handler, method, url, values)
159+
httpBody := HTTPBody(handler, method, url, values, body)
155160

156-
contains := strings.Contains(body, fmt.Sprint(str))
161+
contains := strings.Contains(httpBody, fmt.Sprint(str))
157162
if contains {
158163
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
159164
}

assert/http_assertions_test.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package assert
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"net/http"
67
"net/url"
8+
"strings"
79
"testing"
810
)
911

@@ -120,11 +122,6 @@ func TestHTTPStatusesWrapper(t *testing.T) {
120122
assert.Equal(mockAssert.HTTPError(httpError, "GET", "/", nil), true)
121123
}
122124

123-
func httpHelloName(w http.ResponseWriter, r *http.Request) {
124-
name := r.FormValue("name")
125-
w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
126-
}
127-
128125
func TestHTTPRequestWithNoParams(t *testing.T) {
129126
var got *http.Request
130127
handler := func(w http.ResponseWriter, r *http.Request) {
@@ -158,25 +155,44 @@ func TestHttpBody(t *testing.T) {
158155
assert := New(t)
159156
mockT := new(testing.T)
160157

161-
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
162-
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
163-
assert.False(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
158+
assert.True(HTTPBodyContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "Hello, World!"))
159+
assert.True(HTTPBodyContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "World"))
160+
assert.False(HTTPBodyContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "world"))
161+
162+
assert.False(HTTPBodyNotContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "Hello, World!"))
163+
assert.False(HTTPBodyNotContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "World"))
164+
assert.True(HTTPBodyNotContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "world"))
165+
166+
assert.True(HTTPBodyContains(mockT, httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "Hello, World!"))
164167

165-
assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
166-
assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
167-
assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
168+
body := strings.NewReader("I will get this request body back as response!!")
169+
assert.True(HTTPBodyContains(mockT, httpPostHandler, "POST", "/", nil, body, "I will get this request body back as response!!"))
168170
}
169171

170172
func TestHttpBodyWrappers(t *testing.T) {
171173
assert := New(t)
172174
mockAssert := New(new(testing.T))
173175

174-
assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
175-
assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
176-
assert.False(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
176+
assert.True(mockAssert.HTTPBodyContains(httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil,"Hello, World!"))
177+
assert.True(mockAssert.HTTPBodyContains(httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil,"World"))
178+
assert.False(mockAssert.HTTPBodyContains(httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil,"world"))
177179

178-
assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
179-
assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
180-
assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
180+
assert.False(mockAssert.HTTPBodyNotContains(httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil, "Hello, World!"))
181+
assert.False(mockAssert.HTTPBodyNotContains(httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil,"World"))
182+
assert.True(mockAssert.HTTPBodyNotContains(httpGetHelloNameHandler, "GET", "/", url.Values{"name": []string{"World"}}, nil,"world"))
183+
}
181184

185+
func httpGetHelloNameHandler(w http.ResponseWriter, r *http.Request) {
186+
name := r.FormValue("name")
187+
w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
182188
}
189+
190+
func httpPostHandler(w http.ResponseWriter, r *http.Request) {
191+
body, err := ioutil.ReadAll(r.Body)
192+
if err != nil {
193+
http.Error(w, "can't read body", http.StatusBadRequest)
194+
return
195+
}
196+
197+
w.Write(body)
198+
}

0 commit comments

Comments
 (0)