72 lines
2.5 KiB
Go
72 lines
2.5 KiB
Go
// This file is part of the happyDomain (R) project.
|
|
// Copyright (c) 2020-2026 happyDomain
|
|
// Authors: Pierre-Olivier Mercier, et al.
|
|
|
|
package checker
|
|
|
|
import (
|
|
"testing"
|
|
|
|
sdk "git.happydns.org/checker-sdk-go/checker"
|
|
)
|
|
|
|
func TestHTTPSRedirectRule_NoProbes(t *testing.T) {
|
|
data := &HTTPData{Probes: []HTTPProbe{httpsProbe("a:443")}}
|
|
states := runRule(t, &httpsRedirectRule{}, data, nil)
|
|
mustStatus(t, states, sdk.StatusUnknown)
|
|
}
|
|
|
|
func TestHTTPSRedirectRule_OKViaFinalURL(t *testing.T) {
|
|
p := httpProbe("a:80")
|
|
p.StatusCode = 301
|
|
p.FinalURL = "https://example.test/"
|
|
states := runRule(t, &httpsRedirectRule{}, &HTTPData{Probes: []HTTPProbe{p}}, nil)
|
|
mustStatus(t, states, sdk.StatusOK)
|
|
if !hasCode(states, "http.https_redirect.ok") {
|
|
t.Errorf("expected redirect.ok: %+v", states)
|
|
}
|
|
}
|
|
|
|
func TestHTTPSRedirectRule_OKViaRedirectChain(t *testing.T) {
|
|
// FinalURL empty (non-following CheckRedirect path): fallback to last redirect step.
|
|
p := httpProbe("a:80")
|
|
p.StatusCode = 308
|
|
p.FinalURL = ""
|
|
p.RedirectChain = []RedirectStep{{From: "http://example.test/", To: "https://example.test/"}}
|
|
states := runRule(t, &httpsRedirectRule{}, &HTTPData{Probes: []HTTPProbe{p}}, nil)
|
|
mustStatus(t, states, sdk.StatusOK)
|
|
}
|
|
|
|
func TestHTTPSRedirectRule_NoRedirect_Required(t *testing.T) {
|
|
p := httpProbe("a:80")
|
|
p.StatusCode = 200
|
|
p.FinalURL = "http://example.test/"
|
|
states := runRule(t, &httpsRedirectRule{}, &HTTPData{Probes: []HTTPProbe{p}}, sdk.CheckerOptions{OptionRequireHTTPS: true})
|
|
mustStatus(t, states, sdk.StatusWarn)
|
|
if !hasCode(states, "http.no_https_redirect") {
|
|
t.Errorf("expected no_https_redirect: %+v", states)
|
|
}
|
|
}
|
|
|
|
func TestHTTPSRedirectRule_NoRedirect_NotRequired(t *testing.T) {
|
|
p := httpProbe("a:80")
|
|
p.StatusCode = 200
|
|
p.FinalURL = "http://example.test/"
|
|
states := runRule(t, &httpsRedirectRule{}, &HTTPData{Probes: []HTTPProbe{p}}, sdk.CheckerOptions{OptionRequireHTTPS: false})
|
|
mustStatus(t, states, sdk.StatusInfo)
|
|
if !hasCode(states, "http.plain_http_served") {
|
|
t.Errorf("expected plain_http_served: %+v", states)
|
|
}
|
|
}
|
|
|
|
func TestHTTPSRedirectRule_SkipsUnreachable(t *testing.T) {
|
|
// HTTP not running on this IP: reachability rule handles it; redirect rule must skip.
|
|
p := httpProbe("a:80")
|
|
p.TCPConnected = false
|
|
p.StatusCode = 0
|
|
states := runRule(t, &httpsRedirectRule{}, &HTTPData{Probes: []HTTPProbe{p}}, nil)
|
|
mustStatus(t, states, sdk.StatusOK)
|
|
if !hasCode(states, "http.https_redirect.ok") {
|
|
t.Errorf("unreachable HTTP should leave redirect rule with summary OK, got %+v", states)
|
|
}
|
|
}
|