Documentation
¶
Index ¶
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.
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.