handler

package
v0.0.0-...-0deab39 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tester

func Tester(t *testing.T, cases []Case, handler http.Handler)

Tester will test the provided HTTP handler by executing take all the test []Case in a serialized way.

If provided, it will execute the corresponding setUp and tearDown TestAuxFunc functions per each test case. See Case type on how to configure them.

Types

type Case

type Case struct {
	// Case is name for the given test scenario.
	Case,

	Path,
	Method string
	Body    io.Reader
	Headers http.Header
	// Checkers is list of checkers to apply to the response of the request.
	// See functions like handler.CheckContains() on this package.
	Checkers []CheckerFunc
	// The setUp functions optionally allows configuring test dependencies,
	// like databases.
	SetUp TestAuxFunc
	// The tearDown functions optionally allows shutdown test dependencies,
	// like databases.
	TearDown TestAuxFunc
}

Case represents a test case for an HTTP handler.

type CheckerFunc

type CheckerFunc func(t *testing.T, resp *http.Response)

CheckerFunc represents a checker that will assert certain data in handler response.

func CheckContains

func CheckContains(want string) CheckerFunc

CheckContains checks for a valid substring in the response body of an HTTP handler.

func CheckContainsJSON

func CheckContainsJSON(expectedJSON string) CheckerFunc

CheckContainsJSON checks that the provided JSON string matches the one in the response body.

func CheckHasHeaders

func CheckHasHeaders(want http.Header) CheckerFunc

CheckHasHeaders checks the wanted headers are present in the response.

This only checks that the specified headers are present with its respective values. This means that it could be more headers than the expected. In this case the test will pass.

func CheckHasStatus

func CheckHasStatus(want int) CheckerFunc

CheckHasStatus checks for the specified HTTP status code in the handler response.

func CheckMatchesMD5

func CheckMatchesMD5(expectedMD5 string) CheckerFunc

CheckMatchesMD5 checks that the response body of an HTTP handler is equal to the expected MD5 sum.

type TestAuxFunc

type TestAuxFunc func(t *testing.T)

TestAuxFunc its the type for functions used to bring up/down services, as databases, before and after a handler test is executed.

Jump to

Keyboard shortcuts

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