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 ¶
- type App
- func (a *App) Close()
- func (a *App) DELETE(path string) *Request
- func (a *App) GET(path string) *Request
- func (a *App) PATCH(path string, body any) *Request
- func (a *App) POST(path string, body any) *Request
- func (a *App) PUT(path string, body any) *Request
- func (a *App) URL(path string) string
- func (a *App) WithBearerToken(token string) *App
- func (a *App) WithHeader(key, value string) *App
- type FakeJob
- type FakeMail
- type FakeMessage
- type FakeQueue
- type FakeStorage
- type Request
- type Response
- func (r *Response) AssertContains(substr string) *Response
- func (r *Response) AssertCreated() *Response
- func (r *Response) AssertHeader(key, expected string) *Response
- func (r *Response) AssertJSON(key string, expected any) *Response
- func (r *Response) AssertNotFound() *Response
- func (r *Response) AssertOK() *Response
- func (r *Response) AssertStatus(expected int) *Response
- func (r *Response) AssertUnauthorized() *Response
- func (r *Response) Body() []byte
- func (r *Response) BodyString() string
- func (r *Response) Header(key string) string
- func (r *Response) JSON(v any) *Response
- func (r *Response) StatusCode() int
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 ¶
NewApp creates a test application with the given handler. If handler is nil, a minimal echo handler is used.
func (*App) WithBearerToken ¶
WithBearerToken sets the Authorization: Bearer header.
func (*App) WithHeader ¶
WithHeader sets a default header for all requests from this App.
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) 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.
type FakeMessage ¶
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.
type FakeStorage ¶
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.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is a fluent HTTP test request.
func (*Request) WithHeader ¶
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 ¶
AssertContains asserts the body contains the given substring.
func (*Response) AssertCreated ¶
AssertCreated asserts a 201 response.
func (*Response) AssertHeader ¶
AssertHeader asserts a response header equals the expected value.
func (*Response) AssertJSON ¶
AssertJSON checks that a JSON key equals the expected value.
resp.AssertJSON("message", "hello")
func (*Response) AssertNotFound ¶
AssertNotFound asserts a 404 response.
func (*Response) AssertStatus ¶
AssertStatus fails the test if status != expected.
func (*Response) AssertUnauthorized ¶
AssertUnauthorized asserts a 401 response.
func (*Response) BodyString ¶
BodyString returns the response body as a string.
func (*Response) StatusCode ¶
StatusCode returns the HTTP status code.