RUD/internal/server/server_test.go

121 lines
3.9 KiB
Go

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, `<section class="banner"`) {
t.Fatalf("expected unconfigured status_banner element NOT to be rendered")
}
// 3. JSON request via Accept header returns 503 (since a monitor is down) and valid Snapshot JSON
reqJSON := httptest.NewRequest(http.MethodGet, "/", nil)
reqJSON.Header.Set("Accept", "application/json")
wJSON := httptest.NewRecorder()
srv.ServeHTTP(wJSON, reqJSON)
if wJSON.Code != http.StatusServiceUnavailable {
t.Fatalf("expected JSON status 503 when monitor is down, got %d", wJSON.Code)
}
var snap store.Snapshot
if err := json.Unmarshal(wJSON.Body.Bytes(), &snap); err != nil {
t.Fatalf("failed to unmarshal JSON response: %v", err)
}
if snap.OverallState != store.StateDown {
t.Fatalf("expected JSON status 'down', got %q", snap.OverallState)
}
}
func TestServerSecurityAndRenderingFixes(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Page.Theme.FontFamily = `"JetBrains Mono", monospace`
cfg.Page.Theme.BgColor = `#111111; } </style><script>alert(1)</script>`
cfg.Page.Elements = []config.PageElement{
{
Type: "status_banner",
Messages: map[string]string{
"down": "We're experiencing issues",
},
},
{
Type: "monitor_group",
Monitors: []string{"rud-http"},
Display: &config.MonitorDisplayConfig{
ShowMessage: config.BoolPtr(true),
},
},
}
st := store.New(&cfg) // rud-http is pending with empty LastMessage
srv := New(&cfg, st)
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 OK, got %d", w.Code)
}
body := w.Body.String()
if strings.Contains(body, "ZgotmplZ") {
t.Fatalf("unexpected ZgotmplZ in rendered HTML CSS")
}
if !strings.Contains(body, `--font: "JetBrains Mono", monospace;`) {
t.Fatalf("expected quoted FontFamily to render intact, got:\n%s", body)
}
if strings.Contains(body, `<script>alert(1)</script>`) {
t.Fatalf("CSS value breakout was not stripped")
}
if !strings.Contains(body, `We&#39;re experiencing issues`) {
t.Fatalf("expected single quote in data-messages to be HTML-attribute escaped")
}
if !strings.Contains(body, `data-field="message"`) {
t.Fatalf("expected [data-field=\"message\"] placeholder even when monitor LastMessage is empty")
}
if strings.Contains(body, `bar.innerHTML`) {
t.Fatalf("expected safe DOM construction instead of bar.innerHTML")
}
}