|
| 1 | +package fiber_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/gofiber/fiber/v3" |
| 13 | + "github.com/gofiber/fiber/v3/middleware/cors" |
| 14 | + "github.com/gofiber/fiber/v3/middleware/helmet" |
| 15 | + "github.com/gofiber/fiber/v3/middleware/requestid" |
| 16 | + "github.com/valyala/fasthttp" |
| 17 | + "github.com/valyala/fasthttp/fasthttputil" |
| 18 | +) |
| 19 | + |
| 20 | +type integrationCustomCtx struct { |
| 21 | + *fiber.DefaultCtx |
| 22 | +} |
| 23 | + |
| 24 | +func newIntegrationCustomCtx(app *fiber.App) fiber.CustomCtx { |
| 25 | + return &integrationCustomCtx{DefaultCtx: fiber.NewDefaultCtx(app)} |
| 26 | +} |
| 27 | + |
| 28 | +func performOversizedRequest(t *testing.T, app *fiber.App, configure func(req *fasthttp.Request)) *fasthttp.Response { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + ln := fasthttputil.NewInmemoryListener() |
| 32 | + errCh := make(chan error, 1) |
| 33 | + |
| 34 | + go func() { |
| 35 | + errCh <- app.Listener(ln, fiber.ListenConfig{DisableStartupMessage: true}) |
| 36 | + }() |
| 37 | + |
| 38 | + t.Cleanup(func() { |
| 39 | + require.NoError(t, app.Shutdown()) |
| 40 | + if err := <-errCh; err != nil && !errors.Is(err, net.ErrClosed) { |
| 41 | + require.NoError(t, err) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + require.Eventually(t, func() bool { |
| 46 | + conn, err := ln.Dial() |
| 47 | + if err != nil { |
| 48 | + return false |
| 49 | + } |
| 50 | + if err := conn.Close(); err != nil { |
| 51 | + return false |
| 52 | + } |
| 53 | + return true |
| 54 | + }, time.Second, 10*time.Millisecond) |
| 55 | + |
| 56 | + req := fasthttp.AcquireRequest() |
| 57 | + resp := fasthttp.AcquireResponse() |
| 58 | + |
| 59 | + req.SetRequestURI("http://example.com/") |
| 60 | + req.Header.SetMethod(fiber.MethodPost) |
| 61 | + req.Header.Set(fiber.HeaderOrigin, "https://example.com") |
| 62 | + req.SetBody(bytes.Repeat([]byte{'a'}, 32)) |
| 63 | + if configure != nil { |
| 64 | + configure(req) |
| 65 | + } |
| 66 | + |
| 67 | + client := fasthttp.Client{ |
| 68 | + Dial: func(string) (net.Conn, error) { |
| 69 | + return ln.Dial() |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + require.NoError(t, client.Do(req, resp)) |
| 74 | + |
| 75 | + respCopy := fasthttp.AcquireResponse() |
| 76 | + resp.CopyTo(respCopy) |
| 77 | + |
| 78 | + fasthttp.ReleaseRequest(req) |
| 79 | + fasthttp.ReleaseResponse(resp) |
| 80 | + |
| 81 | + t.Cleanup(func() { |
| 82 | + fasthttp.ReleaseResponse(respCopy) |
| 83 | + }) |
| 84 | + |
| 85 | + return respCopy |
| 86 | +} |
| 87 | + |
| 88 | +func Test_Integration_App_ServerErrorHandler_PreservesCORSHeadersOnBodyLimit(t *testing.T) { |
| 89 | + app := fiber.New(fiber.Config{BodyLimit: 16}) |
| 90 | + app.Use(cors.New(cors.Config{ |
| 91 | + AllowOrigins: []string{"https://example.com"}, |
| 92 | + AllowCredentials: true, |
| 93 | + ExposeHeaders: []string{"X-Request-ID"}, |
| 94 | + })) |
| 95 | + app.Post("/", func(c fiber.Ctx) error { |
| 96 | + return c.SendStatus(fiber.StatusOK) |
| 97 | + }) |
| 98 | + |
| 99 | + resp := performOversizedRequest(t, app, nil) |
| 100 | + |
| 101 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 102 | + require.Equal(t, "https://example.com", string(resp.Header.Peek(fiber.HeaderAccessControlAllowOrigin))) |
| 103 | + require.Equal(t, "true", string(resp.Header.Peek(fiber.HeaderAccessControlAllowCredentials))) |
| 104 | + require.Equal(t, "X-Request-ID", string(resp.Header.Peek(fiber.HeaderAccessControlExposeHeaders))) |
| 105 | + require.Equal(t, "Origin", string(resp.Header.Peek(fiber.HeaderVary))) |
| 106 | +} |
| 107 | + |
| 108 | +func Test_Integration_App_ServerErrorHandler_PreservesHelmetHeadersOnBodyLimit(t *testing.T) { |
| 109 | + app := fiber.New(fiber.Config{BodyLimit: 16}) |
| 110 | + app.Use(helmet.New()) |
| 111 | + app.Post("/", func(c fiber.Ctx) error { |
| 112 | + return c.SendStatus(fiber.StatusOK) |
| 113 | + }) |
| 114 | + |
| 115 | + resp := performOversizedRequest(t, app, nil) |
| 116 | + |
| 117 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 118 | + require.Equal(t, "nosniff", string(resp.Header.Peek(fiber.HeaderXContentTypeOptions))) |
| 119 | + require.Equal(t, "same-origin", string(resp.Header.Peek("Cross-Origin-Opener-Policy"))) |
| 120 | + require.Equal(t, "same-origin", string(resp.Header.Peek("Cross-Origin-Resource-Policy"))) |
| 121 | + require.Equal(t, "require-corp", string(resp.Header.Peek("Cross-Origin-Embedder-Policy"))) |
| 122 | +} |
| 123 | + |
| 124 | +func Test_Integration_App_ServerErrorHandler_PreservesRequestID(t *testing.T) { |
| 125 | + const expectedRequestID = "integration-request-id" |
| 126 | + |
| 127 | + app := fiber.New(fiber.Config{BodyLimit: 16}) |
| 128 | + app.Use(requestid.New()) |
| 129 | + app.Post("/", func(c fiber.Ctx) error { |
| 130 | + return c.SendStatus(fiber.StatusOK) |
| 131 | + }) |
| 132 | + |
| 133 | + resp := performOversizedRequest(t, app, func(req *fasthttp.Request) { |
| 134 | + req.Header.Set("X-Request-ID", expectedRequestID) |
| 135 | + }) |
| 136 | + |
| 137 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 138 | + require.Equal(t, expectedRequestID, string(resp.Header.Peek("X-Request-ID"))) |
| 139 | +} |
| 140 | + |
| 141 | +func Test_Integration_App_ServerErrorHandler_GroupMiddlewareChain(t *testing.T) { |
| 142 | + app := fiber.New(fiber.Config{BodyLimit: 16}) |
| 143 | + app.Use(helmet.New()) |
| 144 | + |
| 145 | + api := app.Group("/api") |
| 146 | + api.Use(requestid.New()) |
| 147 | + api.Use(func(c fiber.Ctx) error { |
| 148 | + c.Set("X-Group-Middleware", "active") |
| 149 | + return c.Next() |
| 150 | + }) |
| 151 | + api.Post("/resource", func(c fiber.Ctx) error { |
| 152 | + return c.SendStatus(fiber.StatusOK) |
| 153 | + }) |
| 154 | + |
| 155 | + resp := performOversizedRequest(t, app, func(req *fasthttp.Request) { |
| 156 | + req.SetRequestURI("http://example.com/api/resource") |
| 157 | + }) |
| 158 | + |
| 159 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 160 | + require.Equal(t, "nosniff", string(resp.Header.Peek(fiber.HeaderXContentTypeOptions))) |
| 161 | + require.NotEmpty(t, resp.Header.Peek("X-Request-ID")) |
| 162 | + require.Equal(t, "active", string(resp.Header.Peek("X-Group-Middleware"))) |
| 163 | +} |
| 164 | + |
| 165 | +func Test_Integration_App_ServerErrorHandler_RetainsHeadersFromSubsequentMiddleware(t *testing.T) { |
| 166 | + app := fiber.New(fiber.Config{BodyLimit: 8}) |
| 167 | + app.Use(func(c fiber.Ctx) error { |
| 168 | + c.Set("X-Custom-Middleware", "ran") |
| 169 | + return c.Next() |
| 170 | + }) |
| 171 | + app.Use(cors.New()) |
| 172 | + app.Post("/", func(c fiber.Ctx) error { |
| 173 | + return c.SendStatus(fiber.StatusOK) |
| 174 | + }) |
| 175 | + |
| 176 | + resp := performOversizedRequest(t, app, nil) |
| 177 | + |
| 178 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 179 | + require.Equal(t, "ran", string(resp.Header.Peek("X-Custom-Middleware"))) |
| 180 | + require.Equal(t, "*", string(resp.Header.Peek(fiber.HeaderAccessControlAllowOrigin))) |
| 181 | +} |
| 182 | + |
| 183 | +func Test_Integration_App_ServerErrorHandler_WithCustomCtx(t *testing.T) { |
| 184 | + app := fiber.NewWithCustomCtx(newIntegrationCustomCtx, fiber.Config{BodyLimit: 16}) |
| 185 | + app.Use(func(c fiber.Ctx) error { |
| 186 | + customCtx, ok := c.(*integrationCustomCtx) |
| 187 | + require.True(t, ok) |
| 188 | + customCtx.Set("X-Custom-Ctx", "true") |
| 189 | + return c.Next() |
| 190 | + }) |
| 191 | + app.Use(cors.New(cors.Config{AllowOrigins: []string{"https://example.org"}})) |
| 192 | + app.Post("/", func(c fiber.Ctx) error { |
| 193 | + return c.SendStatus(fiber.StatusOK) |
| 194 | + }) |
| 195 | + |
| 196 | + resp := performOversizedRequest(t, app, func(req *fasthttp.Request) { |
| 197 | + req.Header.Set(fiber.HeaderOrigin, "https://example.org") |
| 198 | + }) |
| 199 | + |
| 200 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 201 | + require.Equal(t, "true", string(resp.Header.Peek("X-Custom-Ctx"))) |
| 202 | + require.Equal(t, "https://example.org", string(resp.Header.Peek(fiber.HeaderAccessControlAllowOrigin))) |
| 203 | +} |
0 commit comments