Skip to content

Commit

Permalink
add unit tests for CORS configuration and handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
abaldeweg authored Jan 16, 2025
1 parent 17ffd97 commit 6f8606f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions framework/cors/cors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cors

import (
"testing"

"github.com/gin-contrib/cors"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

// TestDefaultConfig tests the DefaultConfig function.
func TestDefaultConfig(t *testing.T) {
config := DefaultConfig()
assert.Contains(t, config.AllowHeaders, "Authorization")
assert.Equal(t, []string{"*"}, config.AllowOrigins)

viper.Set("CORS_ALLOW_ORIGIN", "http://localhost,http://test.localhost")
config = DefaultConfig()
assert.Equal(t, []string{"http://localhost", "http://test.localhost"}, config.AllowOrigins)
}

// TestSetDefaultCorsHeaders tests the SetDefaultCorsHeaders function.
func TestSetDefaultCorsHeaders(t *testing.T) {
handler := SetDefaultCorsHeaders()
assert.NotNil(t, handler)
}

// TestSetCorsHeaders tests the SetCorsHeaders function.
func TestSetCorsHeaders(t *testing.T) {
customConfig := cors.Config{
AllowOrigins: []string{"http://localhost"},
}
handler := SetCorsHeaders(customConfig)
assert.NotNil(t, handler)
}

0 comments on commit 6f8606f

Please sign in to comment.