|
1 | 1 | package humafiber |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "compress/gzip" |
4 | 6 | "context" |
| 7 | + "io" |
5 | 8 | "net/http" |
6 | 9 | "testing" |
7 | 10 |
|
8 | 11 | "github.com/danielgtaylor/huma/v2" |
9 | 12 | fiberV2 "github.com/gofiber/fiber/v2" |
| 13 | + "github.com/stretchr/testify/require" |
10 | 14 | ) |
11 | 15 |
|
| 16 | +// TestFiberV2EachHeaderAndCookie ensures EachHeader yields one callback per |
| 17 | +// header value (not one per byte) so header-based helpers like huma.ReadCookie |
| 18 | +// continue to work under the Fiber v2 adapter. Regression test for #1055. |
| 19 | +func TestFiberV2EachHeaderAndCookie(t *testing.T) { |
| 20 | + r := fiberV2.New() |
| 21 | + api := NewV2(r, huma.DefaultConfig("Test API", "1.0.0")) |
| 22 | + |
| 23 | + var authValue string |
| 24 | + var authCallbacks int |
| 25 | + var cookieValue string |
| 26 | + var cookieErr error |
| 27 | + api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) { |
| 28 | + ctx.EachHeader(func(name, value string) { |
| 29 | + if name == "Authorization" { |
| 30 | + authCallbacks++ |
| 31 | + authValue = value |
| 32 | + } |
| 33 | + }) |
| 34 | + cookie, err := huma.ReadCookie(ctx, "session") |
| 35 | + cookieErr = err |
| 36 | + if cookie != nil { |
| 37 | + cookieValue = cookie.Value |
| 38 | + } |
| 39 | + next(ctx) |
| 40 | + }) |
| 41 | + |
| 42 | + huma.Register(api, huma.Operation{ |
| 43 | + OperationID: "get-root", |
| 44 | + Method: http.MethodGet, |
| 45 | + Path: "/", |
| 46 | + }, func(ctx context.Context, _ *struct{}) (*struct{}, error) { |
| 47 | + return &struct{}{}, nil |
| 48 | + }) |
| 49 | + |
| 50 | + req, _ := http.NewRequest(http.MethodGet, "http://example.com/", nil) |
| 51 | + req.Header.Set("Authorization", "Bearer abc") |
| 52 | + req.Header.Set("Cookie", "session=xyz") |
| 53 | + _, err := r.Test(req) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + require.Equal(t, 1, authCallbacks, "EachHeader should fire once per header value, not once per byte") |
| 57 | + require.Equal(t, "Bearer abc", authValue) |
| 58 | + require.NoError(t, cookieErr) |
| 59 | + require.Equal(t, "xyz", cookieValue) |
| 60 | +} |
| 61 | + |
| 62 | +// TestFiberV2BodyReaderDecompresses ensures BodyReader returns the decompressed |
| 63 | +// request body (Body) rather than the raw compressed bytes (BodyRaw), so |
| 64 | +// handlers receive usable payloads when Content-Encoding is set. Regression |
| 65 | +// test for #1055. |
| 66 | +func TestFiberV2BodyReaderDecompresses(t *testing.T) { |
| 67 | + r := fiberV2.New() |
| 68 | + api := NewV2(r, huma.DefaultConfig("Test API", "1.0.0")) |
| 69 | + |
| 70 | + type Body struct { |
| 71 | + Name string `json:"name"` |
| 72 | + } |
| 73 | + huma.Register(api, huma.Operation{ |
| 74 | + OperationID: "echo", |
| 75 | + Method: http.MethodPost, |
| 76 | + Path: "/echo", |
| 77 | + }, func(ctx context.Context, in *struct{ Body Body }) (*struct{ Body Body }, error) { |
| 78 | + return &struct{ Body Body }{Body: in.Body}, nil |
| 79 | + }) |
| 80 | + |
| 81 | + var buf bytes.Buffer |
| 82 | + gz := gzip.NewWriter(&buf) |
| 83 | + _, err := gz.Write([]byte(`{"name":"gzipped"}`)) |
| 84 | + require.NoError(t, err) |
| 85 | + require.NoError(t, gz.Close()) |
| 86 | + |
| 87 | + req, _ := http.NewRequest(http.MethodPost, "http://example.com/echo", &buf) |
| 88 | + req.Header.Set("Content-Type", "application/json") |
| 89 | + req.Header.Set("Content-Encoding", "gzip") |
| 90 | + resp, err := r.Test(req) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + body, err := io.ReadAll(resp.Body) |
| 94 | + require.NoError(t, err) |
| 95 | + require.Equal(t, http.StatusOK, resp.StatusCode, "compressed body should decompress and parse: %s", body) |
| 96 | + require.Contains(t, string(body), `"name":"gzipped"`) |
| 97 | +} |
| 98 | + |
12 | 99 | func BenchmarkHumaFiberV2(b *testing.B) { |
13 | 100 | type GreetingInput struct { |
14 | 101 | ID string `path:"id"` |
|
0 commit comments