Skip to content

Commit

Permalink
test: improve CORS wildcard handling and testing (#144)
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
appleboy authored Mar 6, 2024
1 parent fcbd06f commit 85bf9fb
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}

0 comments on commit 85bf9fb

Please sign in to comment.