echotest

package
v0.1.95 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package echotest builds echo contexts for handler unit tests so each test states only what differs: method, target, body, and the occasional header or path value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any](t testing.TB, rec *httptest.ResponseRecorder) T

Decode unmarshals the recorded JSON response body into T, failing the test when it is not valid JSON for T.

func Get

func Get(t testing.TB, target string, opts ...Option) (*echo.Context, *httptest.ResponseRecorder)

Get is Request for a GET with no body.

func Post

func Post(t testing.TB, target string, body any, opts ...Option) (*echo.Context, *httptest.ResponseRecorder)

Post is Request for a POST with a JSON body.

func Request

func Request(t testing.TB, method, target string, body any, opts ...Option) (*echo.Context, *httptest.ResponseRecorder)

Request builds an echo context and recorder for a handler call.

body may be nil (no body), a string or []byte (sent verbatim), an io.Reader, or any other value, which is JSON-encoded. A non-nil body gets a JSON Content-Type unless WithContentType overrides it.

Types

type Option

type Option func(*settings)

Option adjusts the request or context built by Request.

func WithContentType

func WithContentType(contentType string) Option

WithContentType overrides the Content-Type set for a non-nil body.

func WithHeader

func WithHeader(key, value string) Option

WithHeader sets a request header.

func WithIPExtractor added in v0.1.95

func WithIPExtractor(extractor echo.IPExtractor) Option

WithIPExtractor installs the client address strategy the server would apply, so c.RealIP() reads forwarding headers the way the deployment does.

func WithPath

func WithPath(path string) Option

WithPath sets the route path template (c.Path), for handlers that inspect it.

func WithPathValue

func WithPathValue(name, value string) Option

WithPathValue binds a route parameter (c.Param) for the handler.

func WithRemoteAddr added in v0.1.95

func WithRemoteAddr(addr string) Option

WithRemoteAddr sets the address of the connection the request arrived on, for handlers that distinguish the socket peer from a forwarded client. The value may carry a port ("203.0.113.7:4321") or not.

func WithResponseWriter added in v0.1.95

func WithResponseWriter(wrap func(http.ResponseWriter) http.ResponseWriter) Option

WithResponseWriter serves the response through wrap(recorder) instead of the recorder directly, for a handler whose writes or flushes the test needs to observe as they happen. The recorder Request returns is still the one the wrapper writes through, so assertions on the recorded body are unaffected.

func WithValue

func WithValue(key string, value any) Option

WithValue stores a context value (c.Set) before the handler runs, for values middleware would normally provide.

Jump to

Keyboard shortcuts

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