git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/httpx/cors/utils_test.go (about) 1 package cors 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestWildcard(t *testing.T) { 9 w := wildcard{"foo", "bar"} 10 if !w.match("foobar") { 11 t.Error("foo*bar should match foobar") 12 } 13 if !w.match("foobazbar") { 14 t.Error("foo*bar should match foobazbar") 15 } 16 if w.match("foobaz") { 17 t.Error("foo*bar should not match foobaz") 18 } 19 20 w = wildcard{"foo", "oof"} 21 if w.match("foof") { 22 t.Error("foo*oof should not match foof") 23 } 24 } 25 26 func TestConvert(t *testing.T) { 27 s := convert([]string{"A", "b", "C"}, strings.ToLower) 28 e := []string{"a", "b", "c"} 29 if s[0] != e[0] || s[1] != e[1] || s[2] != e[2] { 30 t.Errorf("%v != %v", s, e) 31 } 32 } 33 34 func TestParseHeaderList(t *testing.T) { 35 h := parseHeaderList("header, second-header, THIRD-HEADER, Numb3r3d-H34d3r") 36 e := []string{"Header", "Second-Header", "Third-Header", "Numb3r3d-H34d3r"} 37 if h[0] != e[0] || h[1] != e[1] || h[2] != e[2] { 38 t.Errorf("%v != %v", h, e) 39 } 40 } 41 42 func TestParseHeaderListEmpty(t *testing.T) { 43 if len(parseHeaderList("")) != 0 { 44 t.Error("should be empty sclice") 45 } 46 if len(parseHeaderList(" , ")) != 0 { 47 t.Error("should be empty sclice") 48 } 49 } 50 51 func BenchmarkParseHeaderList(b *testing.B) { 52 b.ReportAllocs() 53 for i := 0; i < b.N; i++ { 54 parseHeaderList("header, second-header, THIRD-HEADER") 55 } 56 } 57 58 func BenchmarkParseHeaderListSingle(b *testing.B) { 59 b.ReportAllocs() 60 for i := 0; i < b.N; i++ { 61 parseHeaderList("header") 62 } 63 } 64 65 func BenchmarkParseHeaderListNormalized(b *testing.B) { 66 b.ReportAllocs() 67 for i := 0; i < b.N; i++ { 68 parseHeaderList("Header1, Header2, Third-Header") 69 } 70 } 71 72 func BenchmarkWildcard(b *testing.B) { 73 w := wildcard{"foo", "bar"} 74 b.Run("match", func(b *testing.B) { 75 b.ReportAllocs() 76 for i := 0; i < b.N; i++ { 77 w.match("foobazbar") 78 } 79 }) 80 b.Run("too short", func(b *testing.B) { 81 b.ReportAllocs() 82 for i := 0; i < b.N; i++ { 83 w.match("fobar") 84 } 85 }) 86 }