Files
nmon/internal/webui/webui_test.go
jleung f85c3ef781
Some checks failed
ci / lint (push) Has been cancelled
ci / test (ubuntu-24.04-arm) (push) Has been cancelled
ci / test (ubuntu-latest) (push) Has been cancelled
ci / postgres (push) Has been cancelled
ci / openapi (push) Has been cancelled
M3: Go API per UI contract, embedded UI, openapi compat check, nmon-devtools, live gate + screenshots; M4 WP1 reachability and dependency suppression
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
2026-10-22 20:34:14 +00:00

104 lines
2.9 KiB
Go

package webui
import (
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/fstest"
)
func testHandler(t *testing.T) http.Handler {
t.Helper()
big := strings.Repeat("console.log('x');\n", 200)
fsys := fstest.MapFS{
"index.html": {Data: []byte("<html>app</html>")},
"assets/a-1.js": {Data: []byte(big)},
"favicon.svg": {Data: []byte("<svg/>")},
}
api := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
_, _ = io.WriteString(w, "api:"+r.URL.Path)
})
h, err := Handler(fsys, api)
if err != nil {
t.Fatal(err)
}
return h
}
func get(h http.Handler, method, p string, hdr ...string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, p, nil)
for i := 0; i+1 < len(hdr); i += 2 {
r.Header.Set(hdr[i], hdr[i+1])
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
func TestUnknownRoutesFallBackToIndexButAPIPathsDoNot(t *testing.T) {
h := testHandler(t)
for _, p := range []string{"/", "/devices/core-sw1", "/graph/01ABC"} {
w := get(h, http.MethodGet, p)
if w.Code != http.StatusOK || w.Body.String() != "<html>app</html>" {
t.Errorf("%s: %d %q", p, w.Code, w.Body.String())
}
if cc := w.Header().Get("Cache-Control"); cc != "no-cache" {
t.Errorf("%s: index cache-control %q", p, cc)
}
}
for _, p := range []string{"/api/v1/devices", "/docs", "/openapi.json", "/metrics", "/schemas/x.json"} {
if w := get(h, http.MethodGet, p); w.Code != http.StatusTeapot {
t.Errorf("%s reached the SPA: %d", p, w.Code)
}
}
if w := get(h, http.MethodPost, "/devices"); w.Code != http.StatusTeapot {
t.Errorf("POST outside the SPA: %d", w.Code)
}
}
func TestHashedAssetsAreImmutableAndGzipped(t *testing.T) {
h := testHandler(t)
w := get(h, http.MethodGet, "/assets/a-1.js", "Accept-Encoding", "gzip, br")
if cc := w.Header().Get("Cache-Control"); !strings.Contains(cc, "immutable") {
t.Errorf("cache-control %q", cc)
}
if w.Header().Get("Content-Encoding") != "gzip" {
t.Fatal("not gzipped")
}
zr, err := gzip.NewReader(w.Body)
if err != nil {
t.Fatal(err)
}
if b, _ := io.ReadAll(zr); !strings.HasPrefix(string(b), "console.log") {
t.Errorf("bad body %q", b[:10])
}
if w := get(h, http.MethodGet, "/assets/a-1.js"); w.Header().Get("Content-Encoding") != "" {
t.Error("gzip sent without Accept-Encoding")
}
if w := get(h, http.MethodGet, "/assets/missing-2.js"); w.Code != http.StatusNotFound {
t.Errorf("missing asset: %d", w.Code)
}
}
func TestETagRevalidates(t *testing.T) {
h := testHandler(t)
tag := get(h, http.MethodGet, "/").Header().Get("ETag")
if w := get(h, http.MethodGet, "/", "If-None-Match", tag); w.Code != http.StatusNotModified {
t.Errorf("got %d", w.Code)
}
}
func TestEmbeddedPlaceholderServes(t *testing.T) {
h, err := Handler(Dist(), http.NotFoundHandler())
if err != nil {
t.Fatal(err)
}
if w := get(h, http.MethodGet, "/x/y"); w.Code != http.StatusOK {
t.Errorf("got %d", w.Code)
}
}