humatest

package
v1.14.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package humatest provides testing utilities for testing Huma-powered services.

Example
package main

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/danielgtaylor/huma"
	"github.com/danielgtaylor/huma/humatest"
	"github.com/danielgtaylor/huma/responses"
	"github.com/stretchr/testify/assert"
)

func main() {
	// Normally you will have a T from your test runner.
	t := &testing.T{}

	// Create the test router. Logs will be hidden unless the test fails.
	r := humatest.NewRouter(t)

	// Set up routes & handlers.
	r.Resource("/test").Get("test", "Test get",
		responses.OK().ContentType("text/plain"),
	).Run(func(ctx huma.Context) {
		ctx.Write([]byte("Hello, test!"))
	})

	// Make a test request.
	w := httptest.NewRecorder()
	req, _ := http.NewRequest(http.MethodGet, "/test", nil)
	r.ServeHTTP(w, req)
	assert.Equal(t, http.StatusOK, w.Code)
	assert.Equal(t, "Hello, test!", w.Body.String())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRouter

func NewRouter(t testing.TB) *huma.Router

NewRouter creates a new test router. It includes a logger attached to the test so if it fails you will see additional output. There is no recovery middleware so panics will get caught by the test runner.

func NewRouterObserver

func NewRouterObserver(t testing.TB) (*huma.Router, *observer.ObservedLogs)

NewRouterObserver creates a new router and a log output observer for testing log output at "debug" level and above during requests.

Types

This section is empty.

Jump to

Keyboard shortcuts

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