pat/web/web_test.go
Martin Hebnes Pedersen 946a91fcc4 Move embedded web assets to web package
The embedded filesystem was in package main, making it inaccessible to
alternative main packages that need to serve the HTTP interface. Moving
the embed directive and web handlers to a dedicated web package allows
any package to import and use the web UI functionality.

This also improves separation of concerns by isolating static file
serving, template handling, and dev server proxying in a single package,
and simplifies the API by removing the staticContent parameter from
NewHandler.

Ref #512 and #15
2025-12-30 09:59:16 +01:00

77 lines
1.6 KiB
Go

package web
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestDistHandler(t *testing.T) {
tests := []struct {
name string
path string
wantStatus int
}{
{
name: "valid asset",
path: "/dist/js/app.js",
wantStatus: http.StatusOK,
},
{
name: "non-existent asset",
path: "/dist/foobar",
wantStatus: http.StatusNotFound,
},
}
handler := DistHandler()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != tt.wantStatus {
t.Errorf("got status %d, want %d", rec.Code, tt.wantStatus)
}
})
}
}
func TestUIHandler(t *testing.T) {
tests := []struct {
name string
path string
wantStatus int
}{
{
name: "main UI",
path: "/ui",
wantStatus: http.StatusOK,
},
{
name: "config UI",
path: "/ui/config",
wantStatus: http.StatusOK,
},
{
name: "template UI",
path: "/ui/template",
wantStatus: http.StatusOK,
},
{
name: "non-existent UI page",
path: "/ui/foobar",
wantStatus: http.StatusNotFound,
},
}
handler := UIHandler("N0CALL")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != tt.wantStatus {
t.Errorf("got status %d, want %d", rec.Code, tt.wantStatus)
}
})
}
}