Skip to content

Commit

Permalink
test(middleware/cors): cover additiona test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sixcolors committed Mar 19, 2024
1 parent 08fb3f3 commit eaa110c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
10 changes: 10 additions & 0 deletions middleware/cors/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ func Test_CORS_AllowOriginScheme(t *testing.T) {
reqOrigin: "http://example.com",
shouldAllowOrigin: true,
},
{
pattern: "HTTP://EXAMPLE.COM",
reqOrigin: "http://example.com",
shouldAllowOrigin: true,
},
{
pattern: "https://example.com",
reqOrigin: "https://example.com",
Expand Down Expand Up @@ -310,6 +315,11 @@ func Test_CORS_AllowOriginScheme(t *testing.T) {
reqOrigin: "http://aaa.example.com:8080",
shouldAllowOrigin: true,
},
{
pattern: "http://*.example.com",
reqOrigin: "http://1.2.aaa.example.com",
shouldAllowOrigin: true,
},
{
pattern: "http://example.com",
reqOrigin: "http://gofiber.com",
Expand Down
65 changes: 42 additions & 23 deletions middleware/cors/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,40 +111,76 @@ func Test_normalizeDomain(t *testing.T) {
}
}

func TestSubdomainMatch(t *testing.T) {
// go test -v -run=^$ -bench=Benchmark_CORS_SubdomainMatch -benchmem -count=4
func Benchmark_CORS_SubdomainMatch(b *testing.B) {
s := subdomain{
prefix: "www",
suffix: ".example.com",
}

o := "www.example.com"

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
s.match(o)
}
}

func Test_CORS_SubdomainMatch(t *testing.T) {
tests := []struct {
name string
sub subdomain
origin string
expected bool
}{
{
name: "match with different scheme",
sub: subdomain{prefix: "http://api.", suffix: ".example.com"},
origin: "https://api.service.example.com",
expected: false,
},
{
name: "match with different scheme",
sub: subdomain{prefix: "https://", suffix: ".example.com"},
origin: "http://api.service.example.com",
expected: false,
},
{
name: "match with valid subdomain",
sub: subdomain{prefix: "https://api.", suffix: ".example.com"},
sub: subdomain{prefix: "https://", suffix: ".example.com"},
origin: "https://api.service.example.com",
expected: true,
},
{
name: "match with valid nested subdomain",
sub: subdomain{prefix: "https://", suffix: ".example.com"},
origin: "https://1.2.api.service.example.com",
expected: true,
},

{
name: "no match with invalid prefix",
sub: subdomain{prefix: "https://api.", suffix: ".example.com"},
sub: subdomain{prefix: "https://abc.", suffix: ".example.com"},
origin: "https://service.example.com",
expected: false,
},
{
name: "no match with invalid suffix",
sub: subdomain{prefix: "https://api.", suffix: ".example.com"},
sub: subdomain{prefix: "https://", suffix: ".example.com"},
origin: "https://api.example.org",
expected: false,
},
{
name: "no match with empty origin",
sub: subdomain{prefix: "https://api.", suffix: ".example.com"},
sub: subdomain{prefix: "https://", suffix: ".example.com"},
origin: "",
expected: false,
},
{
name: "partial match not considered a match",
sub: subdomain{prefix: "https://api.", suffix: ".example.com"},
sub: subdomain{prefix: "https://service.", suffix: ".example.com"},
origin: "https://api.example.com",
expected: false,
},
Expand All @@ -157,20 +193,3 @@ func TestSubdomainMatch(t *testing.T) {
})
}
}

// go test -v -run=^$ -bench=Benchmark_CORS_SubdomainMatch -benchmem -count=4
func Benchmark_CORS_SubdomainMatch(b *testing.B) {
s := subdomain{
prefix: "www",
suffix: ".example.com",
}

o := "www.example.com"

b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
s.match(o)
}
}

0 comments on commit eaa110c

Please sign in to comment.