package assertimport ()// httpCode is a helper that returns HTTP code of the response. It returns -1 and// an error if building a new request fails.func httpCode( http.HandlerFunc, , string, url.Values) (int, error) { := httptest.NewRecorder() , := http.NewRequest(, , http.NoBody)if != nil {return -1, } .URL.RawQuery = .Encode() (, )return .Code, nil}// HTTPSuccess asserts that a specified handler returns a success status code.//// assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)//// Returns whether the assertion was successful (true) or not (false).func ( TestingT, http.HandlerFunc, , string, url.Values, ...interface{}) bool {if , := .(tHelper); { .Helper() } , := httpCode(, , , )if != nil {Fail(, fmt.Sprintf("Failed to build test request, got error: %s", ), ...) } := >= http.StatusOK && <= http.StatusPartialContentif ! {Fail(, fmt.Sprintf("Expected HTTP success status code for %q but received %d", +"?"+.Encode(), ), ...) }return}// HTTPRedirect asserts that a specified handler returns a redirect status code.//// assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}//// Returns whether the assertion was successful (true) or not (false).func ( TestingT, http.HandlerFunc, , string, url.Values, ...interface{}) bool {if , := .(tHelper); { .Helper() } , := httpCode(, , , )if != nil {Fail(, fmt.Sprintf("Failed to build test request, got error: %s", ), ...) } := >= http.StatusMultipleChoices && <= http.StatusTemporaryRedirectif ! {Fail(, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", +"?"+.Encode(), ), ...) }return}// HTTPError asserts that a specified handler returns an error status code.//// assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}//// Returns whether the assertion was successful (true) or not (false).func ( TestingT, http.HandlerFunc, , string, url.Values, ...interface{}) bool {if , := .(tHelper); { .Helper() } , := httpCode(, , , )if != nil {Fail(, fmt.Sprintf("Failed to build test request, got error: %s", ), ...) } := >= http.StatusBadRequestif ! {Fail(, fmt.Sprintf("Expected HTTP error status code for %q but received %d", +"?"+.Encode(), ), ...) }return}// 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).func ( TestingT, http.HandlerFunc, , string, url.Values, int, ...interface{}) bool {if , := .(tHelper); { .Helper() } , := httpCode(, , , )if != nil {Fail(, fmt.Sprintf("Failed to build test request, got error: %s", ), ...) } := == if ! {Fail(, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", , +"?"+.Encode(), ), ...) }return}// HTTPBody is a helper that returns HTTP body of the response. It returns// empty string if building a new request fails.func ( http.HandlerFunc, , string, url.Values) string { := httptest.NewRecorder()iflen() > 0 { += "?" + .Encode() } , := http.NewRequest(, , http.NoBody)if != nil {return"" } (, )return .Body.String()}// HTTPBodyContains asserts that a specified handler returns a// body that contains a string.//// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")//// Returns whether the assertion was successful (true) or not (false).func ( TestingT, http.HandlerFunc, , string, url.Values, interface{}, ...interface{}) bool {if , := .(tHelper); { .Helper() } := HTTPBody(, , , ) := strings.Contains(, fmt.Sprint())if ! {Fail(, fmt.Sprintf("Expected response body for %q to contain %q but found %q", +"?"+.Encode(), , ), ...) }return}// HTTPBodyNotContains asserts that a specified handler returns a// body that does not contain a string.//// assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")//// Returns whether the assertion was successful (true) or not (false).func ( TestingT, http.HandlerFunc, , string, url.Values, interface{}, ...interface{}) bool {if , := .(tHelper); { .Helper() } := HTTPBody(, , , ) := strings.Contains(, fmt.Sprint())if {Fail(, fmt.Sprintf("Expected response body for %q to NOT contain %q but found %q", +"?"+.Encode(), , ), ...) }return !}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.