humatest

package
v2.0.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 12 Imported by: 1

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 adapter for the given chi router.

func NewContext

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

NewContext creates a new test context from a request/response pair.

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` or an `io.Reader` for 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` or an `io.Reader`
	// for the request body. Anything else will panic.
	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` or an `io.Reader`
	// for the request body. Anything else will panic.
	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` or an `io.Reader`
	// for the request body. Anything else will panic.
	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` or an `io.Reader`
	// for the request body. Anything else will panic.
	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` or an `io.Reader`
	// for the request body. Anything else will panic.
	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