testutil

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package testutil provides test environment setup and utilities for internal package tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpectRedirect

func ExpectRedirect(
	t *testing.T,
	result HTTPResult,
) string

ExpectRedirect validates a redirect response and returns the Location header

func ExpectStatus

func ExpectStatus(
	t *testing.T,
	expected int,
	result HTTPResult,
)

ExpectStatus validates the HTTP status code and fails the test if it doesn't match

Types

type HTTPResult

type HTTPResult struct {
	Code    int
	Error   error
	Headers http.Header
	Body    []byte
}

HTTPResult captures HTTP response details for test assertions

func Get

func Get(
	router http.Handler,
	url string,
	response any,
	headers ...Header,
) HTTPResult

Get performs a GET request and optionally decodes JSON response

func Post

func Post(
	router http.Handler,
	url string,
	body string,
	response any,
	headers ...Header,
) HTTPResult

Post performs a POST request and optionally decodes JSON response

func PostForm

func PostForm(
	router http.Handler,
	urlPath string,
	values url.Values,
	response any,
) HTTPResult

PostForm performs a POST with form-urlencoded body

func PostJSON

func PostJSON(
	router http.Handler,
	urlPath string,
	body string,
	response any,
) HTTPResult

PostJSON performs a POST with JSON body

type Header struct {
	Key   string
	Value string
}

Header represents an HTTP header key-value pair

func ContentTypeForm

func ContentTypeForm() Header

ContentTypeForm returns a header for form-urlencoded content type

func ContentTypeJSON

func ContentTypeJSON() Header

ContentTypeJSON returns a header for JSON content type

type TestEnv

type TestEnv struct {
	DB             *database.SQLiteStore
	Service        *service.Service
	Router         http.Handler
	TokenIssuer    tokens.Issuer
	TokenValidator tokens.Validator
}

TestEnv provides all dependencies needed for testing

func SetupTestEnv

func SetupTestEnv(
	t *testing.T,
) *TestEnv

SetupTestEnv creates an isolated test environment with in-memory SQLite

func SetupTestEnvWithRouter

func SetupTestEnvWithRouter(
	t *testing.T,
) *TestEnv

SetupTestEnvWithRouter creates TestEnv and configures the API router

func (*TestEnv) IssueTestAccessToken

func (env *TestEnv) IssueTestAccessToken(
	t *testing.T,
	subject string,
	audience []string,
) *tokens.AccessToken

IssueTestAccessToken creates an access token for testing

func (*TestEnv) IssueTestRefreshToken

func (env *TestEnv) IssueTestRefreshToken(
	t *testing.T,
	subject string,
	audience []string,
) *tokens.RefreshToken

IssueTestRefreshToken creates a refresh token for testing

func (*TestEnv) RegisterTestUser

func (env *TestEnv) RegisterTestUser(
	t *testing.T,
	handle string,
	password string,
)

RegisterTestUser creates a test user in the database

func (*TestEnv) StoreTestRefreshToken

func (env *TestEnv) StoreTestRefreshToken(
	t *testing.T,
	handle string,
	audience []string,
) *tokens.RefreshToken

StoreTestRefreshToken issues and stores a refresh token in the database

Jump to

Keyboard shortcuts

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