blob: 4ed341dd28934c102aa7a40c74ee24b6555c1db1 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001package assert
2
3import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "strings"
9)
10
11// httpCode is a helper that returns HTTP code of the response. It returns -1 and
12// an error if building a new request fails.
13func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) {
14 w := httptest.NewRecorder()
15 req, err := http.NewRequest(method, url, nil)
16 if err != nil {
17 return -1, err
18 }
19 req.URL.RawQuery = values.Encode()
20 handler(w, req)
21 return w.Code, nil
22}
23
24// HTTPSuccess asserts that a specified handler returns a success status code.
25//
26// assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
27//
28// Returns whether the assertion was successful (true) or not (false).
29func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool {
30 if h, ok := t.(tHelper); ok {
31 h.Helper()
32 }
33 code, err := httpCode(handler, method, url, values)
34 if err != nil {
35 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
Scott Baker2c1c4822019-10-16 11:02:41 -070036 }
37
38 isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent
39 if !isSuccessCode {
40 Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code))
41 }
42
43 return isSuccessCode
44}
45
46// HTTPRedirect asserts that a specified handler returns a redirect status code.
47//
48// assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
49//
50// Returns whether the assertion was successful (true) or not (false).
51func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool {
52 if h, ok := t.(tHelper); ok {
53 h.Helper()
54 }
55 code, err := httpCode(handler, method, url, values)
56 if err != nil {
57 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
Scott Baker2c1c4822019-10-16 11:02:41 -070058 }
59
60 isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
61 if !isRedirectCode {
62 Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code))
63 }
64
65 return isRedirectCode
66}
67
68// HTTPError asserts that a specified handler returns an error status code.
69//
70// assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
71//
72// Returns whether the assertion was successful (true) or not (false).
73func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool {
74 if h, ok := t.(tHelper); ok {
75 h.Helper()
76 }
77 code, err := httpCode(handler, method, url, values)
78 if err != nil {
79 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
Scott Baker2c1c4822019-10-16 11:02:41 -070080 }
81
82 isErrorCode := code >= http.StatusBadRequest
83 if !isErrorCode {
84 Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code))
85 }
86
87 return isErrorCode
88}
89
khenaidoo26721882021-08-11 17:42:52 -040090// HTTPStatusCode asserts that a specified handler returns a specified status code.
91//
92// assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)
93//
94// Returns whether the assertion was successful (true) or not (false).
95func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) bool {
96 if h, ok := t.(tHelper); ok {
97 h.Helper()
98 }
99 code, err := httpCode(handler, method, url, values)
100 if err != nil {
101 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
102 }
103
104 successful := code == statuscode
105 if !successful {
106 Fail(t, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", statuscode, url+"?"+values.Encode(), code))
107 }
108
109 return successful
110}
111
Scott Baker2c1c4822019-10-16 11:02:41 -0700112// HTTPBody is a helper that returns HTTP body of the response. It returns
113// empty string if building a new request fails.
114func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
115 w := httptest.NewRecorder()
116 req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
117 if err != nil {
118 return ""
119 }
120 handler(w, req)
121 return w.Body.String()
122}
123
124// HTTPBodyContains asserts that a specified handler returns a
125// body that contains a string.
126//
127// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
128//
129// Returns whether the assertion was successful (true) or not (false).
130func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
131 if h, ok := t.(tHelper); ok {
132 h.Helper()
133 }
134 body := HTTPBody(handler, method, url, values)
135
136 contains := strings.Contains(body, fmt.Sprint(str))
137 if !contains {
138 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
139 }
140
141 return contains
142}
143
144// HTTPBodyNotContains asserts that a specified handler returns a
145// body that does not contain a string.
146//
147// assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
148//
149// Returns whether the assertion was successful (true) or not (false).
150func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
151 if h, ok := t.(tHelper); ok {
152 h.Helper()
153 }
154 body := HTTPBody(handler, method, url, values)
155
156 contains := strings.Contains(body, fmt.Sprint(str))
157 if contains {
158 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
159 }
160
161 return !contains
162}