From eaa110c1849f69135bd9c63871f096877cdc96a1 Mon Sep 17 00:00:00 2001 From: Jason McNeil Date: Mon, 18 Mar 2024 21:38:20 -0300 Subject: [PATCH] test(middleware/cors): cover additiona test cases --- middleware/cors/cors_test.go | 10 ++++++ middleware/cors/utils_test.go | 65 ++++++++++++++++++++++------------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/middleware/cors/cors_test.go b/middleware/cors/cors_test.go index 932e56c476..57f5f91205 100644 --- a/middleware/cors/cors_test.go +++ b/middleware/cors/cors_test.go @@ -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", @@ -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", diff --git a/middleware/cors/utils_test.go b/middleware/cors/utils_test.go index ba7c0c9c68..47dddc2c69 100644 --- a/middleware/cors/utils_test.go +++ b/middleware/cors/utils_test.go @@ -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, }, @@ -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) - } -}