Documentation
¶
Index ¶
- func ExpectReceive[T any](t testing.TB, c <-chan T, opts ...ExpectOption) (msg T)
- func NewTestLogger(t testing.TB, opts ...TestLoggerOption) *slog.Logger
- type BrowsingContext
- type EchoHandler
- type ExpectOption
- type GomegaSuite
- type HTTPRequestRecorder
- type HandlerOption
- type LogRecorder
- func (r *LogRecorder) Enabled(context.Context, slog.Level) bool
- func (r *LogRecorder) FilterLevel(lvl slog.Level) (ret []slog.Record)
- func (r *LogRecorder) Handle(_ context.Context, rec slog.Record) error
- func (r *LogRecorder) Reset()
- func (r *LogRecorder) WithAttrs(attrs []slog.Attr) slog.Handler
- func (r *LogRecorder) WithGroup(name string) slog.Handler
- type PipeHandler
- func (h *PipeHandler) Close()
- func (h *PipeHandler) Do(f func(http.ResponseWriter))
- func (h *PipeHandler) Flush()
- func (h *PipeHandler) Print(a ...any)
- func (h *PipeHandler) Printf(format string, a ...any)
- func (h *PipeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *PipeHandler) WriteHeader(s int)
- type StaticFile
- type StaticFileServer
- type TestLoggerOption
- type TestingLogHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpectReceive ¶ added in v0.8.0
func ExpectReceive[T any](t testing.TB, c <-chan T, opts ...ExpectOption) (msg T)
ExpectReceive is a test helper verifying that a message can be received
func NewTestLogger ¶ added in v0.6.0
func NewTestLogger(t testing.TB, opts ...TestLoggerOption) *slog.Logger
Types ¶
type BrowsingContext ¶ added in v0.8.0
type BrowsingContext struct {
Ctx context.Context
Client http.Client
Location string
// contains filtered or unexported fields
}
BrowsingContext implements html.BrowsingContext to help test code depending on a context, e.g, fetch and XHR.
func NewBrowsingContext ¶ added in v0.8.0
func NewBrowsingContext(t testing.TB, h http.Handler) BrowsingContext
func (BrowsingContext) Context ¶ added in v0.8.0
func (c BrowsingContext) Context() context.Context
func (BrowsingContext) HTTPClient ¶ added in v0.8.0
func (c BrowsingContext) HTTPClient() http.Client
func (BrowsingContext) LocationHREF ¶ added in v0.8.0
func (c BrowsingContext) LocationHREF() string
func (BrowsingContext) Logger ¶ added in v0.8.0
func (c BrowsingContext) Logger() *slog.Logger
type EchoHandler ¶
type EchoHandler struct {
// Requests contains all HTTP requests sent to the handler
Requests []*http.Request
}
EchoHandler is an http.Handler that echos the requested path in the document heading, i.e., the <h1> element.
func (*EchoHandler) RequestCount ¶
func (h *EchoHandler) RequestCount() int
RequestCount returns how many HTTP requests have been made to the handler
func (*EchoHandler) ServeHTTP ¶
func (h *EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ExpectOption ¶ added in v0.8.0
type ExpectOption func(*expectOptions)
func Context ¶ added in v0.8.0
func Context(ctx context.Context) ExpectOption
func FatalOnError ¶ added in v0.8.0
func FatalOnError() ExpectOption
func Timeout ¶ added in v0.8.0
func Timeout(d time.Duration) ExpectOption
type GomegaSuite ¶
GomegaSuite is a specialised suite.Suite that provides support for the gomega assertion library by providing a GomegaSuite.Expect function automatically initializes gomega with
func (*GomegaSuite) Expect ¶
func (s *GomegaSuite) Expect(actual interface{}, extra ...interface{}) types.Assertion
Expect is wraps gomega/Gomega.Expect, passing the correct testing/T. This supports gomega matchers for verification; which can in some cases provide more expressive tests.
type HTTPRequestRecorder ¶ added in v0.6.0
HTTPRequestRecorder is an HTTPHandler middleware that keeps a record of all incoming request objects.
func NewHTTPRequestRecorder ¶ added in v0.6.0
func NewHTTPRequestRecorder(t testing.TB, handler http.Handler) *HTTPRequestRecorder
func (*HTTPRequestRecorder) Clear ¶ added in v0.6.0
func (r *HTTPRequestRecorder) Clear()
Clear deletes all recorded Requests.
func (*HTTPRequestRecorder) ServeHTTP ¶ added in v0.6.0
func (rec *HTTPRequestRecorder) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*HTTPRequestRecorder) Single ¶ added in v0.6.0
func (r *HTTPRequestRecorder) Single() *http.Request
func (HTTPRequestRecorder) URLs ¶ added in v0.6.0
func (r HTTPRequestRecorder) URLs() []string
URLs return all URL strings recorded
type HandlerOption ¶ added in v0.8.0
type HandlerOption = func(*TestingLogHandler)
func AllowErrors ¶ added in v0.8.0
func AllowErrors() HandlerOption
func MinLogLevel ¶ added in v0.8.0
func MinLogLevel(lvl slog.Level) HandlerOption
type LogRecorder ¶ added in v0.6.0
LogRecorder is an implementation of slog.Handler recording all log entries in a slice.
func (*LogRecorder) FilterLevel ¶ added in v0.6.0
func (r *LogRecorder) FilterLevel(lvl slog.Level) (ret []slog.Record)
FilterLevel returns a slice of log records with the specified log level
func (*LogRecorder) Reset ¶ added in v0.6.0
func (r *LogRecorder) Reset()
type PipeHandler ¶ added in v0.8.0
type PipeHandler struct {
T testing.TB
BufSize uint
Req *http.Request
// ClientDisconnected tells whther the HTTP client disconnects before the
// handler has completed.
ClientDisconnected bool
// contains filtered or unexported fields
}
PipeHandler is reminescent of an io pipe, connecting an output stream to an input stream. This handler works as an HTTP handler that can be connected to, but client code can control the response.
This is intended to be used in a test context, where it is designed to process exactly one request.
All "write" methods return immediately, and will eventually perform the requested operation on the actual http.ResponseWriter. Errors returned from the ResponseWriter will result in a call to Error on the testing.TB instance.
"Write" methods are sent to a buffered channel. BufSize controls the size of the buffer. If zero, a default value of 16 is used.
If no TB instance is passed, errors are silently ignored.
The zero value is safe to use but will not communicate errors, nor will it cancel on timeout.
func NewPipeHandler ¶ added in v0.8.0
func NewPipeHandler(t testing.TB) *PipeHandler
Creates a new PipeHandler with a default testing.TB and context.Context instance.
While both are optional, they are recommended options.
func (*PipeHandler) Close ¶ added in v0.8.0
func (h *PipeHandler) Close()
Close closes the "pipe", completing the HTTP response.
func (*PipeHandler) Do ¶ added in v0.8.0
func (h *PipeHandler) Do(f func(http.ResponseWriter))
func (*PipeHandler) Flush ¶ added in v0.8.0
func (h *PipeHandler) Flush()
func (*PipeHandler) Print ¶ added in v0.8.0
func (h *PipeHandler) Print(a ...any)
func (*PipeHandler) Printf ¶ added in v0.8.0
func (h *PipeHandler) Printf(format string, a ...any)
func (*PipeHandler) ServeHTTP ¶ added in v0.8.0
func (h *PipeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*PipeHandler) WriteHeader ¶ added in v0.8.0
func (h *PipeHandler) WriteHeader(s int)
type StaticFile ¶ added in v0.6.0
A simple http.Handler that serves static file content. This type is a pair of MIMEType and body content.
func StaticHTML ¶ added in v0.6.0
func StaticHTML(html string) StaticFile
StaticHTML creates an http.Handler that serves static content with the MIME type, "text/html".
func StaticJS ¶ added in v0.6.0
func StaticJS(js string) StaticFile
StaticJS creates an http.Handler that serves static content with the MIME type, "text/javascript".
func StaticJSON ¶ added in v0.8.0
func StaticJSON(json string) StaticFile
StaticJSON creates an http.Handler that serves static content with the MIME type, "text/javascript".
func (StaticFile) ServeHTTP ¶ added in v0.6.0
func (f StaticFile) ServeHTTP(w http.ResponseWriter, r *http.Request)
type StaticFileServer ¶ added in v0.6.0
StaticFileServer is a simple http.Handler implementing a mux (router) based on a simple map[string]http.Handler, mapping a local path to a handler.
Having a map as an underlying type, you can create the entire http handler as a Go map literal, making it simpler to configure than creating a new mux. Combine with StaticHTML, StaticJS, or StaticJSON (or other variants added after this comment was written), makes it easy to serve static files for test content.
TODO: Give this a new name. The first version was _only_ static files, but after extracting the StaticFile type as a dedicated and valid http.Handler implementation, this has been elevated to a more general use case.
func (StaticFileServer) ServeHTTP ¶ added in v0.6.0
func (s StaticFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
type TestLoggerOption ¶ added in v0.8.0
type TestLoggerOption = func(*TestingLogHandler)