(t *testing.T)
| 117 | } |
| 118 | |
| 119 | func 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{ |
nothing calls this directly
no test coverage detected
searching dependent graphs…