1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00
woodpecker/vendor/github.com/stretchr/testify/assert/http_assertions.go

107 lines
3.5 KiB
Go
Raw Normal View History

2015-09-30 23:00:48 +02: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
// if building a new request fails.
2016-03-07 21:23:49 +02:00
func httpCode(handler http.HandlerFunc, method, url string, values url.Values) int {
2015-09-30 23:00:48 +02:00
w := httptest.NewRecorder()
2016-03-07 21:23:49 +02:00
req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
2015-09-30 23:00:48 +02:00
if err != nil {
return -1
}
handler(w, req)
return w.Code
}
// HTTPSuccess asserts that a specified handler returns a success status code.
//
2016-03-07 21:23:49 +02:00
// assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil)
2015-09-30 23:00:48 +02:00
//
// Returns whether the assertion was successful (true) or not (false).
2016-03-07 21:23:49 +02:00
func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
code := httpCode(handler, method, url, values)
2015-09-30 23:00:48 +02:00
if code == -1 {
return false
}
return code >= http.StatusOK && code <= http.StatusPartialContent
}
// 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).
2016-03-07 21:23:49 +02:00
func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
code := httpCode(handler, method, url, values)
2015-09-30 23:00:48 +02:00
if code == -1 {
return false
}
return code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect
}
// 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).
2016-03-07 21:23:49 +02:00
func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values) bool {
code := httpCode(handler, method, url, values)
2015-09-30 23:00:48 +02:00
if code == -1 {
return false
}
return code >= http.StatusBadRequest
}
2016-03-07 21:23:49 +02:00
// HTTPBody is a helper that returns HTTP body of the response. It returns
2015-09-30 23:00:48 +02:00
// empty string if building a new request fails.
2016-03-07 21:23:49 +02:00
func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string {
2015-09-30 23:00:48 +02:00
w := httptest.NewRecorder()
2016-03-07 21:23:49 +02:00
req, err := http.NewRequest(method, url+"?"+values.Encode(), nil)
2015-09-30 23:00:48 +02:00
if err != nil {
return ""
}
handler(w, req)
return w.Body.String()
}
// HTTPBodyContains asserts that a specified handler returns a
// body that contains a string.
//
// assert.HTTPBodyContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
//
// Returns whether the assertion was successful (true) or not (false).
2016-03-07 21:23:49 +02:00
func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
body := HTTPBody(handler, method, url, values)
2015-09-30 23:00:48 +02:00
contains := strings.Contains(body, fmt.Sprint(str))
if !contains {
Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body))
}
return contains
}
// HTTPBodyNotContains asserts that a specified handler returns a
// body that does not contain a string.
//
// assert.HTTPBodyNotContains(t, myHandler, "www.google.com", nil, "I'm Feeling Lucky")
//
// Returns whether the assertion was successful (true) or not (false).
2016-03-07 21:23:49 +02:00
func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}) bool {
body := HTTPBody(handler, method, url, values)
2015-09-30 23:00:48 +02:00
contains := strings.Contains(body, fmt.Sprint(str))
if contains {
Fail(t, "Expected response body for %s to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)
}
return !contains
}