testing

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

testing/helpers.go

testing/recorder.go

testing/testing.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context(t *testing.T) context.Context

Context returns a context with a reasonable timeout for tests.

func ContextWithTimeout

func ContextWithTimeout(t *testing.T, timeout time.Duration) context.Context

ContextWithTimeout returns a context with a custom timeout.

func DevLogger

func DevLogger() *zap.Logger

DevLogger returns a development logger for debugging tests.

func Eventually

func Eventually(t *testing.T, check func() bool, timeout, interval time.Duration)

Eventually retries a check function until it passes or times out.

func EventuallyWithCollect

func EventuallyWithCollect(t *testing.T, check func(c Collect), timeout, interval time.Duration)

EventuallyWithCollect retries a check function that uses a Collect interface until it passes.

func MustJSON

func MustJSON(t *testing.T, v any) []byte

MustJSON marshals v to JSON, failing the test on error.

func MustJSONString

func MustJSONString(t *testing.T, v any) string

MustJSONString marshals v to a JSON string, failing the test on error.

func Parallel

func Parallel(t *testing.T)

Parallel marks the test as parallel. Convenience wrapper for t.Parallel().

func ReadFixture

func ReadFixture(t *testing.T, path string) []byte

ReadFixture reads a fixture file from testdata directory.

func ReadJSONFixture

func ReadJSONFixture(t *testing.T, path string, v any)

ReadJSONFixture reads and unmarshals a JSON fixture file.

func RequireEnv

func RequireEnv(t *testing.T, key string) string

RequireEnv skips the test if an environment variable is not set.

func SetEnv

func SetEnv(t *testing.T, key, value string)

SetEnv sets an environment variable for the duration of the test.

func Skip

func Skip(t *testing.T, condition bool, reason string)

Skip skips the test if the condition is true.

func SkipCI

func SkipCI(t *testing.T)

SkipCI skips the test if running in CI environment.

func SkipShort

func SkipShort(t *testing.T)

SkipShort skips the test if running with -short flag.

func TempDir

func TempDir(t *testing.T) string

TempDir creates a temporary directory that is cleaned up after the test.

func TempFile

func TempFile(t *testing.T, name, content string) string

TempFile creates a temporary file with the given content.

func TestLogger

func TestLogger() *zap.Logger

TestLogger returns a no-op logger for tests.

func UnsetEnv

func UnsetEnv(t *testing.T, key string)

UnsetEnv unsets an environment variable for the duration of the test.

Types

type Collect

type Collect interface {
	Error(args ...any)
	Errorf(format string, args ...any)
	Fail()
	FailNow()
	Failed() bool
}

Collect is an interface for collecting test failures.

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder provides a way to test handlers without starting a server.

func NewRecorder

func NewRecorder(t *testing.T) *Recorder

NewRecorder creates a new Recorder for testing.

func (*Recorder) Delete

func (rec *Recorder) Delete(path string) *RecorderRequest

Delete creates a DELETE request.

func (*Recorder) Get

func (rec *Recorder) Get(path string) *RecorderRequest

Get creates a GET request.

func (*Recorder) Patch

func (rec *Recorder) Patch(path string) *RecorderRequest

Patch creates a PATCH request.

func (*Recorder) Post

func (rec *Recorder) Post(path string) *RecorderRequest

Post creates a POST request.

func (*Recorder) Put

func (rec *Recorder) Put(path string) *RecorderRequest

Put creates a PUT request.

func (*Recorder) Request

func (rec *Recorder) Request(method, path string) *RecorderRequest

Request creates a new recorder request builder.

type RecorderRequest

type RecorderRequest struct {
	// contains filtered or unexported fields
}

RecorderRequest builds a request for handler testing.

func (*RecorderRequest) BasicAuth

func (rr *RecorderRequest) BasicAuth(username, password string) *RecorderRequest

BasicAuth sets Basic auth header.

func (*RecorderRequest) Bearer

func (rr *RecorderRequest) Bearer(token string) *RecorderRequest

Bearer sets the Authorization header with a Bearer token.

func (*RecorderRequest) Body

func (rr *RecorderRequest) Body(body io.Reader) *RecorderRequest

Body sets the request body.

func (*RecorderRequest) BodyBytes

func (rr *RecorderRequest) BodyBytes(body []byte) *RecorderRequest

BodyBytes sets the request body from bytes.

func (*RecorderRequest) BodyString

func (rr *RecorderRequest) BodyString(body string) *RecorderRequest

BodyString sets the request body from a string.

func (*RecorderRequest) Build

func (rr *RecorderRequest) Build() *http.Request

Build creates the http.Request without executing it.

func (*RecorderRequest) Cookie

func (rr *RecorderRequest) Cookie(name, value string) *RecorderRequest

Cookie adds a cookie to the request.

func (*RecorderRequest) Form

func (rr *RecorderRequest) Form(data url.Values) *RecorderRequest

Form sets the request body as form data.

func (*RecorderRequest) Header

func (rr *RecorderRequest) Header(key, value string) *RecorderRequest

Header sets a request header.

func (*RecorderRequest) Headers

func (rr *RecorderRequest) Headers(headers map[string]string) *RecorderRequest

Headers sets multiple request headers.

func (*RecorderRequest) JSON

func (rr *RecorderRequest) JSON(v any) *RecorderRequest

JSON sets the request body as JSON.

func (*RecorderRequest) Queries

func (rr *RecorderRequest) Queries(params map[string]string) *RecorderRequest

Queries sets multiple query parameters.

func (*RecorderRequest) Query

func (rr *RecorderRequest) Query(key, value string) *RecorderRequest

Query sets a query parameter.

func (*RecorderRequest) Run

func (rr *RecorderRequest) Run(handler http.Handler) *Response

Run executes the request against a handler and returns the response.

func (*RecorderRequest) RunFunc

func (rr *RecorderRequest) RunFunc(handler http.HandlerFunc) *Response

RunFunc executes the request against a handler function.

type RequestBuilder

type RequestBuilder struct {
	// contains filtered or unexported fields
}

RequestBuilder builds and executes HTTP requests.

func (*RequestBuilder) BasicAuth

func (rb *RequestBuilder) BasicAuth(username, password string) *RequestBuilder

BasicAuth sets the Authorization header with Basic auth.

func (*RequestBuilder) Bearer

func (rb *RequestBuilder) Bearer(token string) *RequestBuilder

Bearer sets the Authorization header with a Bearer token.

func (*RequestBuilder) Body

func (rb *RequestBuilder) Body(body io.Reader) *RequestBuilder

Body sets the request body.

func (*RequestBuilder) BodyBytes

func (rb *RequestBuilder) BodyBytes(body []byte) *RequestBuilder

BodyBytes sets the request body from bytes.

func (*RequestBuilder) BodyString

func (rb *RequestBuilder) BodyString(body string) *RequestBuilder

BodyString sets the request body from a string.

func (*RequestBuilder) Cookie

func (rb *RequestBuilder) Cookie(name, value string) *RequestBuilder

Cookie adds a cookie to the request.

func (*RequestBuilder) Do

func (rb *RequestBuilder) Do() *Response

Do executes the request and returns a Response.

func (*RequestBuilder) Form

func (rb *RequestBuilder) Form(data url.Values) *RequestBuilder

Form sets the request body as form data and sets the Content-Type header.

func (*RequestBuilder) Header

func (rb *RequestBuilder) Header(key, value string) *RequestBuilder

Header sets a request header.

func (*RequestBuilder) Headers

func (rb *RequestBuilder) Headers(headers map[string]string) *RequestBuilder

Headers sets multiple request headers.

func (*RequestBuilder) JSON

func (rb *RequestBuilder) JSON(v any) *RequestBuilder

JSON sets the request body as JSON and sets the Content-Type header.

func (*RequestBuilder) Queries

func (rb *RequestBuilder) Queries(params map[string]string) *RequestBuilder

Queries sets multiple query parameters.

func (*RequestBuilder) Query

func (rb *RequestBuilder) Query(key, value string) *RequestBuilder

Query sets a query parameter.

type Response

type Response struct {
	*http.Response
	Body []byte
	// contains filtered or unexported fields
}

Response wraps http.Response with assertion methods.

func (*Response) BodyContains

func (r *Response) BodyContains(substr string) *Response

BodyContains asserts the body contains a substring.

func (*Response) BodyEmpty

func (r *Response) BodyEmpty() *Response

BodyEmpty asserts the body is empty.

func (*Response) BodyEquals

func (r *Response) BodyEquals(expected string) *Response

BodyEquals asserts the body equals the expected string.

func (*Response) BodyNotContains

func (r *Response) BodyNotContains(substr string) *Response

BodyNotContains asserts the body does not contain a substring.

func (*Response) Bytes

func (r *Response) Bytes() []byte

Bytes returns the response body as bytes.

func (*Response) ContentType

func (r *Response) ContentType(expected string) *Response

ContentType asserts the Content-Type header.

func (*Response) ContentTypeJSON

func (r *Response) ContentTypeJSON() *Response

ContentTypeJSON asserts Content-Type is application/json.

func (*Response) HeaderContains

func (r *Response) HeaderContains(key, substr string) *Response

HeaderContains asserts a header contains a substring.

func (*Response) HeaderEquals

func (r *Response) HeaderEquals(key, expected string) *Response

HeaderEquals asserts a header value.

func (*Response) HeaderExists

func (r *Response) HeaderExists(key string) *Response

HeaderExists asserts a header exists.

func (*Response) JSON

func (r *Response) JSON(v any) *Response

JSON unmarshals the body into v.

func (*Response) JSONPath

func (r *Response) JSONPath(path string) any

JSONPath extracts a value from the JSON body at the given path. Path is dot-separated (e.g., "user.name", "items.0.id").

func (*Response) JSONPathContains

func (r *Response) JSONPathContains(path, substr string) *Response

JSONPathContains asserts a JSON path contains a substring (for strings).

func (*Response) JSONPathEquals

func (r *Response) JSONPathEquals(path string, expected any) *Response

JSONPathEquals asserts a JSON path equals an expected value.

func (*Response) Print

func (r *Response) Print() *Response

Print prints the response for debugging.

func (*Response) Status

func (r *Response) Status(code int) *Response

Status asserts the response status code.

func (*Response) StatusBadRequest

func (r *Response) StatusBadRequest() *Response

StatusBadRequest asserts 400 Bad Request.

func (*Response) StatusConflict

func (r *Response) StatusConflict() *Response

StatusConflict asserts 409 Conflict.

func (*Response) StatusCreated

func (r *Response) StatusCreated() *Response

StatusCreated asserts 201 Created.

func (*Response) StatusForbidden

func (r *Response) StatusForbidden() *Response

StatusForbidden asserts 403 Forbidden.

func (*Response) StatusInternalServerError

func (r *Response) StatusInternalServerError() *Response

StatusInternalServerError asserts 500 Internal Server Error.

func (*Response) StatusNoContent

func (r *Response) StatusNoContent() *Response

StatusNoContent asserts 204 No Content.

func (*Response) StatusNotFound

func (r *Response) StatusNotFound() *Response

StatusNotFound asserts 404 Not Found.

func (*Response) StatusOK

func (r *Response) StatusOK() *Response

StatusOK asserts 200 OK.

func (*Response) StatusTooManyRequests

func (r *Response) StatusTooManyRequests() *Response

StatusTooManyRequests asserts 429 Too Many Requests.

func (*Response) StatusUnauthorized

func (r *Response) StatusUnauthorized() *Response

StatusUnauthorized asserts 401 Unauthorized.

func (*Response) StatusUnprocessableEntity

func (r *Response) StatusUnprocessableEntity() *Response

StatusUnprocessableEntity asserts 422 Unprocessable Entity.

func (*Response) String

func (r *Response) String() string

String returns the response body as a string.

type Server

type Server struct {
	*httptest.Server
	Router chi.Router
	// contains filtered or unexported fields
}

Server wraps httptest.Server with convenience methods for testing.

func NewServer

func NewServer(t *testing.T, r chi.Router) *Server

NewServer creates a test server with the given router.

func NewTLSServer

func NewTLSServer(t *testing.T, r chi.Router) *Server

NewTLSServer creates a test server with TLS.

func (*Server) Delete

func (s *Server) Delete(path string) *RequestBuilder

Delete creates a DELETE request builder.

func (*Server) Get

func (s *Server) Get(path string) *RequestBuilder

Get creates a GET request builder.

func (*Server) Patch

func (s *Server) Patch(path string) *RequestBuilder

Patch creates a PATCH request builder.

func (*Server) Post

func (s *Server) Post(path string) *RequestBuilder

Post creates a POST request builder.

func (*Server) Put

func (s *Server) Put(path string) *RequestBuilder

Put creates a PUT request builder.

func (*Server) Request

func (s *Server) Request(method, path string) *RequestBuilder

Request creates a new request builder for the server.

Jump to

Keyboard shortcuts

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