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.
13 func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) {
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, err := httpCode(handler, method, url, values)
31 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
35 isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent
37 Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code))
43 // HTTPRedirect asserts that a specified handler returns a redirect status code.
45 // assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
47 // Returns whether the assertion was successful (true) or not (false).
48 func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
49 code, err := httpCode(handler, method, url, values)
51 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
55 isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
57 Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code))
63 // HTTPError asserts that a specified handler returns an error status code.
65 // assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
67 // Returns whether the assertion was successful (true) or not (false).
68 func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
69 code, err := httpCode(handler, method, url, values)
71 Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err))
75 isErrorCode := code >= http.StatusBadRequest
77 Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code))
83 // HTTPBody is a helper that returns HTTP body of the response. It returns
84 // empty string if building a new request fails.
85 func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
86 w := httptest.NewRecorder()
87 req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
92 return w.Body.String()
95 // HTTPBodyContains asserts that a specified handler returns a
96 // body that contains a string.
98 // assert.HTTPBodyContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
100 // Returns whether the assertion was successful (true) or not (false).
101 func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
102 body := HTTPBody(handler, method, url, values)
104 contains := strings.Contains(body, fmt.Sprint(str))
106 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
112 // HTTPBodyNotContains asserts that a specified handler returns a
113 // body that does not contain a string.
115 // assert.HTTPBodyNotContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
117 // Returns whether the assertion was successful (true) or not (false).
118 func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
119 body := HTTPBody(handler, method, url, values)
121 contains := strings.Contains(body, fmt.Sprint(str))
123 Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))