Documentation
¶
Overview ¶
Package govalintest provides a test harness for testing govalin applications over real HTTP.
The harness takes your already constructed App — built by your own application constructor, with your config, plugins and routes — starts it on an OS-assigned port, waits for it to be ready, and hands your test a Client bound to it. The app is shut down automatically via t.Cleanup.
func TestMyAPI(t *testing.T) {
govalintest.Test(t, myapp.New(), func(client *govalintest.Client) {
if body := client.Get("/health"); body != "ok" {
t.Errorf("expected ok, got %q", body)
}
})
}
Failures in the harness or client fail the calling test; they never abort the test process. Client methods must be called from the test goroutine.
Index ¶
- func Test(t *testing.T, app *govalin.App, fn func(client *Client))
- func TestWithOptions(t *testing.T, app *govalin.App, opts Options, fn func(client *Client))
- type Client
- func (client *Client) Delete(path string) string
- func (client *Client) DeleteResponse(path string) *http.Response
- func (client *Client) DeleteStatus(path string) int
- func (client *Client) Do(req *http.Request) *http.Response
- func (client *Client) Get(path string) string
- func (client *Client) GetResponse(path string) *http.Response
- func (client *Client) GetStatus(path string) int
- func (client *Client) HTTP() *http.Client
- func (client *Client) Head(path string) string
- func (client *Client) HeadResponse(path string) *http.Response
- func (client *Client) HeadStatus(path string) int
- func (client *Client) Options(path string) string
- func (client *Client) OptionsResponse(path string) *http.Response
- func (client *Client) OptionsStatus(path string) int
- func (client *Client) Patch(path string, body any) string
- func (client *Client) PatchResponse(path string, body any) *http.Response
- func (client *Client) PatchStatus(path string, body any) int
- func (client *Client) Post(path string, body any) string
- func (client *Client) PostResponse(path string, body any) *http.Response
- func (client *Client) PostStatus(path string, body any) int
- func (client *Client) Put(path string, body any) string
- func (client *Client) PutResponse(path string, body any) *http.Response
- func (client *Client) PutStatus(path string, body any) int
- func (client *Client) Websocket(path string) *websocket.Conn
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// Host is the base URL of the app under test, e.g. "http://localhost:54321".
Host string
// contains filtered or unexported fields
}
Client makes HTTP requests against the app under test and fails the test on transport errors.
The body-returning verbs (Get, Post, ...) return the response body regardless of status code, so tests can assert on error handler responses; the *Status variants return just the status code (closing the body for you); the *Response variants return the full response for asserting on status codes and headers together. The caller is responsible for closing the body of a returned *http.Response.
func (*Client) DeleteResponse ¶
DeleteResponse performs a DELETE request and returns the full response.
func (*Client) DeleteStatus ¶ added in v0.2.12
DeleteStatus performs a DELETE request and returns the response status code, discarding the body.
func (*Client) Do ¶
Do sends a custom request and returns the response, failing the test on transport errors. A request with a relative URL (no scheme/host) is resolved against the app under test, so requests built with http.NewRequest("GET", "/path", nil) work directly.
func (*Client) GetResponse ¶
GetResponse performs a GET request and returns the full response.
func (*Client) GetStatus ¶ added in v0.2.12
GetStatus performs a GET request and returns the response status code, discarding the body.
func (*Client) HTTP ¶
HTTP returns the underlying http.Client for customization, e.g. setting a timeout or a cookie jar.
func (*Client) HeadResponse ¶
HeadResponse performs a HEAD request and returns the full response.
func (*Client) HeadStatus ¶ added in v0.2.12
HeadStatus performs a HEAD request and returns the response status code, discarding the body.
func (*Client) OptionsResponse ¶
OptionsResponse performs an OPTIONS request and returns the full response.
func (*Client) OptionsStatus ¶ added in v0.2.12
OptionsStatus performs an OPTIONS request and returns the response status code, discarding the body.
func (*Client) Patch ¶
Patch performs a PATCH request and returns the response body. See Post for body semantics.
func (*Client) PatchResponse ¶
PatchResponse performs a PATCH request and returns the full response. See Post for body semantics.
func (*Client) PatchStatus ¶ added in v0.2.12
PatchStatus performs a PATCH request and returns the response status code, discarding the body. See Post for body semantics.
func (*Client) Post ¶
Post performs a POST request and returns the response body.
A string, []byte or io.Reader body is sent as-is; any other value is JSON-encoded and sent with Content-Type: application/json.
func (*Client) PostResponse ¶
PostResponse performs a POST request and returns the full response. See Post for body semantics.
func (*Client) PostStatus ¶ added in v0.2.12
PostStatus performs a POST request and returns the response status code, discarding the body. See Post for body semantics.
func (*Client) Put ¶
Put performs a PUT request and returns the response body. See Post for body semantics.
func (*Client) PutResponse ¶
PutResponse performs a PUT request and returns the full response. See Post for body semantics.