package server import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "git.gsdistance.org/tyuXX/RUD/internal/config" "git.gsdistance.org/tyuXX/RUD/internal/store" ) func TestSingleEndpointHTMLAndJSON(t *testing.T) { cfg := config.DefaultConfig() // Configure only a single header element to verify strict element rendering cfg.Page.Elements = []config.PageElement{ { Type: "header", Title: "Custom Minimal Title", }, } st := store.New(&cfg) st.Record("rud-http", store.CheckResult{State: store.StateDown, LatencyMs: 12.3, Message: "connection refused"}) srv := New(&cfg, st) // 1. Non-configured path must return 404 req404 := httptest.NewRequest(http.MethodGet, "/other-path", nil) w404 := httptest.NewRecorder() srv.ServeHTTP(w404, req404) if w404.Code != http.StatusNotFound { t.Fatalf("expected 404 on non-endpoint path, got %d", w404.Code) } // 2. HTML request returns 200 (default for Cloudflared compatibility), renders only the header + 'Powered by R u ded?' reqHTML := httptest.NewRequest(http.MethodGet, "/", nil) wHTML := httptest.NewRecorder() srv.ServeHTTP(wHTML, reqHTML) if wHTML.Code != http.StatusOK { t.Fatalf("expected HTML status 200, got %d", wHTML.Code) } htmlBody := wHTML.Body.String() if !strings.Contains(htmlBody, "Custom Minimal Title") { t.Fatalf("expected configured header title in HTML") } if !strings.Contains(htmlBody, "Powered by") || !strings.Contains(htmlBody, "R u ded?") { t.Fatalf("expected 'Powered by R u ded?' attribution in HTML") } if strings.Contains(htmlBody, `