| 1 | package httpcaddyfile |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 8 | _ "github.com/caddyserver/caddy/v2/modules/logging" |
| 9 | ) |
| 10 | |
| 11 | func TestLogDirectiveSyntax(t *testing.T) { |
| 12 | for i, tc := range []struct { |
| 13 | input string |
| 14 | output string |
| 15 | expectError bool |
| 16 | }{ |
| 17 | { |
| 18 | input: `:8080 { |
| 19 | log |
| 20 | } |
| 21 | `, |
| 22 | output: `{"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{}}}}}}`, |
| 23 | expectError: false, |
| 24 | }, |
| 25 | { |
| 26 | input: `:8080 { |
| 27 | log { |
| 28 | core mock |
| 29 | output file foo.log |
| 30 | } |
| 31 | } |
| 32 | `, |
| 33 | output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"writer":{"filename":"foo.log","output":"file"},"core":{"module":"mock"},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`, |
| 34 | expectError: false, |
| 35 | }, |
| 36 | { |
| 37 | input: `:8080 { |
| 38 | log { |
| 39 | format filter { |
| 40 | wrap console |
| 41 | fields { |
| 42 | request>remote_ip ip_mask { |
| 43 | ipv4 24 |
| 44 | ipv6 32 |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | `, |
| 51 | output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"encoder":{"fields":{"request\u003eremote_ip":{"filter":"ip_mask","ipv4_cidr":24,"ipv6_cidr":32}},"format":"filter","wrap":{"format":"console"}},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`, |
| 52 | expectError: false, |
| 53 | }, |
| 54 | { |
| 55 | input: `:8080 { |
| 56 | log name-override { |
| 57 | core mock |
| 58 | output file foo.log |
| 59 | } |
| 60 | } |
| 61 | `, |
| 62 | output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.name-override"]},"name-override":{"writer":{"filename":"foo.log","output":"file"},"core":{"module":"mock"},"include":["http.log.access.name-override"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"name-override"}}}}}}`, |
| 63 | expectError: false, |
| 64 | }, |
| 65 | { |
| 66 | input: `:8080 { |
| 67 | log { |
| 68 | sampling { |
| 69 | interval 2s |
| 70 | first 3 |
| 71 | thereafter 4 |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | `, |
| 76 | output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"sampling":{"interval":2000000000,"first":3,"thereafter":4},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`, |
| 77 | expectError: false, |
| 78 | }, |
| 79 | } { |
| 80 | |
| 81 | adapter := caddyfile.Adapter{ |
| 82 | ServerType: ServerType{}, |
| 83 | } |
| 84 | |
| 85 | out, _, err := adapter.Adapt([]byte(tc.input), nil) |
| 86 | |
| 87 | if err != nil != tc.expectError { |
| 88 | t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err) |
| 89 | continue |
| 90 | } |
| 91 | |
| 92 | if string(out) != tc.output { |
| 93 | t.Errorf("Test %d error output mismatch Expected: %s, got %s", i, tc.output, out) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestRedirDirectiveSyntax(t *testing.T) { |
| 99 | for i, tc := range []struct { |
| 100 | input string |
| 101 | expectError bool |
| 102 | }{ |
| 103 | { |
| 104 | input: `:8080 { |
| 105 | redir :8081 |
| 106 | }`, |
| 107 | expectError: false, |
| 108 | }, |
| 109 | { |
| 110 | input: `:8080 { |
| 111 | redir * :8081 |
| 112 | }`, |
| 113 | expectError: false, |
| 114 | }, |
| 115 | { |
| 116 | input: `:8080 { |
| 117 | redir /api/* :8081 300 |
| 118 | }`, |
| 119 | expectError: false, |
| 120 | }, |
| 121 | { |
| 122 | input: `:8080 { |
| 123 | redir :8081 300 |
| 124 | }`, |
| 125 | expectError: false, |
| 126 | }, |
| 127 | { |
| 128 | input: `:8080 { |
| 129 | redir /api/* :8081 399 |
| 130 | }`, |
| 131 | expectError: false, |
| 132 | }, |
| 133 | { |
| 134 | input: `:8080 { |
| 135 | redir :8081 399 |
| 136 | }`, |
| 137 | expectError: false, |
| 138 | }, |
| 139 | { |
| 140 | input: `:8080 { |
| 141 | redir /old.html /new.html |
| 142 | }`, |
| 143 | expectError: false, |
| 144 | }, |
| 145 | { |
| 146 | input: `:8080 { |
| 147 | redir /old.html /new.html temporary |
| 148 | }`, |
| 149 | expectError: false, |
| 150 | }, |
| 151 | { |
| 152 | input: `:8080 { |
| 153 | redir https://example.com{uri} permanent |
| 154 | }`, |
| 155 | expectError: false, |
| 156 | }, |
| 157 | { |
| 158 | input: `:8080 { |
| 159 | redir /old.html /new.html permanent |
| 160 | }`, |
| 161 | expectError: false, |
| 162 | }, |
| 163 | { |
| 164 | input: `:8080 { |
| 165 | redir /old.html /new.html html |
| 166 | }`, |
| 167 | expectError: false, |
| 168 | }, |
| 169 | { |
| 170 | // this is now allowed so a Location header |
| 171 | // can be written and consumed by JS |
| 172 | // in the case of XHR requests |
| 173 | input: `:8080 { |
| 174 | redir * :8081 401 |
| 175 | }`, |
| 176 | expectError: false, |
| 177 | }, |
| 178 | { |
| 179 | input: `:8080 { |
| 180 | redir * :8081 402 |
| 181 | }`, |
| 182 | expectError: true, |
| 183 | }, |
| 184 | { |
| 185 | input: `:8080 { |
| 186 | redir * :8081 {http.reverse_proxy.status_code} |
| 187 | }`, |
| 188 | expectError: false, |
| 189 | }, |
| 190 | { |
| 191 | input: `:8080 { |
| 192 | redir /old.html /new.html htlm |
| 193 | }`, |
| 194 | expectError: true, |
| 195 | }, |
| 196 | { |
| 197 | input: `:8080 { |
| 198 | redir * :8081 200 |
| 199 | }`, |
| 200 | expectError: true, |
| 201 | }, |
| 202 | { |
| 203 | input: `:8080 { |
| 204 | redir * :8081 temp |
| 205 | }`, |
| 206 | expectError: true, |
| 207 | }, |
| 208 | { |
| 209 | input: `:8080 { |
| 210 | redir * :8081 perm |
| 211 | }`, |
| 212 | expectError: true, |
| 213 | }, |
| 214 | { |
| 215 | input: `:8080 { |
| 216 | redir * :8081 php |
| 217 | }`, |
| 218 | expectError: true, |
| 219 | }, |
| 220 | } { |
| 221 | |
| 222 | adapter := caddyfile.Adapter{ |
| 223 | ServerType: ServerType{}, |
| 224 | } |
| 225 | |
| 226 | _, _, err := adapter.Adapt([]byte(tc.input), nil) |
| 227 | |
| 228 | if err != nil != tc.expectError { |
| 229 | t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err) |
| 230 | continue |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func TestImportErrorLine(t *testing.T) { |
| 236 | for i, tc := range []struct { |
| 237 | input string |
| 238 | errorFunc func(err error) bool |
| 239 | }{ |
| 240 | { |
| 241 | input: `(t1) { |
| 242 | abort {args[:]} |
| 243 | } |
| 244 | :8080 { |
| 245 | import t1 |
| 246 | import t1 true |
| 247 | }`, |
| 248 | errorFunc: func(err error) bool { |
| 249 | return err != nil && strings.Contains(err.Error(), "Caddyfile:6 (import t1)") |
| 250 | }, |
| 251 | }, |
| 252 | { |
| 253 | input: `(t1) { |
| 254 | abort {args[:]} |
| 255 | } |
| 256 | :8080 { |
| 257 | import t1 true |
| 258 | }`, |
| 259 | errorFunc: func(err error) bool { |
| 260 | return err != nil && strings.Contains(err.Error(), "Caddyfile:5 (import t1)") |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | input: ` |
| 265 | import testdata/import_variadic_snippet.txt |
| 266 | :8080 { |
| 267 | import t1 true |
| 268 | }`, |
| 269 | errorFunc: func(err error) bool { |
| 270 | return err == nil |
| 271 | }, |
| 272 | }, |
| 273 | { |
| 274 | input: ` |
| 275 | import testdata/import_variadic_with_import.txt |
| 276 | :8080 { |
| 277 | import t1 true |
| 278 | import t2 true |
| 279 | }`, |
| 280 | errorFunc: func(err error) bool { |
| 281 | return err == nil |
| 282 | }, |
| 283 | }, |
| 284 | } { |
| 285 | adapter := caddyfile.Adapter{ |
| 286 | ServerType: ServerType{}, |
| 287 | } |
| 288 | |
| 289 | _, _, err := adapter.Adapt([]byte(tc.input), nil) |
| 290 | |
| 291 | if !tc.errorFunc(err) { |
| 292 | t.Errorf("Test %d error expectation failed, got %s", i, err) |
| 293 | continue |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | func TestNestedImport(t *testing.T) { |
| 299 | for i, tc := range []struct { |
| 300 | input string |
| 301 | errorFunc func(err error) bool |
| 302 | }{ |
| 303 | { |
| 304 | input: `(t1) { |
| 305 | respond {args[0]} {args[1]} |
| 306 | } |
| 307 | |
| 308 | (t2) { |
| 309 | import t1 {args[0]} 202 |
| 310 | } |
| 311 | |
| 312 | :8080 { |
| 313 | handle { |
| 314 | import t2 "foobar" |
| 315 | } |
| 316 | }`, |
| 317 | errorFunc: func(err error) bool { |
| 318 | return err == nil |
| 319 | }, |
| 320 | }, |
| 321 | { |
| 322 | input: `(t1) { |
| 323 | respond {args[:]} |
| 324 | } |
| 325 | |
| 326 | (t2) { |
| 327 | import t1 {args[0]} {args[1]} |
| 328 | } |
| 329 | |
| 330 | :8080 { |
| 331 | handle { |
| 332 | import t2 "foobar" 202 |
| 333 | } |
| 334 | }`, |
| 335 | errorFunc: func(err error) bool { |
| 336 | return err == nil |
| 337 | }, |
| 338 | }, |
| 339 | { |
| 340 | input: `(t1) { |
| 341 | respond {args[0]} {args[1]} |
| 342 | } |
| 343 | |
| 344 | (t2) { |
| 345 | import t1 {args[:]} |
| 346 | } |
| 347 | |
| 348 | :8080 { |
| 349 | handle { |
| 350 | import t2 "foobar" 202 |
| 351 | } |
| 352 | }`, |
| 353 | errorFunc: func(err error) bool { |
| 354 | return err == nil |
| 355 | }, |
| 356 | }, |
| 357 | } { |
| 358 | adapter := caddyfile.Adapter{ |
| 359 | ServerType: ServerType{}, |
| 360 | } |
| 361 | |
| 362 | _, _, err := adapter.Adapt([]byte(tc.input), nil) |
| 363 | |
| 364 | if !tc.errorFunc(err) { |
| 365 | t.Errorf("Test %d error expectation failed, got %s", i, err) |
| 366 | continue |
| 367 | } |
| 368 | } |
| 369 | } |