1 package assert
2
3 import (
4 "fmt"
5 "io"
6 "net/http"
7 "net/url"
8 "testing"
9 )
10
11 func httpOK(w http.ResponseWriter, r *http.Request) {
12 w.WriteHeader(http.StatusOK)
13 }
14
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
21 func httpRedirect(w http.ResponseWriter, r *http.Request) {
22 w.WriteHeader(http.StatusTemporaryRedirect)
23 }
24
25 func httpError(w http.ResponseWriter, r *http.Request) {
26 w.WriteHeader(http.StatusInternalServerError)
27 }
28
29 func httpStatusCode(w http.ResponseWriter, r *http.Request) {
30 w.WriteHeader(http.StatusSwitchingProtocols)
31 }
32
33 func TestHTTPSuccess(t *testing.T) {
34 assert := New(t)
35
36 mockT1 := new(testing.T)
37 assert.Equal(HTTPSuccess(mockT1, httpOK, "GET", "/", nil), true)
38 assert.False(mockT1.Failed())
39
40 mockT2 := new(testing.T)
41 assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false)
42 assert.True(mockT2.Failed())
43
44 mockT3 := new(mockTestingT)
45 assert.Equal(HTTPSuccess(
46 mockT3, httpError, "GET", "/", nil,
47 "was not expecting a failure here",
48 ), false)
49 assert.True(mockT3.Failed())
50 assert.Contains(mockT3.errorString(), "was not expecting a failure here")
51
52 mockT4 := new(testing.T)
53 assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false)
54 assert.True(mockT4.Failed())
55
56 mockT5 := new(testing.T)
57 assert.Equal(HTTPSuccess(mockT5, httpReadBody, "POST", "/", nil), true)
58 assert.False(mockT5.Failed())
59 }
60
61 func TestHTTPRedirect(t *testing.T) {
62 assert := New(t)
63
64 mockT1 := new(mockTestingT)
65 assert.Equal(HTTPRedirect(
66 mockT1, httpOK, "GET", "/", nil,
67 "was expecting a 3xx status code. Got 200.",
68 ), false)
69 assert.True(mockT1.Failed())
70 assert.Contains(mockT1.errorString(), "was expecting a 3xx status code. Got 200.")
71
72 mockT2 := new(testing.T)
73 assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true)
74 assert.False(mockT2.Failed())
75
76 mockT3 := new(testing.T)
77 assert.Equal(HTTPRedirect(mockT3, httpError, "GET", "/", nil), false)
78 assert.True(mockT3.Failed())
79
80 mockT4 := new(testing.T)
81 assert.Equal(HTTPRedirect(mockT4, httpStatusCode, "GET", "/", nil), false)
82 assert.True(mockT4.Failed())
83 }
84
85 func TestHTTPError(t *testing.T) {
86 assert := New(t)
87
88 mockT1 := new(testing.T)
89 assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false)
90 assert.True(mockT1.Failed())
91
92 mockT2 := new(mockTestingT)
93 assert.Equal(HTTPError(
94 mockT2, httpRedirect, "GET", "/", nil,
95 "Expected this request to error out. But it didn't",
96 ), false)
97 assert.True(mockT2.Failed())
98 assert.Contains(mockT2.errorString(), "Expected this request to error out. But it didn't")
99
100 mockT3 := new(testing.T)
101 assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true)
102 assert.False(mockT3.Failed())
103
104 mockT4 := new(testing.T)
105 assert.Equal(HTTPError(mockT4, httpStatusCode, "GET", "/", nil), false)
106 assert.True(mockT4.Failed())
107 }
108
109 func TestHTTPStatusCode(t *testing.T) {
110 assert := New(t)
111
112 mockT1 := new(testing.T)
113 assert.Equal(HTTPStatusCode(mockT1, httpOK, "GET", "/", nil, http.StatusSwitchingProtocols), false)
114 assert.True(mockT1.Failed())
115
116 mockT2 := new(testing.T)
117 assert.Equal(HTTPStatusCode(mockT2, httpRedirect, "GET", "/", nil, http.StatusSwitchingProtocols), false)
118 assert.True(mockT2.Failed())
119
120 mockT3 := new(mockTestingT)
121 assert.Equal(HTTPStatusCode(
122 mockT3, httpError, "GET", "/", nil, http.StatusSwitchingProtocols,
123 "Expected the status code to be %d", http.StatusSwitchingProtocols,
124 ), false)
125 assert.True(mockT3.Failed())
126 assert.Contains(mockT3.errorString(), "Expected the status code to be 101")
127
128 mockT4 := new(testing.T)
129 assert.Equal(HTTPStatusCode(mockT4, httpStatusCode, "GET", "/", nil, http.StatusSwitchingProtocols), true)
130 assert.False(mockT4.Failed())
131 }
132
133 func TestHTTPStatusesWrapper(t *testing.T) {
134 assert := New(t)
135 mockAssert := New(new(testing.T))
136
137 assert.Equal(mockAssert.HTTPSuccess(httpOK, "GET", "/", nil), true)
138 assert.Equal(mockAssert.HTTPSuccess(httpRedirect, "GET", "/", nil), false)
139 assert.Equal(mockAssert.HTTPSuccess(httpError, "GET", "/", nil), false)
140
141 assert.Equal(mockAssert.HTTPRedirect(httpOK, "GET", "/", nil), false)
142 assert.Equal(mockAssert.HTTPRedirect(httpRedirect, "GET", "/", nil), true)
143 assert.Equal(mockAssert.HTTPRedirect(httpError, "GET", "/", nil), false)
144
145 assert.Equal(mockAssert.HTTPError(httpOK, "GET", "/", nil), false)
146 assert.Equal(mockAssert.HTTPError(httpRedirect, "GET", "/", nil), false)
147 assert.Equal(mockAssert.HTTPError(httpError, "GET", "/", nil), true)
148 }
149
150 func httpHelloName(w http.ResponseWriter, r *http.Request) {
151 name := r.FormValue("name")
152 _, _ = fmt.Fprintf(w, "Hello, %s!", name)
153 }
154
155 func TestHTTPRequestWithNoParams(t *testing.T) {
156 var got *http.Request
157 handler := func(w http.ResponseWriter, r *http.Request) {
158 got = r
159 w.WriteHeader(http.StatusOK)
160 }
161
162 True(t, HTTPSuccess(t, handler, "GET", "/url", nil))
163
164 Empty(t, got.URL.Query())
165 Equal(t, "/url", got.URL.RequestURI())
166 }
167
168 func TestHTTPRequestWithParams(t *testing.T) {
169 var got *http.Request
170 handler := func(w http.ResponseWriter, r *http.Request) {
171 got = r
172 w.WriteHeader(http.StatusOK)
173 }
174 params := url.Values{}
175 params.Add("id", "12345")
176
177 True(t, HTTPSuccess(t, handler, "GET", "/url", params))
178
179 Equal(t, url.Values{"id": []string{"12345"}}, got.URL.Query())
180 Equal(t, "/url?id=12345", got.URL.String())
181 Equal(t, "/url?id=12345", got.URL.RequestURI())
182 }
183
184 func TestHttpBody(t *testing.T) {
185 assert := New(t)
186 mockT := new(mockTestingT)
187
188 assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
189 assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
190 assert.False(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
191
192 assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
193 assert.False(HTTPBodyNotContains(
194 mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World",
195 "Expected the request body to not contain 'World'. But it did.",
196 ))
197 assert.True(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
198 assert.Contains(mockT.errorString(), "Expected the request body to not contain 'World'. But it did.")
199
200 assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello"))
201 }
202
203 func TestHttpBodyWrappers(t *testing.T) {
204 assert := New(t)
205 mockAssert := New(new(testing.T))
206
207 assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
208 assert.True(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
209 assert.False(mockAssert.HTTPBodyContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
210
211 assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
212 assert.False(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
213 assert.True(mockAssert.HTTPBodyNotContains(httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
214 }
215
View as plain text