HTTPStatusCode asserts that a specified handler returns a specified status code. assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501) Returns whether the assertion was successful (true) or not (false).
(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, statuscode int, msgAndArgs ...interface{})
| 93 | // |
| 94 | // Returns whether the assertion was successful (true) or not (false). |
| 95 | func 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), msgAndArgs...) |
| 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), msgAndArgs...) |
| 107 | } |
| 108 | |
| 109 | return successful |
| 110 | } |
| 111 | |
| 112 | // HTTPBody is a helper that returns HTTP body of the response. It returns |
| 113 | // empty string if building a new request fails. |