humatest

package
v2.0.0-...-21a9387 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAdapter

func NewAdapter(r chi.Router) huma.Adapter

NewAdapter creates a new test adapter from a chi 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.

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) (chi.Router, 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 NewTestAPI

func NewTestAPI(tb TB, r chi.Router, config huma.Config) TestAPI

NewTestAPI creates a new test API from a chi router and API config.

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