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 | "net/http" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/caddyserver/caddy/v2/caddytest" |
| 8 | ) |
| 9 | |
| 10 | func TestAutoHTTPtoHTTPSRedirectsImplicitPort(t *testing.T) { |
| 11 | tester := caddytest.NewTester(t) |
| 12 | tester.InitServer(` |
| 13 | { |
| 14 | admin localhost:2999 |
| 15 | skip_install_trust |
| 16 | http_port 9080 |
| 17 | https_port 9443 |
| 18 | } |
| 19 | localhost |
| 20 | respond "Yahaha! You found me!" |
| 21 | `, "caddyfile") |
| 22 | |
| 23 | tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect) |
| 24 | } |
| 25 | |
| 26 | func TestAutoHTTPtoHTTPSRedirectsExplicitPortSameAsHTTPSPort(t *testing.T) { |
| 27 | tester := caddytest.NewTester(t) |
| 28 | tester.InitServer(` |
| 29 | { |
| 30 | skip_install_trust |
| 31 | admin localhost:2999 |
| 32 | http_port 9080 |
| 33 | https_port 9443 |
| 34 | } |
| 35 | localhost:9443 |
| 36 | respond "Yahaha! You found me!" |
| 37 | `, "caddyfile") |
| 38 | |
| 39 | tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect) |
| 40 | } |
| 41 | |
| 42 | func TestAutoHTTPtoHTTPSRedirectsExplicitPortDifferentFromHTTPSPort(t *testing.T) { |
| 43 | tester := caddytest.NewTester(t) |
| 44 | tester.InitServer(` |
| 45 | { |
| 46 | skip_install_trust |
| 47 | admin localhost:2999 |
| 48 | http_port 9080 |
| 49 | https_port 9443 |
| 50 | } |
| 51 | localhost:1234 |
| 52 | respond "Yahaha! You found me!" |
| 53 | `, "caddyfile") |
| 54 | |
| 55 | tester.AssertRedirect("http://localhost:9080/", "https://localhost:1234/", http.StatusPermanentRedirect) |
| 56 | } |
| 57 | |
| 58 | func TestAutoHTTPtoHTTPSRedirectsPreferHTTPSPortOverAlternatePort(t *testing.T) { |
| 59 | tester := caddytest.NewTester(t) |
| 60 | tester.InitServer(` |
| 61 | { |
| 62 | skip_install_trust |
| 63 | admin localhost:2999 |
| 64 | http_port 9080 |
| 65 | https_port 9443 |
| 66 | local_certs |
| 67 | } |
| 68 | localhost { |
| 69 | respond "Canonical" |
| 70 | } |
| 71 | |
| 72 | localhost:10443 { |
| 73 | respond "Alternate" |
| 74 | } |
| 75 | `, "caddyfile") |
| 76 | |
| 77 | tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect) |
| 78 | } |
| 79 | |
| 80 | func TestAutoHTTPRedirectsWithHTTPListenerFirstInAddresses(t *testing.T) { |
| 81 | tester := caddytest.NewTester(t) |
| 82 | tester.InitServer(` |
| 83 | { |
| 84 | "admin": { |
| 85 | "listen": "localhost:2999" |
| 86 | }, |
| 87 | "apps": { |
| 88 | "http": { |
| 89 | "http_port": 9080, |
| 90 | "https_port": 9443, |
| 91 | "servers": { |
| 92 | "ingress_server": { |
| 93 | "listen": [ |
| 94 | ":9080", |
| 95 | ":9443" |
| 96 | ], |
| 97 | "routes": [ |
| 98 | { |
| 99 | "match": [ |
| 100 | { |
| 101 | "host": ["localhost"] |
| 102 | } |
| 103 | ] |
| 104 | } |
| 105 | ] |
| 106 | } |
| 107 | } |
| 108 | }, |
| 109 | "pki": { |
| 110 | "certificate_authorities": { |
| 111 | "local": { |
| 112 | "install_trust": false |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | `, "json") |
| 119 | tester.AssertRedirect("http://localhost:9080/", "https://localhost/", http.StatusPermanentRedirect) |
| 120 | } |
| 121 | |
| 122 | func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAll(t *testing.T) { |
| 123 | tester := caddytest.NewTester(t) |
| 124 | tester.InitServer(` |
| 125 | { |
| 126 | skip_install_trust |
| 127 | admin localhost:2999 |
| 128 | http_port 9080 |
| 129 | https_port 9443 |
| 130 | local_certs |
| 131 | } |
| 132 | http://:9080 { |
| 133 | respond "Foo" |
| 134 | } |
| 135 | http://baz.localhost:9080 { |
| 136 | respond "Baz" |
| 137 | } |
| 138 | bar.localhost { |
| 139 | respond "Bar" |
| 140 | } |
| 141 | `, "caddyfile") |
| 142 | tester.AssertRedirect("http://bar.localhost:9080/", "https://bar.localhost/", http.StatusPermanentRedirect) |
| 143 | tester.AssertGetResponse("http://foo.localhost:9080/", 200, "Foo") |
| 144 | tester.AssertGetResponse("http://baz.localhost:9080/", 200, "Baz") |
| 145 | } |
| 146 | |
| 147 | func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAllWithNoExplicitHTTPSite(t *testing.T) { |
| 148 | tester := caddytest.NewTester(t) |
| 149 | tester.InitServer(` |
| 150 | { |
| 151 | skip_install_trust |
| 152 | admin localhost:2999 |
| 153 | http_port 9080 |
| 154 | https_port 9443 |
| 155 | local_certs |
| 156 | } |
| 157 | http://:9080 { |
| 158 | respond "Foo" |
| 159 | } |
| 160 | bar.localhost { |
| 161 | respond "Bar" |
| 162 | } |
| 163 | `, "caddyfile") |
| 164 | tester.AssertRedirect("http://bar.localhost:9080/", "https://bar.localhost/", http.StatusPermanentRedirect) |
| 165 | tester.AssertGetResponse("http://foo.localhost:9080/", 200, "Foo") |
| 166 | tester.AssertGetResponse("http://baz.localhost:9080/", 200, "Foo") |
| 167 | } |
| 168 | |
| 169 | func TestAutoHTTPSRedirectSortingExactMatchOverWildcard(t *testing.T) { |
| 170 | tester := caddytest.NewTester(t) |
| 171 | tester.InitServer(` |
| 172 | { |
| 173 | skip_install_trust |
| 174 | admin localhost:2999 |
| 175 | http_port 9080 |
| 176 | https_port 9443 |
| 177 | local_certs |
| 178 | } |
| 179 | *.localhost:10443 { |
| 180 | respond "Wildcard" |
| 181 | } |
| 182 | dev.localhost { |
| 183 | respond "Exact" |
| 184 | } |
| 185 | `, "caddyfile") |
| 186 | |
| 187 | tester.AssertRedirect("http://dev.localhost:9080/", "https://dev.localhost/", http.StatusPermanentRedirect) |
| 188 | |
| 189 | tester.AssertRedirect("http://foo.localhost:9080/", "https://foo.localhost:10443/", http.StatusPermanentRedirect) |
| 190 | } |