govalintest

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Test

func Test(t *testing.T, app *govalin.App, fn func(client *Client))

Test starts the given app on an OS-assigned port, waits for it to be ready, runs the test function with a Client bound to it, and shuts the app down when the test finishes.

func TestWithOptions

func TestWithOptions(t *testing.T, app *govalin.App, opts Options, fn func(client *Client))

TestWithOptions is Test with explicit harness options.

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) Delete

func (client *Client) Delete(path string) string

Delete performs a DELETE request and returns the response body.

func (*Client) DeleteResponse

func (client *Client) DeleteResponse(path string) *http.Response

DeleteResponse performs a DELETE request and returns the full response.

func (*Client) DeleteStatus added in v0.2.12

func (client *Client) DeleteStatus(path string) int

DeleteStatus performs a DELETE request and returns the response status code, discarding the body.

func (*Client) Do

func (client *Client) Do(req *http.Request) *http.Response

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) Get

func (client *Client) Get(path string) string

Get performs a GET request and returns the response body.

func (*Client) GetResponse

func (client *Client) GetResponse(path string) *http.Response

GetResponse performs a GET request and returns the full response.

func (*Client) GetStatus added in v0.2.12

func (client *Client) GetStatus(path string) int

GetStatus performs a GET request and returns the response status code, discarding the body.

func (*Client) HTTP

func (client *Client) HTTP() *http.Client

HTTP returns the underlying http.Client for customization, e.g. setting a timeout or a cookie jar.

func (*Client) Head

func (client *Client) Head(path string) string

Head performs a HEAD request and returns the response body.

func (*Client) HeadResponse

func (client *Client) HeadResponse(path string) *http.Response

HeadResponse performs a HEAD request and returns the full response.

func (*Client) HeadStatus added in v0.2.12

func (client *Client) HeadStatus(path string) int

HeadStatus performs a HEAD request and returns the response status code, discarding the body.

func (*Client) Options

func (client *Client) Options(path string) string

Options performs an OPTIONS request and returns the response body.

func (*Client) OptionsResponse

func (client *Client) OptionsResponse(path string) *http.Response

OptionsResponse performs an OPTIONS request and returns the full response.

func (*Client) OptionsStatus added in v0.2.12

func (client *Client) OptionsStatus(path string) int

OptionsStatus performs an OPTIONS request and returns the response status code, discarding the body.

func (*Client) Patch

func (client *Client) Patch(path string, body any) string

Patch performs a PATCH request and returns the response body. See Post for body semantics.

func (*Client) PatchResponse

func (client *Client) PatchResponse(path string, body any) *http.Response

PatchResponse performs a PATCH request and returns the full response. See Post for body semantics.

func (*Client) PatchStatus added in v0.2.12

func (client *Client) PatchStatus(path string, body any) int

PatchStatus performs a PATCH request and returns the response status code, discarding the body. See Post for body semantics.

func (*Client) Post

func (client *Client) Post(path string, body any) string

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

func (client *Client) PostResponse(path string, body any) *http.Response

PostResponse performs a POST request and returns the full response. See Post for body semantics.

func (*Client) PostStatus added in v0.2.12

func (client *Client) PostStatus(path string, body any) int

PostStatus performs a POST request and returns the response status code, discarding the body. See Post for body semantics.

func (*Client) Put

func (client *Client) Put(path string, body any) string

Put performs a PUT request and returns the response body. See Post for body semantics.

func (*Client) PutResponse

func (client *Client) PutResponse(path string, body any) *http.Response

PutResponse performs a PUT request and returns the full response. See Post for body semantics.

func (*Client) PutStatus added in v0.2.12

func (client *Client) PutStatus(path string, body any) int

PutStatus performs a PUT request and returns the response status code, discarding the body. See Post for body semantics.

func (*Client) Websocket

func (client *Client) Websocket(path string) *websocket.Conn

Websocket connects a websocket to the given path on the app under test.

type Options

type Options struct {
	// StartupTimeout is the maximum time to wait for the app to start.
	// Zero means the default of 5 seconds.
	StartupTimeout time.Duration
}

Options configures the test harness. The zero value means sane defaults.

Jump to

Keyboard shortcuts

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