From 85bf9fb45757fab3a0f5a7237ec2cad06604a2f1 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 6 Mar 2024 09:33:13 +0800 Subject: [PATCH] test: improve CORS wildcard handling and testing (#144) - Add two new test functions in `cors_test.go` for parsing wildcard rules: one without wildcards and one with invalid wildcards - Ensure no wildcard rules are returned when `AllowWildcard` is set to false - Verify that parsing invalid wildcard rules triggers a panic Signed-off-by: Bo-Yi Wu --- cors_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cors_test.go b/cors_test.go index c87d60a..ea14a41 100644 --- a/cors_test.go +++ b/cors_test.go @@ -414,3 +414,35 @@ func TestWildcard(t *testing.T) { w = performRequest(router, "GET", "https://github.com") assert.Equal(t, 200, w.Code) } + +func TestParseWildcardRules_NoWildcard(t *testing.T) { + config := Config{ + AllowOrigins: []string{ + "http://example.com", + "https://google.com", + "github.com", + }, + AllowWildcard: false, + } + + var expected [][]string + + result := config.parseWildcardRules() + + assert.Equal(t, expected, result) +} + +func TestParseWildcardRules_InvalidWildcard(t *testing.T) { + config := Config{ + AllowOrigins: []string{ + "http://example.com", + "https://*.google.com*", + "*.github.com*", + }, + AllowWildcard: true, + } + + assert.Panics(t, func() { + config.parseWildcardRules() + }) +}