assert

package
v0.0.0-...-fdc0102 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 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(t *testing.T, variable string, actual, expected any) bool

DeepEqual checks if the actual and expected value are equal as determined by reflect.DeepEqual(), and t.Error()s 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.

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.

Jump to

Keyboard shortcuts

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