Skip to content

Commit 1ce3182

Browse files
authored
🧹 chore: Improve EarlyData middleware tests coverage (#3520)
* test: move config tests into earlydata_test * Fix lint issue
1 parent 670fbd5 commit 1ce3182

1 file changed

Lines changed: 73 additions & 4 deletions

File tree

middleware/earlydata/earlydata_test.go

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package earlydata_test
1+
package earlydata
22

33
import (
44
"errors"
55
"fmt"
66
"net/http/httptest"
7+
"reflect"
78
"testing"
89

910
"github.com/gofiber/fiber/v3"
10-
"github.com/gofiber/fiber/v3/middleware/earlydata"
1111
"github.com/stretchr/testify/require"
12+
"github.com/valyala/fasthttp"
1213
)
1314

1415
const (
@@ -28,12 +29,12 @@ func appWithConfig(t *testing.T, c *fiber.Config) *fiber.App {
2829
app = fiber.New(*c)
2930
}
3031

31-
app.Use(earlydata.New())
32+
app.Use(New())
3233

3334
// Middleware to test IsEarly func
3435
const localsKeyTestValid = "earlydata_testvalid"
3536
app.Use(func(c fiber.Ctx) error {
36-
isEarly := earlydata.IsEarly(c)
37+
isEarly := IsEarly(c)
3738

3839
switch h := c.Get(headerName); h {
3940
case "", headerValOff:
@@ -191,3 +192,71 @@ func Test_EarlyData(t *testing.T) {
191192
trustedRun(t, app)
192193
})
193194
}
195+
196+
// Test_EarlyDataNext verifies that the middleware skips its logic when Next returns true.
197+
func Test_EarlyDataNext(t *testing.T) {
198+
t.Parallel()
199+
app := fiber.New()
200+
201+
app.Use(New(Config{
202+
Next: func(fiber.Ctx) bool { return true },
203+
}))
204+
205+
called := false
206+
app.Get("/", func(c fiber.Ctx) error {
207+
called = true
208+
if IsEarly(c) {
209+
return errors.New("IsEarly(c) should be false when Next returns true")
210+
}
211+
return nil
212+
})
213+
214+
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
215+
req.Header.Set(headerName, headerValOn)
216+
resp, err := app.Test(req)
217+
require.NoError(t, err)
218+
require.Equal(t, fiber.StatusOK, resp.StatusCode)
219+
require.True(t, called)
220+
}
221+
222+
// Test_configDefault_NoConfig verifies that calling configDefault without
223+
// providing a configuration returns ConfigDefault as-is.
224+
func Test_configDefault_NoConfig(t *testing.T) {
225+
t.Parallel()
226+
cfg := configDefault()
227+
require.Equal(t, ConfigDefault.Error, cfg.Error)
228+
require.Equal(t, reflect.ValueOf(ConfigDefault.IsEarlyData).Pointer(), reflect.ValueOf(cfg.IsEarlyData).Pointer())
229+
require.Equal(t, reflect.ValueOf(ConfigDefault.AllowEarlyData).Pointer(), reflect.ValueOf(cfg.AllowEarlyData).Pointer())
230+
}
231+
232+
// Test_configDefault_WithConfig verifies that provided configuration fields are
233+
// kept while missing fields are populated with defaults.
234+
func Test_configDefault_WithConfig(t *testing.T) {
235+
t.Parallel()
236+
expectedErr := errors.New("boom")
237+
called := false
238+
custom := Config{
239+
Next: func(_ fiber.Ctx) bool { called = true; return false },
240+
Error: expectedErr,
241+
}
242+
243+
cfg := configDefault(custom)
244+
245+
// Next should be preserved and not invoked by configDefault.
246+
require.False(t, called)
247+
require.Equal(t, reflect.ValueOf(custom.Next).Pointer(), reflect.ValueOf(cfg.Next).Pointer())
248+
// Custom error must be preserved.
249+
require.Equal(t, expectedErr, cfg.Error)
250+
// Missing fields should be set to defaults.
251+
require.NotNil(t, cfg.IsEarlyData)
252+
require.NotNil(t, cfg.AllowEarlyData)
253+
254+
// Verify default functions behave as expected.
255+
app := fiber.New()
256+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
257+
c.Request().Header.Set(DefaultHeaderName, DefaultHeaderTrueValue)
258+
c.Request().Header.SetMethod(fiber.MethodGet)
259+
require.True(t, cfg.IsEarlyData(c))
260+
require.True(t, cfg.AllowEarlyData(c))
261+
app.ReleaseCtx(c)
262+
}

0 commit comments

Comments
 (0)