11 // httpCode is a helper that returns HTTP code of the response. It returns -1
12 // if building a new request fails.
13 func httpCode(handler http.HandlerFunc, method, url string, values url.Values) int {
14 w := httptest.NewRecorder()
15 req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
23 // HTTPSuccess asserts that a specified handler returns a success status code.
25 // assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
27 // Returns whether the assertion was successful (true) or not (false).
28 func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
29 code := httpCode(handler, method, url, values)
33 return code >= http.StatusOK && code <= http.StatusPartialContent
36 // HTTPRedirect asserts that a specified handler returns a redirect status code.
38 // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
40 // Returns whether the assertion was successful (true) or not (false).
41 func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
42 code := httpCode(handler, method, url, values)
46 return code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
49 // HTTPError asserts that a specified handler returns an error status code.
51 // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
53 // Returns whether the assertion was successful (true) or not (false).
54 func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
55 code := httpCode(handler, method, url, values)
59 return code >= http.StatusBadRequest
62 // HTTPBody is a helper that returns HTTP body of the response. It returns
63 // empty string if building a new request fails.
64 func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
65 w := httptest.NewRecorder()
66 req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
71 return w.Body.String()
74 // HTTPBodyContains asserts that a specified handler returns a
75 // body that contains a string.
77 // assert.HTTPBodyContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
79 // Returns whether the assertion was successful (true) or not (false).
80 func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
81 body := HTTPBody(handler, method, url, values)
83 contains := strings.Contains(body, fmt.Sprint(str))
85 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
91 // HTTPBodyNotContains asserts that a specified handler returns a
92 // body that does not contain a string.
94 // assert.HTTPBodyNotContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
96 // Returns whether the assertion was successful (true) or not (false).
97 func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
98 body := HTTPBody(handler, method, url, values)
100 contains := strings.Contains(body, fmt.Sprint(str))
102 Fail(t, "Expected response body for %s to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)