Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | package integration |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/caddyserver/caddy/v2/caddytest" |
| 7 | ) |
| 8 | |
| 9 | func TestIntercept(t *testing.T) { |
| 10 | tester := caddytest.NewTester(t) |
| 11 | tester.InitServer(`{ |
| 12 | skip_install_trust |
| 13 | admin localhost:2999 |
| 14 | http_port 9080 |
| 15 | https_port 9443 |
| 16 | grace_period 1ns |
| 17 | } |
| 18 | |
| 19 | localhost:9080 { |
| 20 | respond /intercept "I'm a teapot" 408 |
| 21 | header /intercept To-Intercept ok |
| 22 | respond /no-intercept "I'm not a teapot" |
| 23 | |
| 24 | intercept { |
| 25 | @teapot status 408 |
| 26 | handle_response @teapot { |
| 27 | header /intercept intercepted {resp.header.To-Intercept} |
| 28 | respond /intercept "I'm a combined coffee/tea pot that is temporarily out of coffee" 503 |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | `, "caddyfile") |
| 33 | |
| 34 | r, _ := tester.AssertGetResponse("http://localhost:9080/intercept", 503, "I'm a combined coffee/tea pot that is temporarily out of coffee") |
| 35 | if r.Header.Get("intercepted") != "ok" { |
| 36 | t.Fatalf(`header "intercepted" value is not "ok": %s`, r.Header.Get("intercepted")) |
| 37 | } |
| 38 | |
| 39 | tester.AssertGetResponse("http://localhost:9080/no-intercept", 200, "I'm not a teapot") |
| 40 | } |
| 41 | |
| 42 | func TestInterceptReplaceStatusWithMatcher(t *testing.T) { |
| 43 | tester := caddytest.NewTester(t) |
| 44 | tester.InitServer(`{ |
| 45 | skip_install_trust |
| 46 | admin localhost:2999 |
| 47 | http_port 9080 |
| 48 | https_port 9443 |
| 49 | grace_period 1ns |
| 50 | } |
| 51 | |
| 52 | localhost:9080 { |
| 53 | respond /error "boom" 500 |
| 54 | |
| 55 | intercept { |
| 56 | @err status 5xx |
| 57 | replace_status @err 200 |
| 58 | } |
| 59 | } |
| 60 | `, "caddyfile") |
| 61 | |
| 62 | tester.AssertGetResponse("http://localhost:9080/error", 200, "boom") |
| 63 | } |
| 64 | |
| 65 | func TestInterceptReplaceStatusWithoutMatcher(t *testing.T) { |
| 66 | tester := caddytest.NewTester(t) |
| 67 | tester.InitServer(`{ |
| 68 | skip_install_trust |
| 69 | admin localhost:2999 |
| 70 | http_port 9080 |
| 71 | https_port 9443 |
| 72 | grace_period 1ns |
| 73 | } |
| 74 | |
| 75 | localhost:9080 { |
| 76 | respond /forbidden "denied" 403 |
| 77 | |
| 78 | intercept { |
| 79 | replace_status 200 |
| 80 | } |
| 81 | } |
| 82 | `, "caddyfile") |
| 83 | |
| 84 | tester.AssertGetResponse("http://localhost:9080/forbidden", 200, "denied") |
| 85 | } |
| 86 | |
| 87 | func TestInterceptReplaceStatusNotMatched(t *testing.T) { |
| 88 | tester := caddytest.NewTester(t) |
| 89 | tester.InitServer(`{ |
| 90 | skip_install_trust |
| 91 | admin localhost:2999 |
| 92 | http_port 9080 |
| 93 | https_port 9443 |
| 94 | grace_period 1ns |
| 95 | } |
| 96 | |
| 97 | localhost:9080 { |
| 98 | respond /ok "all good" 200 |
| 99 | |
| 100 | intercept { |
| 101 | @err status 5xx |
| 102 | replace_status @err 503 |
| 103 | } |
| 104 | } |
| 105 | `, "caddyfile") |
| 106 | |
| 107 | // 200 does not match @err (5xx), so status should pass through unchanged |
| 108 | tester.AssertGetResponse("http://localhost:9080/ok", 200, "all good") |
| 109 | } |