apptest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package apptest provides small helpers for exercising a gin-kit application in tests and decoding its envelope responses. Assertions beyond status checks stay in plain Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(t *testing.T, connection *database.Connection, dialect database.Dialect, dir string)

Migrate applies the goose migrations in dir (e.g. "migrations") to the connection.

func OpenSQLite

func OpenSQLite(t *testing.T, orm database.ORM) *database.Connection

OpenSQLite opens a unique in-memory SQLite connection for integration tests. One connection is pinned for the test's lifetime so pool churn cannot drop the shared-cache database, and everything closes via t.Cleanup. The test is skipped when the SQLite driver is unavailable (CGO disabled).

func SQLiteConfig

func SQLiteConfig(t *testing.T, orm database.ORM) database.Config

SQLiteConfig returns a database configuration for a unique in-memory SQLite database (shared cache, so every pooled connection sees the same data).

func Seed

func Seed(t *testing.T, connection *database.Connection, seeders ...func(context.Context, *database.Connection) error)

Seed runs seeder functions matching the generated Seeder.Run signature.

Types

type App

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

App wraps a runtime application for in-process HTTP testing.

func New

func New(t *testing.T, options runtime.Options) *App

New builds an application from options, fails the test on error, and closes the application (shutdown hooks) when the test finishes.

func (*App) Application

func (a *App) Application() *runtime.Application

Application exposes the wrapped runtime application.

func (*App) Client

func (a *App) Client() *Client

Client returns a stateful client that carries cookies across requests, for session and CSRF flows.

func (*App) DELETE

func (a *App) DELETE(path string, opts ...RequestOption) *Response

DELETE performs this package operation.

func (*App) Do

func (a *App) Do(method, path string, body any, opts ...RequestOption) *Response

Do performs an in-process request. Bodies: nil sends none, Form is form-urlencoded, *MultipartBody is multipart, Raw is sent verbatim, and anything else is JSON-encoded.

func (*App) GET

func (a *App) GET(path string, opts ...RequestOption) *Response

GET performs this package operation.

func (*App) PATCH

func (a *App) PATCH(path string, body any, opts ...RequestOption) *Response

PATCH performs this package operation.

func (*App) POST

func (a *App) POST(path string, body any, opts ...RequestOption) *Response

POST performs this package operation.

func (*App) PUT

func (a *App) PUT(path string, body any, opts ...RequestOption) *Response

PUT performs this package operation.

func (*App) Router

func (a *App) Router() *gin.Engine

Router exposes the underlying engine for route registration.

type Client

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

Client performs requests while persisting cookies between them.

func (*Client) CSRFToken

func (c *Client) CSRFToken(path string) string

CSRFToken GETs path with this client (establishing the session cookie) and extracts the CSRF token from the rendered hidden form field.

func (*Client) DELETE

func (c *Client) DELETE(path string, opts ...RequestOption) *Response

DELETE performs this package operation.

func (*Client) Do

func (c *Client) Do(method, path string, body any, opts ...RequestOption) *Response

Do performs this package operation.

func (*Client) GET

func (c *Client) GET(path string, opts ...RequestOption) *Response

GET performs this package operation.

func (*Client) PATCH

func (c *Client) PATCH(path string, body any, opts ...RequestOption) *Response

PATCH performs this package operation.

func (*Client) POST

func (c *Client) POST(path string, body any, opts ...RequestOption) *Response

POST performs this package operation.

func (*Client) PUT

func (c *Client) PUT(path string, body any, opts ...RequestOption) *Response

PUT performs this package operation.

type Form

type Form = url.Values

Form marks a body as application/x-www-form-urlencoded.

type MultipartBody

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

MultipartBody builds a multipart/form-data request body.

func NewMultipart

func NewMultipart() *MultipartBody

NewMultipart performs this package operation.

func (*MultipartBody) Field

func (m *MultipartBody) Field(name, value string) *MultipartBody

Field performs this package operation.

func (*MultipartBody) File

func (m *MultipartBody) File(field, filename, contentType string, content []byte) *MultipartBody

File performs this package operation.

type Raw

type Raw struct {
	// ContentType store data used by this type.
	ContentType string
	// Body store data used by this type.
	Body []byte
}

Raw sends the body verbatim with the given content type.

type RequestOption

type RequestOption func(*http.Request)

RequestOption customizes one outgoing request.

func WithBearer

func WithBearer(token string) RequestOption

WithBearer sets the Authorization header with a Bearer token.

func WithCookie

func WithCookie(cookie *http.Cookie) RequestOption

WithCookie attaches a cookie to the request.

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader sets a single header.

func WithHeaders

func WithHeaders(h http.Header) RequestOption

WithHeaders adds every value of h.

type Response

type Response struct {
	*httptest.ResponseRecorder
	// contains filtered or unexported fields
}

Response wraps a recorded response with envelope decoding helpers.

func (*Response) Data

func (r *Response) Data(out any) *Response

Data decodes the envelope's data field into out.

func (*Response) Err

func (r *Response) Err() httpx.ErrorBody

Err decodes the error envelope.

func (*Response) JSON

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

JSON decodes the whole response body into out.

func (*Response) Meta

func (r *Response) Meta(out any) *Response

Meta decodes the envelope's meta field into out.

func (*Response) RequireStatus

func (r *Response) RequireStatus(status int) *Response

RequireStatus fails the test unless the response has the given status.

Jump to

Keyboard shortcuts

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