@@ -14,33 +14,50 @@ import (
1414)
1515
1616func Test_rule_matches (t * testing.T ) {
17- type fields struct {
18- Hostname string
19- Path * Regexp
20- Service OriginService
21- }
2217 type args struct {
2318 requestURL * url.URL
2419 }
2520 tests := []struct {
26- name string
27- fields fields
28- args args
29- want bool
21+ name string
22+ rule Rule
23+ args args
24+ want bool
3025 }{
3126 {
3227 name : "Just hostname, pass" ,
33- fields : fields {
28+ rule : Rule {
3429 Hostname : "example.com" ,
3530 },
3631 args : args {
3732 requestURL : MustParseURL (t , "https://example.com" ),
3833 },
3934 want : true ,
4035 },
36+ {
37+ name : "Unicode hostname with unicode request, pass" ,
38+ rule : Rule {
39+ Hostname : "môô.cloudflare.com" ,
40+ punycodeHostname : "xn--m-xgaa.cloudflare.com" ,
41+ },
42+ args : args {
43+ requestURL : MustParseURL (t , "https://môô.cloudflare.com" ),
44+ },
45+ want : true ,
46+ },
47+ {
48+ name : "Unicode hostname with punycode request, pass" ,
49+ rule : Rule {
50+ Hostname : "môô.cloudflare.com" ,
51+ punycodeHostname : "xn--m-xgaa.cloudflare.com" ,
52+ },
53+ args : args {
54+ requestURL : MustParseURL (t , "https://xn--m-xgaa.cloudflare.com" ),
55+ },
56+ want : true ,
57+ },
4158 {
4259 name : "Entire hostname is wildcard, should match everything" ,
43- fields : fields {
60+ rule : Rule {
4461 Hostname : "*" ,
4562 },
4663 args : args {
@@ -50,7 +67,7 @@ func Test_rule_matches(t *testing.T) {
5067 },
5168 {
5269 name : "Just hostname, fail" ,
53- fields : fields {
70+ rule : Rule {
5471 Hostname : "example.com" ,
5572 },
5673 args : args {
@@ -60,7 +77,7 @@ func Test_rule_matches(t *testing.T) {
6077 },
6178 {
6279 name : "Just wildcard hostname, pass" ,
63- fields : fields {
80+ rule : Rule {
6481 Hostname : "*.example.com" ,
6582 },
6683 args : args {
@@ -70,7 +87,7 @@ func Test_rule_matches(t *testing.T) {
7087 },
7188 {
7289 name : "Just wildcard hostname, fail" ,
73- fields : fields {
90+ rule : Rule {
7491 Hostname : "*.example.com" ,
7592 },
7693 args : args {
@@ -80,7 +97,7 @@ func Test_rule_matches(t *testing.T) {
8097 },
8198 {
8299 name : "Just wildcard outside of subdomain in hostname, fail" ,
83- fields : fields {
100+ rule : Rule {
84101 Hostname : "*example.com" ,
85102 },
86103 args : args {
@@ -90,7 +107,7 @@ func Test_rule_matches(t *testing.T) {
90107 },
91108 {
92109 name : "Wildcard over multiple subdomains" ,
93- fields : fields {
110+ rule : Rule {
94111 Hostname : "*.example.com" ,
95112 },
96113 args : args {
@@ -100,7 +117,7 @@ func Test_rule_matches(t *testing.T) {
100117 },
101118 {
102119 name : "Hostname and path" ,
103- fields : fields {
120+ rule : Rule {
104121 Hostname : "*.example.com" ,
105122 Path : & Regexp {Regexp : regexp .MustCompile ("/static/.*\\ .html" )},
106123 },
@@ -111,7 +128,7 @@ func Test_rule_matches(t *testing.T) {
111128 },
112129 {
113130 name : "Hostname and empty Regex" ,
114- fields : fields {
131+ rule : Rule {
115132 Hostname : "example.com" ,
116133 Path : & Regexp {},
117134 },
@@ -122,7 +139,7 @@ func Test_rule_matches(t *testing.T) {
122139 },
123140 {
124141 name : "Hostname and nil path" ,
125- fields : fields {
142+ rule : Rule {
126143 Hostname : "example.com" ,
127144 Path : nil ,
128145 },
@@ -134,13 +151,8 @@ func Test_rule_matches(t *testing.T) {
134151 }
135152 for _ , tt := range tests {
136153 t .Run (tt .name , func (t * testing.T ) {
137- r := Rule {
138- Hostname : tt .fields .Hostname ,
139- Path : tt .fields .Path ,
140- Service : tt .fields .Service ,
141- }
142154 u := tt .args .requestURL
143- if got := r .Matches (u .Hostname (), u .Path ); got != tt .want {
155+ if got := tt . rule .Matches (u .Hostname (), u .Path ); got != tt .want {
144156 t .Errorf ("rule.matches() = %v, want %v" , got , tt .want )
145157 }
146158 })
0 commit comments