testing

package
v0.1.53 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package testing provides testing utilities for HTTP handlers and common test patterns.

This package provides helpers for testing HTTP handlers, including: - Response status and body assertions - JSON comparison - HTTP handler testing helpers - Stub helpers

Example:

func TestCreateUser(t *testing.T) {
    rr := testing.RunHandler(handler, http.MethodPost, "/users", userJSON)

    testing.AssertStatus(t, rr.Code, http.StatusCreated)
    testing.AssertJSON(t, rr.Body.String(), expected)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertBody

func AssertBody(t *testing.T, got, want string)

AssertBody asserts the body matches expected.

func AssertError

func AssertError(t *testing.T, err error)

AssertError asserts the error is not nil.

func AssertHeader

func AssertHeader(t *testing.T, header http.Header, key, want string)

AssertHeader asserts a response header matches expected.

func AssertJSON

func AssertJSON(t *testing.T, got, want string)

AssertJSON asserts the JSON body matches expected (order-independent).

func AssertJSONContains

func AssertJSONContains(t *testing.T, body, want string)

AssertJSONContains asserts the JSON contains the expected field/value.

func AssertNoError

func AssertNoError(t *testing.T, err error)

AssertNoError asserts the error is nil.

func AssertStatus

func AssertStatus(t *testing.T, got, want int)

AssertStatus asserts the response status matches expected.

func AssertStatusBadRequest

func AssertStatusBadRequest(t *testing.T, code int)

AssertStatusBadRequest asserts the response is 400 Bad Request.

func AssertStatusCreated

func AssertStatusCreated(t *testing.T, code int)

AssertStatusCreated asserts the response is 201 Created.

func AssertStatusForbidden

func AssertStatusForbidden(t *testing.T, code int)

AssertStatusForbidden asserts the response is 403 Forbidden.

func AssertStatusInternalError

func AssertStatusInternalError(t *testing.T, code int)

AssertStatusInternalError asserts the response is 500 Internal Server Error.

func AssertStatusNotFound

func AssertStatusNotFound(t *testing.T, code int)

AssertStatusNotFound asserts the response is 404 Not Found.

func AssertStatusOK

func AssertStatusOK(t *testing.T, code int)

AssertStatusOK asserts the response is 200 OK.

func AssertStatusUnauthorized

func AssertStatusUnauthorized(t *testing.T, code int)

AssertStatusUnauthorized asserts the response is 401 Unauthorized.

func JSONBody

func JSONBody(v any) string

JSONBody creates JSON string from value.

func JSONEqual

func JSONEqual(t *testing.T, got, want string) bool

JSONEqual compares two JSON strings for equality (order-independent).

func NewJSONRequest

func NewJSONRequest(method, path string, body any) *http.Request

NewJSONRequest creates a JSON request from a value.

func NewRequest

func NewRequest(method, path, body string) *http.Request

NewRequest creates a new HTTP request for testing.

func NewRequestWithHeaders

func NewRequestWithHeaders(method, path, body string, headers map[string]string) *http.Request

NewRequestWithHeaders creates a request with custom headers.

func RunHandler

func RunHandler(t *testing.T, handler http.HandlerFunc, method, path, body string) *httptest.ResponseRecorder

RunHandler tests a standard HTTP handler.

Types

type StubCache

type StubCache struct {
	Data map[string]any
}

StubCache is a stub cache for testing.

func (*StubCache) Clear

func (s *StubCache) Clear()

Clear removes all values from cache.

func (*StubCache) Del

func (s *StubCache) Del(key string)

Del removes value from cache.

func (*StubCache) Get

func (s *StubCache) Get(key string) (any, error)

Get returns cached value.

func (*StubCache) Set

func (s *StubCache) Set(key string, value any)

Set stores value in cache.

type StubDB

type StubDB struct {
	FnQuery  func() error
	FnInsert func() error
	FnGet    func() (any, error)
}

StubDB is a stub database for testing.

func (*StubDB) Get

func (s *StubDB) Get() (any, error)

Get stubs database get.

func (*StubDB) Insert

func (s *StubDB) Insert() error

Insert stubs database insert.

func (*StubDB) Query

func (s *StubDB) Query() error

Query stubs database query.

type StubWriter

type StubWriter struct {
	Code int
	Body string
	Hdr  http.Header
}

StubWriter is a stub http.ResponseWriter for testing.

func NewStubWriter

func NewStubWriter() *StubWriter

NewStubWriter creates a new stub writer.

func (*StubWriter) Header

func (w *StubWriter) Header() http.Header

Header returns the header map.

func (*StubWriter) Write

func (w *StubWriter) Write(b []byte) (int, error)

Write writes the body.

func (*StubWriter) WriteHeader

func (w *StubWriter) WriteHeader(code int)

WriteHeader writes the status code.

Jump to

Keyboard shortcuts

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