gosttest

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllowErrors = gosttest.AllowErrors
View Source
var MinLogLevel = gosttest.MinLogLevel
View Source
var NewTestLogger = gosttest.NewTestingLogger

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

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 NewBrowsingContextFromT added in v0.9.2

func NewBrowsingContextFromT(
	t testing.TB,
	h http.Handler,
	opts ...BrowsingContextOption,
) (BrowsingContext, context.CancelFunc)

NewBrowsingContextFromT creates html.BrowsingContext with the logger connected to the current test case, as well as a default context deadline.

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 BrowsingContextOption added in v0.9.2

type BrowsingContextOption func(*browsingContextOption)

func WithTimeoutMS added in v0.9.2

func WithTimeoutMS(ms int) BrowsingContextOption

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

type GomegaSuite struct {
	suite.Suite
}

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

type HTTPRequestRecorder struct {
	T        testing.TB
	Handler  http.Handler
	Requests []*http.Request
}

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 = gosttest.TestingLogHandlerOption

type HttpHandlerMap added in v0.9.1

type HttpHandlerMap map[string]http.Handler

HttpHandlerMap 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 handlers like StaticHTML, StaticJS, or StaticJSON makes it easy build the necessary context for a many test cases.

func (HttpHandlerMap) ServeHTTP added in v0.9.1

func (s HttpHandlerMap) ServeHTTP(w http.ResponseWriter, r *http.Request)

type LogRecorder added in v0.6.0

type LogRecorder struct {
	Records []slog.Record
}

LogRecorder is an implementation of slog.Handler recording all log entries in a slice.

func (*LogRecorder) Enabled added in v0.6.0

func (r *LogRecorder) Enabled(context.Context, slog.Level) bool

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) Handle added in v0.6.0

func (r *LogRecorder) Handle(_ context.Context, rec slog.Record) error

func (*LogRecorder) Reset added in v0.6.0

func (r *LogRecorder) Reset()

func (*LogRecorder) WithAttrs added in v0.6.0

func (r *LogRecorder) WithAttrs(attrs []slog.Attr) slog.Handler

func (*LogRecorder) WithGroup added in v0.6.0

func (r *LogRecorder) WithGroup(name string) slog.Handler

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

type StaticFile struct {
	MIMEType string
	Body     string
}

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)

Jump to

Keyboard shortcuts

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