2020-09-18 06:46:24 +03:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2024-06-19 07:45:54 +03:00
|
|
|
"fmt"
|
2020-09-18 06:46:24 +03:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
|
|
)
|
|
|
|
|
2020-12-11 00:36:46 +03:00
|
|
|
func TestAutoHTTPtoHTTPSRedirectsImplicitPort(t *testing.T) {
|
2024-06-19 07:45:54 +03:00
|
|
|
harness := caddytest.StartHarness(t)
|
|
|
|
harness.LoadConfig(`
|
2020-12-11 00:36:46 +03:00
|
|
|
{
|
2024-06-19 07:45:54 +03:00
|
|
|
admin {$TESTING_CADDY_ADMIN_BIND}
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
2024-06-19 07:45:54 +03:00
|
|
|
http_port {$TESTING_CADDY_PORT_ONE}
|
|
|
|
https_port {$TESTING_CADDY_PORT_TWO}
|
2020-12-11 00:36:46 +03:00
|
|
|
}
|
|
|
|
localhost
|
|
|
|
respond "Yahaha! You found me!"
|
|
|
|
`, "caddyfile")
|
|
|
|
|
2024-06-19 07:45:54 +03:00
|
|
|
harness.AssertRedirect(fmt.Sprintf("http://localhost:%d/", harness.Tester().PortOne()), "https://localhost/", http.StatusPermanentRedirect)
|
2020-12-11 00:36:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoHTTPtoHTTPSRedirectsExplicitPortSameAsHTTPSPort(t *testing.T) {
|
2024-06-19 07:45:54 +03:00
|
|
|
harness := caddytest.StartHarness(t)
|
|
|
|
harness.LoadConfig(`
|
2020-09-18 06:46:24 +03:00
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
2024-06-19 07:45:54 +03:00
|
|
|
admin {$TESTING_CADDY_ADMIN_BIND}
|
|
|
|
http_port {$TESTING_CADDY_PORT_ONE}
|
|
|
|
https_port {$TESTING_CADDY_PORT_TWO}
|
2020-09-18 06:46:24 +03:00
|
|
|
}
|
2024-06-19 07:45:54 +03:00
|
|
|
localhost:{$TESTING_CADDY_PORT_TWO}
|
2020-09-18 06:46:24 +03:00
|
|
|
respond "Yahaha! You found me!"
|
|
|
|
`, "caddyfile")
|
|
|
|
|
2024-06-19 07:45:54 +03:00
|
|
|
harness.AssertRedirect(fmt.Sprintf("http://localhost:%d/", harness.Tester().PortOne()), "https://localhost/", http.StatusPermanentRedirect)
|
2020-09-18 06:46:24 +03:00
|
|
|
}
|
2020-12-11 00:36:46 +03:00
|
|
|
|
|
|
|
func TestAutoHTTPtoHTTPSRedirectsExplicitPortDifferentFromHTTPSPort(t *testing.T) {
|
2024-06-19 07:45:54 +03:00
|
|
|
harness := caddytest.StartHarness(t)
|
|
|
|
harness.LoadConfig(`
|
2020-12-11 00:36:46 +03:00
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
2024-06-19 07:45:54 +03:00
|
|
|
admin {$TESTING_CADDY_ADMIN_BIND}
|
|
|
|
http_port {$TESTING_CADDY_PORT_ONE}
|
|
|
|
https_port {$TESTING_CADDY_PORT_TWO}
|
2020-12-11 00:36:46 +03:00
|
|
|
}
|
|
|
|
localhost:1234
|
|
|
|
respond "Yahaha! You found me!"
|
|
|
|
`, "caddyfile")
|
|
|
|
|
2024-06-19 07:45:54 +03:00
|
|
|
harness.AssertRedirect(fmt.Sprintf("http://localhost:%d/", harness.Tester().PortOne()), "https://localhost:1234/", http.StatusPermanentRedirect)
|
2020-12-11 00:36:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoHTTPRedirectsWithHTTPListenerFirstInAddresses(t *testing.T) {
|
2024-06-19 07:45:54 +03:00
|
|
|
harness := caddytest.StartHarness(t)
|
|
|
|
harness.LoadConfig(`
|
2020-12-11 00:36:46 +03:00
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
"admin": {
|
2024-06-19 07:45:54 +03:00
|
|
|
"listen": "{$TESTING_CADDY_ADMIN_BIND}"
|
2022-09-24 22:00:55 +03:00
|
|
|
},
|
2020-12-11 00:36:46 +03:00
|
|
|
"apps": {
|
|
|
|
"http": {
|
2024-06-19 07:45:54 +03:00
|
|
|
"http_port": {$TESTING_CADDY_PORT_ONE},
|
|
|
|
"https_port": {$TESTING_CADDY_PORT_TWO},
|
2020-12-11 00:36:46 +03:00
|
|
|
"servers": {
|
|
|
|
"ingress_server": {
|
|
|
|
"listen": [
|
2024-06-19 07:45:54 +03:00
|
|
|
":{$TESTING_CADDY_PORT_ONE}",
|
|
|
|
":{$TESTING_CADDY_PORT_TWO}"
|
2020-12-11 00:36:46 +03:00
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"match": [
|
|
|
|
{
|
|
|
|
"host": ["localhost"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 22:00:55 +03:00
|
|
|
},
|
|
|
|
"pki": {
|
|
|
|
"certificate_authorities": {
|
|
|
|
"local": {
|
|
|
|
"install_trust": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 00:36:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`, "json")
|
2024-06-19 07:45:54 +03:00
|
|
|
harness.AssertRedirect(fmt.Sprintf("http://localhost:%d/", harness.Tester().PortOne()), "https://localhost/", http.StatusPermanentRedirect)
|
2020-12-11 00:36:46 +03:00
|
|
|
}
|
2021-04-20 04:54:12 +03:00
|
|
|
|
|
|
|
func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAll(t *testing.T) {
|
2024-06-19 07:45:54 +03:00
|
|
|
harness := caddytest.StartHarness(t)
|
|
|
|
harness.LoadConfig(`
|
2021-04-20 04:54:12 +03:00
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
2024-06-19 07:45:54 +03:00
|
|
|
admin {$TESTING_CADDY_ADMIN_BIND}
|
|
|
|
http_port {$TESTING_CADDY_PORT_ONE}
|
|
|
|
https_port {$TESTING_CADDY_PORT_TWO}
|
2021-04-20 04:54:12 +03:00
|
|
|
local_certs
|
|
|
|
}
|
2024-06-19 07:45:54 +03:00
|
|
|
http://:{$TESTING_CADDY_PORT_ONE} {
|
2021-04-20 04:54:12 +03:00
|
|
|
respond "Foo"
|
|
|
|
}
|
2024-06-19 07:45:54 +03:00
|
|
|
http://baz.localhost:{$TESTING_CADDY_PORT_ONE} {
|
2021-04-20 04:54:12 +03:00
|
|
|
respond "Baz"
|
|
|
|
}
|
|
|
|
bar.localhost {
|
|
|
|
respond "Bar"
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
2024-06-19 07:45:54 +03:00
|
|
|
harness.AssertRedirect(fmt.Sprintf("http://bar.localhost:%d/", harness.Tester().PortOne()), "https://bar.localhost/", http.StatusPermanentRedirect)
|
|
|
|
harness.AssertGetResponse(fmt.Sprintf("http://foo.localhost:%d/", harness.Tester().PortOne()), 200, "Foo")
|
|
|
|
harness.AssertGetResponse(fmt.Sprintf("http://baz.localhost:%d/", harness.Tester().PortOne()), 200, "Baz")
|
2021-04-20 04:54:12 +03:00
|
|
|
}
|
2021-07-14 19:49:34 +03:00
|
|
|
|
|
|
|
func TestAutoHTTPRedirectsInsertedBeforeUserDefinedCatchAllWithNoExplicitHTTPSite(t *testing.T) {
|
2024-06-19 07:45:54 +03:00
|
|
|
harness := caddytest.StartHarness(t)
|
|
|
|
harness.LoadConfig(`
|
2021-07-14 19:49:34 +03:00
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
2024-06-19 07:45:54 +03:00
|
|
|
admin {$TESTING_CADDY_ADMIN_BIND}
|
|
|
|
http_port {$TESTING_CADDY_PORT_ONE}
|
|
|
|
https_port {$TESTING_CADDY_PORT_TWO}
|
2021-07-14 19:49:34 +03:00
|
|
|
local_certs
|
|
|
|
}
|
2024-06-19 07:45:54 +03:00
|
|
|
http://:{$TESTING_CADDY_PORT_ONE} {
|
2021-07-14 19:49:34 +03:00
|
|
|
respond "Foo"
|
|
|
|
}
|
|
|
|
bar.localhost {
|
|
|
|
respond "Bar"
|
|
|
|
}
|
|
|
|
`, "caddyfile")
|
2024-06-19 07:45:54 +03:00
|
|
|
harness.AssertRedirect(fmt.Sprintf("http://bar.localhost:%d/", harness.Tester().PortOne()), "https://bar.localhost/", http.StatusPermanentRedirect)
|
|
|
|
harness.AssertGetResponse(fmt.Sprintf("http://foo.localhost:%d/", harness.Tester().PortOne()), 200, "Foo")
|
|
|
|
harness.AssertGetResponse(fmt.Sprintf("http://baz.localhost:%d/", harness.Tester().PortOne()), 200, "Foo")
|
2021-07-14 19:49:34 +03:00
|
|
|
}
|