mirror of
https://github.com/la5nta/pat
synced 2026-08-07 22:32:06 -04:00
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
77 lines
1.6 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|