Skip to content

Commit

Permalink
Access-Control-Allow-Private-Network in CORS middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-Khokher committed Mar 9, 2024
1 parent 33194c8 commit e9e9c97
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions middleware/cors/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,43 @@ func Test_CORS_AllowCredentials(t *testing.T) {
}
}

// The Enhancement for issue #2804
func Test_CORS_AllowPrivateNetworkAccess(t *testing.T) {
t.Parallel()

// Test scenario where AllowPrivateNetworkAccess is enabled
app := fiber.New()
app.Use(New(Config{
AllowPrivateNetworkAccess: true,
}))

handler := app.Handler()

ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(fiber.MethodOptions)
ctx.Request.Header.Set(fiber.HeaderOrigin, "https://example.com")
ctx.Request.Header.Set("Access-Control-Request-Private-Network", "true")
handler(ctx)

// Verify the Access-Control-Allow-Private-Network header is set to "true"
require.Equal(t, "true", string(ctx.Response.Header.Peek("Access-Control-Allow-Private-Network")), "The Access-Control-Allow-Private-Network header should be set to 'true' when AllowPrivateNetworkAccess is enabled")

// Reset ctx for next test
ctx = &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(fiber.MethodOptions)
ctx.Request.Header.Set(fiber.HeaderOrigin, "https://example.com")

Check failure on line 717 in middleware/cors/cors_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
// Test scenario where AllowPrivateNetworkAccess is disabled (default)
app = fiber.New()
app.Use(New())

handler = app.Handler()
handler(ctx)

// Verify the Access-Control-Allow-Private-Network header is not present
require.Equal(t, "", string(ctx.Response.Header.Peek("Access-Control-Allow-Private-Network")), "The Access-Control-Allow-Private-Network header should not be present by default")
}

// go test -v -run=^$ -bench=Benchmark_CORS_NewHandler -benchmem -count=4
func Benchmark_CORS_NewHandler(b *testing.B) {
app := fiber.New()
Expand Down

0 comments on commit e9e9c97

Please sign in to comment.