Documentation
¶
Index ¶
- func DeepEqual[V any](t *testing.T, variable string, actual, expected V) bool
- func Equal[V comparable](t TestingT, actual, expected V) bool
- func ErrEqual(t TestingT, actual error, expectedErrorOrMessageOrRegexp any) bool
- type ByteData
- type FixtureFile
- type HTTPRequest
- type HTTPRequestBody
- type HTTPResponseBody
- type JSONFixtureFile
- type JSONObject
- type StringData
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeepEqual ¶
DeepEqual checks if the actual and expected value are equal as determined by reflect.DeepEqual(), and t.Error()s otherwise.
func Equal ¶
func Equal[V comparable](t TestingT, actual, expected V) bool
Equal checks if the actual and expected value are equal according to == rules, and t.Errors() otherwise.
func ErrEqual ¶
ErrEqual checks if the actual error matches the expectation.
- If `expected` is nil, the actual error must be nil.
- If `expected` is of type error, the actual error must be exactly equal to it, or contain it in the sense of errors.Is().
- If `expected` is of type string, the actual error message must be exactly equal to it.
- If `expected` is of type *regexp.Regexp, that regexp must match the actual error message.
Types ¶
type ByteData ¶
type ByteData []byte
ByteData implements the HTTPRequestBody and HTTPResponseBody for plain bytestrings.
func (ByteData) AssertResponseBody ¶
AssertResponseBody implements the HTTPResponseBody interface.
type FixtureFile ¶
type FixtureFile string
FixtureFile implements HTTPResponseBody by locating the expected plain-text response body in the given file.
func (FixtureFile) AssertResponseBody ¶
AssertResponseBody implements the HTTPResponseBody interface.
type HTTPRequest ¶
type HTTPRequest struct {
// request properties
Method string
Path string
Header map[string]string
Body HTTPRequestBody
// response properties
ExpectStatus int
ExpectBody HTTPResponseBody
ExpectHeader map[string]string
}
HTTPRequest is a HTTP request that gets executed by a unit test.
func (HTTPRequest) Check ¶
func (r HTTPRequest) Check(t *testing.T, handler http.Handler) (resp *http.Response, responseBody []byte)
Check performs the HTTP request described by this HTTPRequest against the given http.Handler and compares the response with the expectations in the HTTPRequest.
The HTTP response is returned, along with the response body. (resp.Body is already exhausted when the function returns.) This is useful for tests that want to do further checks on `resp` or want to use data from the response.
Warning: This function is considered deprecated. Please use httptest.Handler instead, which provides more flexible assertions. For example, instead of this:
assert.HTTPRequest {
Method: "GET",
Path: "/v1/info",
ExpectStatus: http.StatusOK,
ExpectBody: assert.JSONObject{"error_count": 0},
}.Check(GinkgoT(), myHandler)
Do this when using the regular std test runner:
h := httptest.NewHandler(myHandler)
resp := h.RespondTo(ctx, "GET /v1/info")
resp.ExpectJSON(t, http.StatusOK, jsonmatch.Object{"error_count": 0})
Or do this when using Ginkgo/Gomega:
h := httptest.NewHandler(myHandler)
var info map[string]any
resp := h.RespondTo(ctx, "GET /v1/info", httptest.ReceiveJSONInto(&info))
Expect(resp).To(HaveHTTPStatus(http.StatusOK))
Expect(info).To(Equal(map[string]any{"error_count": 0}))
type HTTPRequestBody ¶
HTTPRequestBody is the type of field HTTPRequest.RequestBody. It is implemented by StringData and JSONObject.
type HTTPResponseBody ¶
type HTTPResponseBody interface {
// Checks that the given actual response body is equal to this expected value.
// `request` contains a user-readable representation of the original request,
// for use in error messages.
//
// Returns whether the assertion was successful.
AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool
}
HTTPResponseBody is the type of field HTTPRequest.ExpectBody. It is implemented by StringData and JSONObject.
type JSONFixtureFile ¶
type JSONFixtureFile string
JSONFixtureFile implements HTTPResponseBody by locating the expected JSON response body in the given file.
func (JSONFixtureFile) AssertResponseBody ¶
func (f JSONFixtureFile) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool
AssertResponseBody implements the HTTPResponseBody interface.
type JSONObject ¶
JSONObject implements HTTPRequestBody and HTTPResponseBody for JSON objects.
func (JSONObject) AssertResponseBody ¶
AssertResponseBody implements the HTTPResponseBody interface.
func (JSONObject) GetRequestBody ¶
func (o JSONObject) GetRequestBody() (io.Reader, error)
GetRequestBody implements the HTTPRequestBody interface.
type StringData ¶
type StringData string
StringData implements HTTPRequestBody and HTTPResponseBody for plain strings.
func (StringData) AssertResponseBody ¶
AssertResponseBody implements the HTTPResponseBody interface.
func (StringData) GetRequestBody ¶
func (s StringData) GetRequestBody() (io.Reader, error)
GetRequestBody implements the HTTPRequestBody interface.