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 ¶
- func Decode[T any](t testing.TB, rec *httptest.ResponseRecorder) T
- func Get(t testing.TB, target string, opts ...Option) (*echo.Context, *httptest.ResponseRecorder)
- func Post(t testing.TB, target string, body any, opts ...Option) (*echo.Context, *httptest.ResponseRecorder)
- func Request(t testing.TB, method, target string, body any, opts ...Option) (*echo.Context, *httptest.ResponseRecorder)
- type Option
- func WithContentType(contentType string) Option
- func WithHeader(key, value string) Option
- func WithIPExtractor(extractor echo.IPExtractor) Option
- func WithPath(path string) Option
- func WithPathValue(name, value string) Option
- func WithRemoteAddr(addr string) Option
- func WithResponseWriter(wrap func(http.ResponseWriter) http.ResponseWriter) Option
- func WithValue(key string, value any) Option
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 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 ¶
WithContentType overrides the Content-Type set for a non-nil body.
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 WithPathValue ¶
WithPathValue binds a route parameter (c.Param) for the handler.
func WithRemoteAddr ¶ added in v0.1.95
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.