Skip to content

Commit e2741fa

Browse files
Merge pull request #1548 from arjunmahishi/msgAndArgs
http_assertions: honour the msgAndArgs provided with each assertion
2 parents e65c014 + 6e59f20 commit e2741fa

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

assert/assertions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAnd
14631463
h.Helper()
14641464
}
14651465
if math.IsNaN(epsilon) {
1466-
return Fail(t, "epsilon must not be NaN")
1466+
return Fail(t, "epsilon must not be NaN", msgAndArgs...)
14671467
}
14681468
actualEpsilon, err := calcRelativeError(expected, actual)
14691469
if err != nil {

assert/assertions_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,10 @@ func (m *mockTestingT) Errorf(format string, args ...interface{}) {
25142514
m.args = args
25152515
}
25162516

2517+
func (m *mockTestingT) Failed() bool {
2518+
return m.errorFmt != ""
2519+
}
2520+
25172521
func TestFailNowWithPlainTestingT(t *testing.T) {
25182522
mockT := &mockTestingT{}
25192523

assert/http_assertions.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, value
3232
}
3333
code, err := httpCode(handler, method, url, values)
3434
if err != nil {
35-
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
35+
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
3636
}
3737

3838
isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent
3939
if !isSuccessCode {
40-
Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code))
40+
Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...)
4141
}
4242

4343
return isSuccessCode
@@ -54,12 +54,12 @@ func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, valu
5454
}
5555
code, err := httpCode(handler, method, url, values)
5656
if err != nil {
57-
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
57+
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
5858
}
5959

6060
isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
6161
if !isRedirectCode {
62-
Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code))
62+
Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...)
6363
}
6464

6565
return isRedirectCode
@@ -76,12 +76,12 @@ func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values
7676
}
7777
code, err := httpCode(handler, method, url, values)
7878
if err != nil {
79-
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
79+
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
8080
}
8181

8282
isErrorCode := code >= http.StatusBadRequest
8383
if !isErrorCode {
84-
Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code))
84+
Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...)
8585
}
8686

8787
return isErrorCode
@@ -98,12 +98,12 @@ func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, va
9898
}
9999
code, err := httpCode(handler, method, url, values)
100100
if err != nil {
101-
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
101+
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
102102
}
103103

104104
successful := code == statuscode
105105
if !successful {
106-
Fail(t, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", statuscode, url+"?"+values.Encode(), code))
106+
Fail(t, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", statuscode, url+"?"+values.Encode(), code), msgAndArgs...)
107107
}
108108

109109
return successful
@@ -138,7 +138,7 @@ func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string,
138138

139139
contains := strings.Contains(body, fmt.Sprint(str))
140140
if !contains {
141-
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
141+
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body), msgAndArgs...)
142142
}
143143

144144
return contains
@@ -158,7 +158,7 @@ func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url strin
158158

159159
contains := strings.Contains(body, fmt.Sprint(str))
160160
if contains {
161-
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
161+
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body), msgAndArgs...)
162162
}
163163

164164
return !contains

assert/http_assertions_test.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ func TestHTTPSuccess(t *testing.T) {
4141
assert.Equal(HTTPSuccess(mockT2, httpRedirect, "GET", "/", nil), false)
4242
assert.True(mockT2.Failed())
4343

44-
mockT3 := new(testing.T)
45-
assert.Equal(HTTPSuccess(mockT3, httpError, "GET", "/", nil), false)
44+
mockT3 := new(mockTestingT)
45+
assert.Equal(HTTPSuccess(
46+
mockT3, httpError, "GET", "/", nil,
47+
"was not expecting a failure here",
48+
), false)
4649
assert.True(mockT3.Failed())
50+
assert.Contains(mockT3.errorString(), "was not expecting a failure here")
4751

4852
mockT4 := new(testing.T)
4953
assert.Equal(HTTPSuccess(mockT4, httpStatusCode, "GET", "/", nil), false)
@@ -57,9 +61,13 @@ func TestHTTPSuccess(t *testing.T) {
5761
func TestHTTPRedirect(t *testing.T) {
5862
assert := New(t)
5963

60-
mockT1 := new(testing.T)
61-
assert.Equal(HTTPRedirect(mockT1, httpOK, "GET", "/", nil), false)
64+
mockT1 := new(mockTestingT)
65+
assert.Equal(HTTPRedirect(
66+
mockT1, httpOK, "GET", "/", nil,
67+
"was expecting a 3xx status code. Got 200.",
68+
), false)
6269
assert.True(mockT1.Failed())
70+
assert.Contains(mockT1.errorString(), "was expecting a 3xx status code. Got 200.")
6371

6472
mockT2 := new(testing.T)
6573
assert.Equal(HTTPRedirect(mockT2, httpRedirect, "GET", "/", nil), true)
@@ -81,9 +89,13 @@ func TestHTTPError(t *testing.T) {
8189
assert.Equal(HTTPError(mockT1, httpOK, "GET", "/", nil), false)
8290
assert.True(mockT1.Failed())
8391

84-
mockT2 := new(testing.T)
85-
assert.Equal(HTTPError(mockT2, httpRedirect, "GET", "/", nil), false)
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)
8697
assert.True(mockT2.Failed())
98+
assert.Contains(mockT2.errorString(), "Expected this request to error out. But it didn't")
8799

88100
mockT3 := new(testing.T)
89101
assert.Equal(HTTPError(mockT3, httpError, "GET", "/", nil), true)
@@ -105,9 +117,13 @@ func TestHTTPStatusCode(t *testing.T) {
105117
assert.Equal(HTTPStatusCode(mockT2, httpRedirect, "GET", "/", nil, http.StatusSwitchingProtocols), false)
106118
assert.True(mockT2.Failed())
107119

108-
mockT3 := new(testing.T)
109-
assert.Equal(HTTPStatusCode(mockT3, httpError, "GET", "/", nil, http.StatusSwitchingProtocols), false)
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)
110125
assert.True(mockT3.Failed())
126+
assert.Contains(mockT3.errorString(), "Expected the status code to be 101")
111127

112128
mockT4 := new(testing.T)
113129
assert.Equal(HTTPStatusCode(mockT4, httpStatusCode, "GET", "/", nil, http.StatusSwitchingProtocols), true)
@@ -167,15 +183,19 @@ func TestHTTPRequestWithParams(t *testing.T) {
167183

168184
func TestHttpBody(t *testing.T) {
169185
assert := New(t)
170-
mockT := new(testing.T)
186+
mockT := new(mockTestingT)
171187

172188
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
173189
assert.True(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "World"))
174190
assert.False(HTTPBodyContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "world"))
175191

176192
assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "Hello, World!"))
177-
assert.False(HTTPBodyNotContains(mockT, httpHelloName, "GET", "/", url.Values{"name": []string{"World"}}, "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+
))
178197
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.")
179199

180200
assert.True(HTTPBodyContains(mockT, httpReadBody, "GET", "/", nil, "hello"))
181201
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy