MCPcopy
hub / github.com/danielgtaylor/huma / TestFeatures

Function TestFeatures

huma_test.go:119–3188  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

117}
118
119func TestFeatures(t *testing.T) {
120 for _, feature := range []struct {
121 Name string
122 Transformers []huma.Transformer
123 Config huma.Config
124 Register func(t *testing.T, api huma.API)
125 Method string
126 URL string
127 Headers map[string]string
128 Body string
129 Assert func(t *testing.T, resp *httptest.ResponseRecorder)
130 }{
131 {
132 Name: "middleware",
133 Register: func(t *testing.T, api huma.API) {
134 api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
135 // Just a do-nothing passthrough. Shows that chaining works.
136 next(ctx)
137 })
138 api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
139 // Return an error response, never calling the next handler.
140 ctx.SetStatus(299)
141 })
142 huma.Register(api, huma.Operation{
143 Method: http.MethodGet,
144 Path: "/middleware",
145 }, func(ctx context.Context, input *struct{}) (*struct{}, error) {
146 // This should never be called because of the middleware.
147 return nil, nil
148 })
149 },
150 Method: http.MethodGet,
151 URL: "/middleware",
152 Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
153 // We should get the error response from the middleware.
154 assert.Equal(t, 299, resp.Code)
155 },
156 },
157 {
158 Name: "middleware-cookie",
159 Register: func(t *testing.T, api huma.API) {
160 api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
161 cookie, err := huma.ReadCookie(ctx, "foo")
162 require.NoError(t, err)
163 assert.Equal(t, "bar", cookie.Value)
164
165 next(ctx)
166 })
167 huma.Register(api, huma.Operation{
168 Method: http.MethodGet,
169 Path: "/middleware",
170 }, func(ctx context.Context, input *struct{}) (*struct{}, error) {
171 return nil, nil
172 })
173 },
174 Method: http.MethodGet,
175 URL: "/middleware",
176 Headers: map[string]string{

Callers

nothing calls this directly

Calls 15

WrapFunction · 0.92
NewFunction · 0.92
CustomStringParamTypeAlias · 0.85
PtrFunction · 0.85
RecovererFunction · 0.85
BytesMethod · 0.80
LenMethod · 0.80
YAMLMethod · 0.80
LogMethod · 0.80
UseMiddlewareMethod · 0.65
SetStatusMethod · 0.65
OpenAPIMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…