| 1 | package httpcaddyfile |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 9 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 10 | ) |
| 11 | |
| 12 | func TestMatcherSyntax(t *testing.T) { |
| 13 | for i, tc := range []struct { |
| 14 | input string |
| 15 | expectError bool |
| 16 | expectContains string |
| 17 | }{ |
| 18 | { |
| 19 | input: `http://localhost |
| 20 | @debug { |
| 21 | query showdebug=1 |
| 22 | } |
| 23 | `, |
| 24 | expectError: false, |
| 25 | }, |
| 26 | { |
| 27 | input: `http://localhost |
| 28 | @debug { |
| 29 | query bad format |
| 30 | } |
| 31 | `, |
| 32 | expectError: true, |
| 33 | }, |
| 34 | { |
| 35 | input: `http://localhost |
| 36 | @debug { |
| 37 | not { |
| 38 | path /somepath* |
| 39 | } |
| 40 | } |
| 41 | `, |
| 42 | expectError: false, |
| 43 | }, |
| 44 | { |
| 45 | input: `http://localhost |
| 46 | @debug { |
| 47 | not path /somepath* |
| 48 | } |
| 49 | `, |
| 50 | expectError: false, |
| 51 | }, |
| 52 | { |
| 53 | input: `http://localhost |
| 54 | @debug not path /somepath* |
| 55 | `, |
| 56 | expectError: false, |
| 57 | }, |
| 58 | { |
| 59 | input: `http://localhost { |
| 60 | @test { |
| 61 | path /test |
| 62 | } |
| 63 | @test { |
| 64 | path /other |
| 65 | } |
| 66 | respond @test "hello" |
| 67 | } |
| 68 | `, |
| 69 | expectError: true, |
| 70 | expectContains: "is defined more than once", |
| 71 | }, |
| 72 | { |
| 73 | input: `(snippet) { |
| 74 | @{args[0]} { |
| 75 | path /{args[0]} |
| 76 | } |
| 77 | respond @{args[0]} "hello" |
| 78 | } |
| 79 | http://localhost { |
| 80 | import snippet foo |
| 81 | import snippet bar |
| 82 | } |
| 83 | `, |
| 84 | expectError: false, |
| 85 | }, |
| 86 | { |
| 87 | input: `@matcher { |
| 88 | path /matcher-not-allowed/outside-of-site-block/* |
| 89 | } |
| 90 | http://localhost |
| 91 | `, |
| 92 | expectError: true, |
| 93 | }, |
| 94 | } { |
| 95 | |
| 96 | adapter := caddyfile.Adapter{ |
| 97 | ServerType: ServerType{}, |
| 98 | } |
| 99 | |
| 100 | _, _, err := adapter.Adapt([]byte(tc.input), nil) |
| 101 | |
| 102 | if err != nil != tc.expectError { |
| 103 | t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err) |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | if err != nil && tc.expectContains != "" { |
| 108 | if !strings.Contains(err.Error(), tc.expectContains) { |
| 109 | t.Errorf("Test %d error message mismatch: expected to contain %q, got %q", |
| 110 | i, tc.expectContains, err.Error()) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestSpecificity(t *testing.T) { |
| 117 | for i, tc := range []struct { |
| 118 | input string |
| 119 | expect int |
| 120 | }{ |
| 121 | {"", 0}, |
| 122 | {"*", 0}, |
| 123 | {"*.*", 1}, |
| 124 | {"{placeholder}", 0}, |
| 125 | {"/{placeholder}", 1}, |
| 126 | {"foo", 3}, |
| 127 | {"example.com", 11}, |
| 128 | {"a.example.com", 13}, |
| 129 | {"*.example.com", 12}, |
| 130 | {"/foo", 4}, |
| 131 | {"/foo*", 4}, |
| 132 | {"{placeholder}.example.com", 12}, |
| 133 | {"{placeholder.example.com", 24}, |
| 134 | {"}.", 2}, |
| 135 | {"}{", 2}, |
| 136 | {"{}", 0}, |
| 137 | {"{{{}}", 1}, |
| 138 | } { |
| 139 | actual := specificity(tc.input) |
| 140 | if actual != tc.expect { |
| 141 | t.Errorf("Test %d (%s): Expected %d but got %d", i, tc.input, tc.expect, actual) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestGlobalOptions(t *testing.T) { |
| 147 | for i, tc := range []struct { |
| 148 | input string |
| 149 | expectError bool |
| 150 | }{ |
| 151 | { |
| 152 | input: ` |
| 153 | { |
| 154 | email [email protected] |
| 155 | } |
| 156 | :80 |
| 157 | `, |
| 158 | expectError: false, |
| 159 | }, |
| 160 | { |
| 161 | input: ` |
| 162 | { |
| 163 | admin off |
| 164 | } |
| 165 | :80 |
| 166 | `, |
| 167 | expectError: false, |
| 168 | }, |
| 169 | { |
| 170 | input: ` |
| 171 | { |
| 172 | admin 127.0.0.1:2020 |
| 173 | } |
| 174 | :80 |
| 175 | `, |
| 176 | expectError: false, |
| 177 | }, |
| 178 | { |
| 179 | input: ` |
| 180 | { |
| 181 | admin { |
| 182 | disabled false |
| 183 | } |
| 184 | } |
| 185 | :80 |
| 186 | `, |
| 187 | expectError: true, |
| 188 | }, |
| 189 | { |
| 190 | input: ` |
| 191 | { |
| 192 | admin { |
| 193 | enforce_origin |
| 194 | origins 192.168.1.1:2020 127.0.0.1:2020 |
| 195 | } |
| 196 | } |
| 197 | :80 |
| 198 | `, |
| 199 | expectError: false, |
| 200 | }, |
| 201 | { |
| 202 | input: ` |
| 203 | { |
| 204 | admin 127.0.0.1:2020 { |
| 205 | enforce_origin |
| 206 | origins 192.168.1.1:2020 127.0.0.1:2020 |
| 207 | } |
| 208 | } |
| 209 | :80 |
| 210 | `, |
| 211 | expectError: false, |
| 212 | }, |
| 213 | { |
| 214 | input: ` |
| 215 | { |
| 216 | admin 192.168.1.1:2020 127.0.0.1:2020 { |
| 217 | enforce_origin |
| 218 | origins 192.168.1.1:2020 127.0.0.1:2020 |
| 219 | } |
| 220 | } |
| 221 | :80 |
| 222 | `, |
| 223 | expectError: true, |
| 224 | }, |
| 225 | { |
| 226 | input: ` |
| 227 | { |
| 228 | admin off { |
| 229 | enforce_origin |
| 230 | origins 192.168.1.1:2020 127.0.0.1:2020 |
| 231 | } |
| 232 | } |
| 233 | :80 |
| 234 | `, |
| 235 | expectError: true, |
| 236 | }, |
| 237 | } { |
| 238 | |
| 239 | adapter := caddyfile.Adapter{ |
| 240 | ServerType: ServerType{}, |
| 241 | } |
| 242 | |
| 243 | _, _, err := adapter.Adapt([]byte(tc.input), nil) |
| 244 | |
| 245 | if err != nil != tc.expectError { |
| 246 | t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err) |
| 247 | continue |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func TestDefaultSNIWithoutHTTPS(t *testing.T) { |
| 253 | caddyfileStr := `{ |
| 254 | default_sni my-sni.com |
| 255 | } |
| 256 | example.com { |
| 257 | }` |
| 258 | |
| 259 | adapter := caddyfile.Adapter{ |
| 260 | ServerType: ServerType{}, |
| 261 | } |
| 262 | |
| 263 | result, _, err := adapter.Adapt([]byte(caddyfileStr), nil) |
| 264 | if err != nil { |
| 265 | t.Fatalf("Failed to adapt Caddyfile: %v", err) |
| 266 | } |
| 267 | |
| 268 | var config struct { |
| 269 | Apps struct { |
| 270 | HTTP struct { |
| 271 | Servers map[string]*caddyhttp.Server `json:"servers"` |
| 272 | } `json:"http"` |
| 273 | } `json:"apps"` |
| 274 | } |
| 275 | |
| 276 | if err := json.Unmarshal(result, &config); err != nil { |
| 277 | t.Fatalf("Failed to unmarshal JSON config: %v", err) |
| 278 | } |
| 279 | |
| 280 | server, ok := config.Apps.HTTP.Servers["srv0"] |
| 281 | if !ok { |
| 282 | t.Fatalf("Expected server 'srv0' to be created") |
| 283 | } |
| 284 | |
| 285 | if len(server.TLSConnPolicies) == 0 { |
| 286 | t.Fatalf("Expected TLS connection policies to be generated, got none") |
| 287 | } |
| 288 | |
| 289 | found := false |
| 290 | for _, policy := range server.TLSConnPolicies { |
| 291 | if policy.DefaultSNI == "my-sni.com" { |
| 292 | found = true |
| 293 | break |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if !found { |
| 298 | t.Errorf("Expected default_sni 'my-sni.com' in TLS connection policies, but it was missing. Generated JSON: %s", string(result)) |
| 299 | } |
| 300 | } |