@@ -2,8 +2,10 @@ package assert
2
2
3
3
import (
4
4
"fmt"
5
+ "io/ioutil"
5
6
"net/http"
6
7
"net/url"
8
+ "strings"
7
9
"testing"
8
10
)
9
11
@@ -120,11 +122,6 @@ func TestHTTPStatusesWrapper(t *testing.T) {
120
122
assert .Equal (mockAssert .HTTPError (httpError , "GET" , "/" , nil ), true )
121
123
}
122
124
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
-
128
125
func TestHTTPRequestWithNoParams (t * testing.T ) {
129
126
var got * http.Request
130
127
handler := func (w http.ResponseWriter , r * http.Request ) {
@@ -158,25 +155,44 @@ func TestHttpBody(t *testing.T) {
158
155
assert := New (t )
159
156
mockT := new (testing.T )
160
157
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!" ))
164
167
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!!" ))
168
170
}
169
171
170
172
func TestHttpBodyWrappers (t * testing.T ) {
171
173
assert := New (t )
172
174
mockAssert := New (new (testing.T ))
173
175
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" ))
177
179
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
+ }
181
184
185
+ func httpGetHelloNameHandler (w http.ResponseWriter , r * http.Request ) {
186
+ name := r .FormValue ("name" )
187
+ w .Write ([]byte (fmt .Sprintf ("Hello, %s!" , name )))
182
188
}
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