forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/stretchr/testify/assert/http_assertions.go

282 lines
5.7 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package assert
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
)
// httpCode is a helper that returns HTTP code of the response. It returns -1 and
2024-04-26 19:30:35 +00:00
// an error if building a new request fails.
2024-04-26 19:30:35 +00:00
func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) {
2024-04-26 19:30:35 +00:00
w := httptest.NewRecorder()
2024-05-14 13:07:09 +00:00
req, err := http.NewRequest(method, url, http.NoBody)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return -1, err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
req.URL.RawQuery = values.Encode()
2024-04-26 19:30:35 +00:00
handler(w, req)
2024-04-26 19:30:35 +00:00
return w.Code, nil
2024-04-26 19:30:35 +00:00
}
// HTTPSuccess asserts that a specified handler returns a success status code.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Returns whether the assertion was successful (true) or not (false).
2024-04-26 19:30:35 +00:00
func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool {
2024-04-26 19:30:35 +00:00
if h, ok := t.(tHelper); ok {
2024-04-26 19:30:35 +00:00
h.Helper()
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
code, err := httpCode(handler, method, url, values)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent
2024-04-26 19:30:35 +00:00
if !isSuccessCode {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
return isSuccessCode
2024-04-26 19:30:35 +00:00
}
// HTTPRedirect asserts that a specified handler returns a redirect status code.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}}
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Returns whether the assertion was successful (true) or not (false).
2024-04-26 19:30:35 +00:00
func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool {
2024-04-26 19:30:35 +00:00
if h, ok := t.(tHelper); ok {
2024-04-26 19:30:35 +00:00
h.Helper()
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
code, err := httpCode(handler, method, url, values)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
2024-04-26 19:30:35 +00:00
if !isRedirectCode {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
return isRedirectCode
2024-04-26 19:30:35 +00:00
}
// HTTPError asserts that a specified handler returns an error status code.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}}
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Returns whether the assertion was successful (true) or not (false).
2024-04-26 19:30:35 +00:00
func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool {
2024-04-26 19:30:35 +00:00
if h, ok := t.(tHelper); ok {
2024-04-26 19:30:35 +00:00
h.Helper()
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
code, err := httpCode(handler, method, url, values)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
isErrorCode := code >= http.StatusBadRequest
2024-04-26 19:30:35 +00:00
if !isErrorCode {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
return isErrorCode
2024-04-26 19:30:35 +00:00
}
// HTTPStatusCode asserts that a specified handler returns a specified status code.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501)
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Returns whether the assertion was successful (true) or not (false).
2024-04-26 19:30:35 +00:00
func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) bool {
2024-04-26 19:30:35 +00:00
if h, ok := t.(tHelper); ok {
2024-04-26 19:30:35 +00:00
h.Helper()
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
code, err := httpCode(handler, method, url, values)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
successful := code == statuscode
2024-04-26 19:30:35 +00:00
if !successful {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", statuscode, url+"?"+values.Encode(), code), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
return successful
2024-04-26 19:30:35 +00:00
}
// HTTPBody is a helper that returns HTTP body of the response. It returns
2024-04-26 19:30:35 +00:00
// empty string if building a new request fails.
2024-04-26 19:30:35 +00:00
func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
2024-04-26 19:30:35 +00:00
w := httptest.NewRecorder()
2024-05-14 13:07:09 +00:00
if len(values) > 0 {
2024-05-14 13:07:09 +00:00
url += "?" + values.Encode()
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
req, err := http.NewRequest(method, url, http.NoBody)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return ""
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
handler(w, req)
2024-04-26 19:30:35 +00:00
return w.Body.String()
2024-04-26 19:30:35 +00:00
}
// HTTPBodyContains asserts that a specified handler returns a
2024-04-26 19:30:35 +00:00
// body that contains a string.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Returns whether the assertion was successful (true) or not (false).
2024-04-26 19:30:35 +00:00
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
2024-04-26 19:30:35 +00:00
if h, ok := t.(tHelper); ok {
2024-04-26 19:30:35 +00:00
h.Helper()
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
body := HTTPBody(handler, method, url, values)
contains := strings.Contains(body, fmt.Sprint(str))
2024-04-26 19:30:35 +00:00
if !contains {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
return contains
2024-04-26 19:30:35 +00:00
}
// HTTPBodyNotContains asserts that a specified handler returns a
2024-04-26 19:30:35 +00:00
// body that does not contain a string.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky")
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Returns whether the assertion was successful (true) or not (false).
2024-04-26 19:30:35 +00:00
func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool {
2024-04-26 19:30:35 +00:00
if h, ok := t.(tHelper); ok {
2024-04-26 19:30:35 +00:00
h.Helper()
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
body := HTTPBody(handler, method, url, values)
contains := strings.Contains(body, fmt.Sprint(str))
2024-04-26 19:30:35 +00:00
if contains {
2024-05-14 13:07:09 +00:00
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body), msgAndArgs...)
2024-04-26 19:30:35 +00:00
}
return !contains
2024-04-26 19:30:35 +00:00
}