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())
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewRouter ¶
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 ¶
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.
Click to show internal directories.
Click to hide internal directories.