humatest

package
v2.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package humatest provides testing utilities for Huma services. It is based on the standard library `http.Request` & `http.ResponseWriter` types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpRequest added in v2.13.0

func DumpRequest(req *http.Request) ([]byte, error)

DumpRequest returns a string representation of an HTTP request, automatically pretty printing JSON bodies for readability.

func DumpResponse added in v2.13.0

func DumpResponse(resp *http.Response) ([]byte, error)

DumpResponse returns a string representation of an HTTP response, automatically pretty printing JSON bodies for readability.

func NewAdapter

func NewAdapter() huma.Adapter

NewAdapter creates a new test adapter from a router.

func NewContext

func NewContext(op *huma.Operation, r *http.Request, w http.ResponseWriter) huma.Context

NewContext creates a new test context from an HTTP request and response.

func PrintRequest added in v2.13.0

func PrintRequest(req *http.Request)

PrintRequest prints a string representation of an HTTP request to stdout, automatically pretty printing JSON bodies for readability.

Example
req, _ := http.NewRequest(http.MethodGet, "http://example.com/foo?bar=baz", nil)
req.Header.Set("Foo", "bar")
req.Host = "example.com"
PrintRequest(req)
Output:

GET /foo?bar=baz HTTP/1.1
Host: example.com
Foo: bar

func PrintResponse added in v2.13.0

func PrintResponse(resp *http.Response)

PrintResponse prints a string representation of an HTTP response to stdout, automatically pretty printing JSON bodies for readability.

Example
resp := &http.Response{
	StatusCode: http.StatusOK,
	ProtoMajor: 1,
	ProtoMinor: 1,
	Header: http.Header{
		"Content-Type": []string{"application/json"},
	},
	ContentLength: -1,
	Body:          io.NopCloser(strings.NewReader(`{"foo": "bar"}`)),
}
PrintResponse(resp)
Output:

HTTP/1.1 200 OK
Connection: close
Content-Type: application/json

{
  "foo": "bar"
}

Types

type TB

type TB interface {
	Helper()
	Log(args ...any)
	Logf(format string, args ...any)
}

TB is a subset of the `testing.TB` interface used by the test API and implemented by the `*testing.T` and `*testing.B` structs.

type TestAPI

type TestAPI interface {
	huma.API

	// Do a request against the API. Args, if provided, should be string headers
	// like `Content-Type: application/json`, an `io.Reader` for the request
	// body, or a slice/map/struct which will be serialized to JSON and sent
	// as the request body. Anything else will panic.
	Do(method, path string, args ...any) *httptest.ResponseRecorder

	// Get performs a GET request against the API. Args, if provided, should be
	// string headers like `Content-Type: application/json`, an `io.Reader`
	// for the request body, or a slice/map/struct which will be serialized to
	// JSON and sent as the request body. Anything else will panic.
	//
	// 	// Make a GET request
	// 	api.Get("/foo")
	//
	// 	// Make a GET request with a custom header.
	// 	api.Get("/foo", "X-My-Header: my-value")
	Get(path string, args ...any) *httptest.ResponseRecorder

	// Post performs a POST request against the API. Args, if provided, should be
	// string headers like `Content-Type: application/json`, an `io.Reader`
	// for the request body, or a slice/map/struct which will be serialized to
	// JSON and sent as the request body. Anything else will panic.
	//
	// 	// Make a POST request
	// 	api.Post("/foo", bytes.NewReader(`{"foo": "bar"}`))
	//
	// 	// Make a POST request with a custom header.
	// 	api.Post("/foo", "X-My-Header: my-value", MyBody{Foo: "bar"})
	Post(path string, args ...any) *httptest.ResponseRecorder

	// Put performs a PUT request against the API. Args, if provided, should be
	// string headers like `Content-Type: application/json`, an `io.Reader`
	// for the request body, or a slice/map/struct which will be serialized to
	// JSON and sent as the request body. Anything else will panic.
	//
	// 	// Make a PUT request
	// 	api.Put("/foo", bytes.NewReader(`{"foo": "bar"}`))
	//
	// 	// Make a PUT request with a custom header.
	// 	api.Put("/foo", "X-My-Header: my-value", MyBody{Foo: "bar"})
	Put(path string, args ...any) *httptest.ResponseRecorder

	// Patch performs a PATCH request against the API. Args, if provided, should
	// be string headers like `Content-Type: application/json`, an `io.Reader`
	// for the request body, or a slice/map/struct which will be serialized to
	// JSON and sent as the request body. Anything else will panic.
	//
	// 	// Make a PATCH request
	// 	api.Patch("/foo", bytes.NewReader(`{"foo": "bar"}`))
	//
	// 	// Make a PATCH request with a custom header.
	// 	api.Patch("/foo", "X-My-Header: my-value", MyBody{Foo: "bar"})
	Patch(path string, args ...any) *httptest.ResponseRecorder

	// Delete performs a DELETE request against the API. Args, if provided, should
	// be string headers like `Content-Type: application/json`, an `io.Reader`
	// for the request body, or a slice/map/struct which will be serialized to
	// JSON and sent as the request body. Anything else will panic.
	//
	// 	// Make a DELETE request
	// 	api.Delete("/foo")
	//
	// 	// Make a DELETE request with a custom header.
	// 	api.Delete("/foo", "X-My-Header: my-value")
	Delete(path string, args ...any) *httptest.ResponseRecorder
}

TestAPI is a `huma.API` with additional methods specifically for testing.

func New

func New(tb TB, configs ...huma.Config) (http.Handler, TestAPI)

New creates a new router and test API, making it easy to register operations and perform requests against them. Optionally takes a configuration object to customize how the API is created. If no configuration is provided then a simple default configuration supporting `application/json` is used.

func Wrap

func Wrap(tb TB, api huma.API) TestAPI

Wrap returns a `TestAPI` wrapping the given API.

Jump to

Keyboard shortcuts

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