Files
Blacktip/redirect_test.go
T
VeillaxandClaude Sonnet 5.5 b46e0813fe
CI / test (push) Successful in 2m11s
Release / release (push) Successful in 3m45s
Answer domain redirects from the edge, with or without Traefik
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 04:07:03 -06:00

45 lines
1.6 KiB
Go

package blacktip
import (
"net/http"
"net/http/httptest"
"testing"
"git.veillax.com/Veillax/reef/model"
)
// A redirect mapping answers from the edge itself: no proxy, relay or target is needed.
func TestRedirectMappingAnswersWithoutATarget(t *testing.T) {
b := &Blacktip{logq: make(chan model.LogEntry, 8)}
mk := func(m model.Mapping) *listener {
return &listener{b: b, kind: model.ProtoHTTP, maps: []*mapState{{b: b, m: m}}}
}
cases := []struct {
name string
m model.Mapping
uri string
code int
loc string
}{
{"temporary, no path", model.Mapping{Hostname: "old.test", RedirectTo: "https://new.test/home"}, "/a?b=1", http.StatusFound, "https://new.test/home"},
{"permanent keeps path and query", model.Mapping{Hostname: "old.test", RedirectTo: "https://new.test", RedirectPermanent: true, RedirectKeepPath: true}, "/a/b?c=d", http.StatusMovedPermanently, "https://new.test/a/b?c=d"},
}
for _, c := range cases {
r := httptest.NewRequest("GET", "http://old.test"+c.uri, nil)
w := httptest.NewRecorder()
mk(c.m).ServeHTTP(w, r)
if w.Code != c.code || w.Header().Get("Location") != c.loc {
t.Errorf("%s: got %d %q, want %d %q", c.name, w.Code, w.Header().Get("Location"), c.code, c.loc)
}
}
// a hostname nobody serves is still a 404
w := httptest.NewRecorder()
mk(cases[0].m).ServeHTTP(w, httptest.NewRequest("GET", "http://other.test/", nil))
if w.Code != http.StatusNotFound {
t.Errorf("unknown host: got %d", w.Code)
}
if e := <-b.logq; e.Result != "redirect" || e.Status != http.StatusFound {
t.Errorf("access log entry: %+v", e)
}
}