assert

package
v0.0.0-...-32e18bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual

func DeepEqual[V any](t *testing.T, variable string, actual, expected V) bool

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

func (b ByteData) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool

AssertResponseBody implements the HTTPResponseBody interface.

func (ByteData) GetRequestBody

func (b ByteData) GetRequestBody() (io.Reader, error)

GetRequestBody implements the HTTPRequestBody interface.

type FixtureFile

type FixtureFile string

FixtureFile implements HTTPResponseBody by locating the expected plain-text response body in the given file.

func (FixtureFile) AssertResponseBody

func (f FixtureFile) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool

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

type HTTPRequestBody interface {
	GetRequestBody() (io.Reader, error)
}

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

type JSONObject map[string]any

JSONObject implements HTTPRequestBody and HTTPResponseBody for JSON objects.

func (JSONObject) AssertResponseBody

func (o JSONObject) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool

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

func (s StringData) AssertResponseBody(t *testing.T, requestInfo string, responseBody []byte) bool

AssertResponseBody implements the HTTPResponseBody interface.

func (StringData) GetRequestBody

func (s StringData) GetRequestBody() (io.Reader, error)

GetRequestBody implements the HTTPRequestBody interface.

type TestingT

type TestingT interface {
	Helper()
	Errorf(msg string, args ...any)
}

TestingT is an interface implemented by the *testing.T type. Some tests inside go-bits use this interface to substitute a mock for the real *testing.T type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL