testing

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testing provides test helpers for OniWorks applications. It spins up an httptest.Server backed by a real OniWorks router, offers a fluent request builder, and includes assertion helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

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

App is a test application wrapping httptest.Server.

func NewApp

func NewApp(t *testing.T, handler ...http.Handler) *App

NewApp creates a test application with the given handler. If handler is nil, a minimal echo handler is used.

func (*App) Close

func (a *App) Close()

Close shuts down the test server.

func (*App) DELETE

func (a *App) DELETE(path string) *Request

DELETE starts a DELETE request.

func (*App) GET

func (a *App) GET(path string) *Request

GET starts a GET request.

func (*App) PATCH

func (a *App) PATCH(path string, body any) *Request

PATCH starts a PATCH request with a JSON body.

func (*App) POST

func (a *App) POST(path string, body any) *Request

POST starts a POST request with a JSON body.

func (*App) PUT

func (a *App) PUT(path string, body any) *Request

PUT starts a PUT request with a JSON body.

func (*App) URL

func (a *App) URL(path string) string

URL returns the full URL for a path.

func (*App) WithBearerToken

func (a *App) WithBearerToken(token string) *App

WithBearerToken sets the Authorization: Bearer header.

func (*App) WithHeader

func (a *App) WithHeader(key, value string) *App

WithHeader sets a default header for all requests from this App.

type FakeJob

type FakeJob struct {
	Class   string
	Queue   string
	Payload any
	Delay   time.Duration
}

FakeJob records a dispatched job.

type FakeMail

type FakeMail struct {
	Sent []FakeMessage
	// contains filtered or unexported fields
}

FakeMail records outgoing emails for use in tests. Inject it in place of a real mail.Mailer to avoid actual SMTP.

func (*FakeMail) AssertNotSent

func (f *FakeMail) AssertNotSent(t *stdtesting.T, to string)

AssertNotSent fails the test if any email was sent to the given address.

func (*FakeMail) AssertSent

func (f *FakeMail) AssertSent(t *stdtesting.T, to string)

AssertSent fails the test if no email was sent to the given address.

func (*FakeMail) Count

func (f *FakeMail) Count() int

Count returns the number of emails sent.

func (*FakeMail) Last

func (f *FakeMail) Last() (FakeMessage, bool)

Last returns the most recently recorded message.

func (*FakeMail) Record

func (f *FakeMail) Record(msg FakeMessage)

Record adds a message to the sent list.

func (*FakeMail) Reset

func (f *FakeMail) Reset()

Reset clears all recorded messages.

type FakeMessage

type FakeMessage struct {
	To      []string
	Subject string
	HTML    string
	Text    string
	SentAt  time.Time
}

FakeMessage holds a recorded email.

type FakeQueue

type FakeQueue struct {
	Jobs []FakeJob
	// contains filtered or unexported fields
}

FakeQueue records dispatched jobs for use in tests.

func (*FakeQueue) AssertDispatched

func (fq *FakeQueue) AssertDispatched(t *stdtesting.T, class string)

AssertDispatched fails if no job of the given class was dispatched.

func (*FakeQueue) AssertNotDispatched

func (fq *FakeQueue) AssertNotDispatched(t *stdtesting.T, class string)

AssertNotDispatched fails if a job of the given class was dispatched.

func (*FakeQueue) Count

func (fq *FakeQueue) Count() int

Count returns the number of dispatched jobs.

func (*FakeQueue) Record

func (fq *FakeQueue) Record(job FakeJob)

Record adds a job to the fake queue.

func (*FakeQueue) Reset

func (fq *FakeQueue) Reset()

Reset clears all recorded jobs.

type FakeStorage

type FakeStorage struct {
	Objects map[string][]byte
	// contains filtered or unexported fields
}

FakeStorage is an in-memory storage.Disk for use in tests.

func NewFakeStorage

func NewFakeStorage() *FakeStorage

NewFakeStorage creates an empty FakeStorage.

func (*FakeStorage) AssertNotStored

func (fs *FakeStorage) AssertNotStored(t *stdtesting.T, path string)

AssertNotStored fails if path was stored.

func (*FakeStorage) AssertStored

func (fs *FakeStorage) AssertStored(t *stdtesting.T, path string)

AssertStored fails if path was not stored.

func (*FakeStorage) Get

func (fs *FakeStorage) Get(path string) ([]byte, bool)

Get returns the stored bytes for path.

func (*FakeStorage) Has

func (fs *FakeStorage) Has(path string) bool

Has reports whether the given path has been stored.

func (*FakeStorage) Reset

func (fs *FakeStorage) Reset()

Reset removes all stored objects.

type Request

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

Request is a fluent HTTP test request.

func (*Request) Send

func (r *Request) Send() *Response

Send executes the request and returns the response.

func (*Request) WithHeader

func (r *Request) WithHeader(key, value string) *Request

WithHeader adds a header to this request.

type Response

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

Response wraps an http.Response with assertion helpers.

func (*Response) AssertContains

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

AssertContains asserts the body contains the given substring.

func (*Response) AssertCreated

func (r *Response) AssertCreated() *Response

AssertCreated asserts a 201 response.

func (*Response) AssertHeader

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

AssertHeader asserts a response header equals the expected value.

func (*Response) AssertJSON

func (r *Response) AssertJSON(key string, expected any) *Response

AssertJSON checks that a JSON key equals the expected value.

resp.AssertJSON("message", "hello")

func (*Response) AssertNotFound

func (r *Response) AssertNotFound() *Response

AssertNotFound asserts a 404 response.

func (*Response) AssertOK

func (r *Response) AssertOK() *Response

AssertOK asserts a 200 response.

func (*Response) AssertStatus

func (r *Response) AssertStatus(expected int) *Response

AssertStatus fails the test if status != expected.

func (*Response) AssertUnauthorized

func (r *Response) AssertUnauthorized() *Response

AssertUnauthorized asserts a 401 response.

func (*Response) Body

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

Body returns the raw response body bytes.

func (*Response) BodyString

func (r *Response) BodyString() string

BodyString returns the response body as a string.

func (*Response) Header

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

Header returns a response header value.

func (*Response) JSON

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

JSON decodes the body into v.

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the HTTP status code.

Jump to

Keyboard shortcuts

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