package api import ( "net/http" "net/http/httptest" "strings" "testing" "chfs-ng/internal/auth" "chfs-ng/internal/storage" ) func TestRootAndAPIDoc(t *testing.T) { s := &Server{Store: storage.Local{Root: t.TempDir()}, Auth: &auth.Manager{Guest: &auth.Account{Mask: auth.Read}}, HTMLTitle: "My Files", HTMLNotice: "`Hello`"} h := s.Handler() r := httptest.NewRecorder() h.ServeHTTP(r, httptest.NewRequest(http.MethodGet, "/", nil)) if r.Code != 200 || !strings.Contains(r.Body.String(), "My Files") || !strings.Contains(r.Body.String(), "Hello") { t.Fatalf("root: %d %s", r.Code, r.Body.String()) } r = httptest.NewRecorder() h.ServeHTTP(r, httptest.NewRequest(http.MethodGet, "/asset/api.html", nil)) if r.Code != 200 || !strings.Contains(r.Body.String(), "/chfs/files") { t.Fatalf("api doc: %d %s", r.Code, r.Body.String()) } } func TestRootEscapesPlainNotice(t *testing.T) { s := &Server{Store: storage.Local{Root: t.TempDir()}, Auth: &auth.Manager{Guest: &auth.Account{Mask: auth.Read}}, HTMLNotice: ``} r := httptest.NewRecorder() s.Handler().ServeHTTP(r, httptest.NewRequest(http.MethodGet, "/", nil)) if strings.Contains(r.Body.String(), ``) { t.Fatal("plain notice was not escaped") } }