| 110 | } |
| 111 | |
| 112 | func TestServerNameREMatcher(t *testing.T) { |
| 113 | for i, tc := range []struct { |
| 114 | pattern string |
| 115 | input string |
| 116 | expect bool |
| 117 | }{ |
| 118 | { |
| 119 | pattern: "^example\\.(com|net)$", |
| 120 | input: "example.com", |
| 121 | expect: true, |
| 122 | }, |
| 123 | { |
| 124 | pattern: "^example\\.(com|net)$", |
| 125 | input: "foo.com", |
| 126 | expect: false, |
| 127 | }, |
| 128 | { |
| 129 | pattern: "^example\\.(com|net)$", |
| 130 | input: "", |
| 131 | expect: false, |
| 132 | }, |
| 133 | { |
| 134 | pattern: "", |
| 135 | input: "", |
| 136 | expect: true, |
| 137 | }, |
| 138 | { |
| 139 | pattern: "^example\\.(com|net)$", |
| 140 | input: "foo.example.com", |
| 141 | expect: false, |
| 142 | }, |
| 143 | } { |
| 144 | chi := &tls.ClientHelloInfo{ServerName: tc.input} |
| 145 | mre := MatchServerNameRE{MatchRegexp{Pattern: tc.pattern}} |
| 146 | ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 147 | if mre.Provision(ctx) != nil { |
| 148 | t.Errorf("Test %d: Failed to provision a regexp matcher (pattern=%v)", i, tc.pattern) |
| 149 | } |
| 150 | actual := mre.Match(chi) |
| 151 | if actual != tc.expect { |
| 152 | t.Errorf("Test %d: Expected %t but got %t (input=%s match=%v)", |
| 153 | i, tc.expect, actual, tc.input, tc.pattern) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestRemoteIPMatcher(t *testing.T) { |
| 159 | ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) |