testhelper

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package testhelper container methods that are useful for writing unit tests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Mux is a multiplexer that can be used to register handlers.
	Mux *http.ServeMux

	// Server is an in-memory HTTP server for testing.
	Server *httptest.Server
)

Functions

func AssertByteArrayEquals

func AssertByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte)

AssertByteArrayEquals a convenience function for checking whether two byte arrays are equal

func AssertDeepEquals

func AssertDeepEquals(t *testing.T, expected, actual any)

AssertDeepEquals - like Equals - performs a comparison - but on more complex structures that requires deeper inspection

func AssertEquals

func AssertEquals(t *testing.T, expected, actual any)

AssertEquals compares two arbitrary values and performs a comparison. If the comparison fails, a fatal error is raised that will fail the test

func AssertErr

func AssertErr(t *testing.T, e error)

AssertErr is a convenience function for checking whether an error value is nil

func AssertErrIs

func AssertErrIs(t *testing.T, e error, target error)

AssertErrIs is a convenience function for checking whether an error value is target one

func AssertIntGreaterOrEqual

func AssertIntGreaterOrEqual(t *testing.T, v1 int, v2 int)

AssertIntGreaterOrEqual verifies that first value is greater or equal than second values

func AssertIntLesserOrEqual

func AssertIntLesserOrEqual(t *testing.T, v1 int, v2 int)

AssertIntLesserOrEqual verifies that first value is lesser or equal than second values

func AssertJSONEquals

func AssertJSONEquals(t *testing.T, expectedJSON string, actual any)

AssertJSONEquals serializes a value as JSON, parses an expected string as JSON, and ensures that both are consistent. If they aren't, the expected and actual structures are pretty-printed and shown for comparison.

This is useful for comparing structures that are built as nested map[string]any values, which are a pain to construct as literals.

func AssertNoErr

func AssertNoErr(t *testing.T, e error)

AssertNoErr is a convenience function for checking whether an error value is an actual error

func CheckByteArrayEquals

func CheckByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte)

CheckByteArrayEquals a convenience function for silent checking whether two byte arrays are equal

func CheckDeepEquals

func CheckDeepEquals(t *testing.T, expected, actual any)

CheckDeepEquals is similar to AssertDeepEquals, except with a non-fatal error

func CheckEquals

func CheckEquals(t *testing.T, expected, actual any)

CheckEquals is similar to AssertEquals, except with a non-fatal error

func CheckErr

func CheckErr(t *testing.T, e error, expected ...any)

CheckErr is similar to AssertErr, except with a non-fatal error. If expected errors are passed, this function also checks that an error in e's tree is assignable to one of them. The tree consists of e itself, followed by the errors obtained by repeatedly calling Unwrap.

CheckErr panics if expected contains anything other than non-nil pointers to either a type that implements error, or to any interface type.

func CheckJSONEquals

func CheckJSONEquals(t *testing.T, expectedJSON string, actual any)

CheckJSONEquals is similar to AssertJSONEquals, but nonfatal.

func CheckNoErr

func CheckNoErr(t *testing.T, e error)

CheckNoErr is similar to AssertNoErr, except with a non-fatal error

func Endpoint

func Endpoint() string

Endpoint returns a fake endpoint that will actually target the Mux.

func ServeFile

func ServeFile(t *testing.T, baseURL, relPath, contentType, content string) string

Serves a static content at baseURL/relPath

func SetupHTTP

func SetupHTTP()

SetupHTTP prepares the Mux and Server.

func SetupPersistentPortHTTP

func SetupPersistentPortHTTP(t *testing.T, port int)

SetupPersistentPortHTTP prepares the Mux and Server listening specific port.

func TeardownHTTP

func TeardownHTTP()

TeardownHTTP releases HTTP-related resources.

func TestBody

func TestBody(t *testing.T, r *http.Request, expected string)

TestBody verifies that the request body matches an expected body.

func TestFormValues

func TestFormValues(t *testing.T, r *http.Request, values map[string]string)

TestFormValues ensures that all the URL parameters given to the http.Request are the same as values.

func TestHeader

func TestHeader(t *testing.T, r *http.Request, header string, expected string)

TestHeader checks that the header on the http.Request matches the expected value.

func TestHeaderUnset

func TestHeaderUnset(t *testing.T, r *http.Request, header string)

TestHeaderUnset checks that the header on the http.Request doesn't exist.

func TestJSONRequest

func TestJSONRequest(t *testing.T, r *http.Request, expected string)

TestJSONRequest verifies that the JSON payload of a request matches an expected structure, without asserting things about whitespace or ordering.

func TestMethod

func TestMethod(t *testing.T, r *http.Request, expected string)

TestMethod checks that the Request has the expected method (e.g. GET, POST).

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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