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) } }