Init
This commit is contained in:
parent
c823102a63
commit
d37503c863
20 changed files with 4433 additions and 3 deletions
70
internal/server/server_test.go
Normal file
70
internal/server/server_test.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue