Skip to content

Commit 3184a9e

Browse files
This reverts commit 0a813b5.
1 parent e2b269e commit 3184a9e

File tree

4 files changed

+37
-60
lines changed

4 files changed

+37
-60
lines changed

assert/assertion_format.go

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

88
import (
9-
"io"
109
http "net/http"
1110
url "net/url"
1211
time "time"
@@ -202,11 +201,11 @@ func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, arg
202201
// assert.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
203202
//
204203
// Returns whether the assertion was successful (true) or not (false).
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 {
204+
func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
206205
if h, ok := t.(tHelper); ok {
207206
h.Helper()
208207
}
209-
return HTTPBodyContains(t, handler, method, url, values, body, str, append([]interface{}{msg}, args...)...)
208+
return HTTPBodyContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...)
210209
}
211210

212211
// HTTPBodyNotContainsf asserts that a specified handler returns a
@@ -215,11 +214,11 @@ func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url
215214
// assert.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
216215
//
217216
// Returns whether the assertion was successful (true) or not (false).
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 {
217+
func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
219218
if h, ok := t.(tHelper); ok {
220219
h.Helper()
221220
}
222-
return HTTPBodyNotContains(t, handler, method, url, values, body, str, append([]interface{}{msg}, args...)...)
221+
return HTTPBodyNotContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...)
223222
}
224223

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

assert/assertion_forward.go

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

88
import (
9-
"io"
109
http "net/http"
1110
url "net/url"
1211
time "time"
@@ -386,11 +385,11 @@ func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args .
386385
// a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
387386
//
388387
// Returns whether the assertion was successful (true) or not (false).
389-
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, body io.Reader, str interface{}, msgAndArgs ...interface{}) bool {
388+
func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
390389
if h, ok := a.t.(tHelper); ok {
391390
h.Helper()
392391
}
393-
return HTTPBodyContains(a.t, handler, method, url, values, body, str, msgAndArgs...)
392+
return HTTPBodyContains(a.t, handler, method, url, values, str, msgAndArgs...)
394393
}
395394

396395
// HTTPBodyContainsf asserts that a specified handler returns a
@@ -399,11 +398,11 @@ func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, u
399398
// a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
400399
//
401400
// Returns whether the assertion was successful (true) or not (false).
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 {
401+
func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
403402
if h, ok := a.t.(tHelper); ok {
404403
h.Helper()
405404
}
406-
return HTTPBodyContainsf(a.t, handler, method, url, values, body, str, msg, args...)
405+
return HTTPBodyContainsf(a.t, handler, method, url, values, str, msg, args...)
407406
}
408407

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

422421
// HTTPBodyNotContainsf asserts that a specified handler returns a
@@ -425,11 +424,11 @@ func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string
425424
// a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted")
426425
//
427426
// Returns whether the assertion was successful (true) or not (false).
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 {
427+
func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool {
429428
if h, ok := a.t.(tHelper); ok {
430429
h.Helper()
431430
}
432-
return HTTPBodyNotContainsf(a.t, handler, method, url, values, body, str, msg, args...)
431+
return HTTPBodyNotContainsf(a.t, handler, method, url, values, str, msg, args...)
433432
}
434433

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

assert/http_assertions.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package assert
22

33
import (
44
"fmt"
5-
"io"
65
"net/http"
76
"net/http/httptest"
87
"net/url"
@@ -112,13 +111,9 @@ func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, va
112111

113112
// HTTPBody is a helper that returns HTTP body of the response. It returns
114113
// empty string if building a new request fails.
115-
func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values, body io.Reader) string {
114+
func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
116115
w := httptest.NewRecorder()
117-
118-
if values !=nil {
119-
url = url+"?"+values.Encode()
120-
}
121-
req, err := http.NewRequest(method, url, body)
116+
req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
122117
if err != nil {
123118
return ""
124119
}
@@ -132,13 +127,13 @@ func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values, b
132127
// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
133128
//
134129
// Returns whether the assertion was successful (true) or not (false).
135-
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, body io.Reader, str interface{}, msgAndArgs ...interface{}) bool {
130+
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
136131
if h, ok := t.(tHelper); ok {
137132
h.Helper()
138133
}
139-
httpBody := HTTPBody(handler, method, url, values, body)
134+
body := HTTPBody(handler, method, url, values)
140135

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

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

assert/http_assertions_test.go

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

33
import (
44
"fmt"
5-
"io/ioutil"
65
"net/http"
76
"net/url"
8-
"strings"
97
"testing"
108
)
119

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

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+
125128
func TestHTTPRequestWithNoParams(t *testing.T) {
126129
var got *http.Request
127130
handler := func(w http.ResponseWriter, r *http.Request) {
@@ -155,44 +158,25 @@ func TestHttpBody(t *testing.T) {
155158
assert := New(t)
156159
mockT := new(testing.T)
157160

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!"))
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"))
167164

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!!"))
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"))
170168
}
171169

172170
func TestHttpBodyWrappers(t *testing.T) {
173171
assert := New(t)
174172
mockAssert := New(new(testing.T))
175173

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"))
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"))
179177

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-
}
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"))
184181

185-
func httpGetHelloNameHandler(w http.ResponseWriter, r *http.Request) {
186-
name := r.FormValue("name")
187-
w.Write([]byte(fmt.Sprintf("Hello, %s!", name)))
188182
}
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)