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