Skip to content

Commit aea0fd2

Browse files
authored
🧹 chore: Improve CORS tests coverage (#3530)
* test(cors): warn when all origins and func * Fix linter issue
1 parent 3b2af61 commit aea0fd2

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

middleware/cors/cors_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package cors
22

33
import (
4+
"bytes"
45
"net/http/httptest"
6+
"os"
57
"strings"
68
"testing"
79

810
"github.com/gofiber/fiber/v3"
11+
"github.com/gofiber/fiber/v3/log"
912
"github.com/stretchr/testify/require"
1013
"github.com/valyala/fasthttp"
1114
)
@@ -261,6 +264,21 @@ func Test_CORS_Wildcard_AllowCredentials_Panic(t *testing.T) {
261264
}
262265
}
263266

267+
// Test that a warning is logged when AllowOrigins allows all origins and
268+
// AllowOriginsFunc is also provided.
269+
func Test_CORS_Warn_AllowAllOrigins_WithFunc(t *testing.T) {
270+
var buf bytes.Buffer
271+
log.SetOutput(&buf)
272+
t.Cleanup(func() { log.SetOutput(os.Stderr) })
273+
274+
fiber.New().Use(New(Config{
275+
AllowOrigins: []string{"*"},
276+
AllowOriginsFunc: func(string) bool { return true },
277+
}))
278+
279+
require.Contains(t, buf.String(), "AllowOriginsFunc' will not be used")
280+
}
281+
264282
// go test -run -v Test_CORS_Invalid_Origin_Panic
265283
func Test_CORS_Invalid_Origins_Panic(t *testing.T) {
266284
t.Parallel()

0 commit comments

Comments
 (0)