55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
// src/server/webapp/static_files_test.go
|
|
package webapp
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// findFirstStaticFile walks the embedded FS and returns the first file path.
|
|
func findFirstStaticFile() (webPath string, content []byte, ok bool) {
|
|
_ = fs.WalkDir(assets, "static", func(path string, d fs.DirEntry, err error) error {
|
|
if !d.IsDir() && ok == false {
|
|
data, _ := assets.ReadFile(path) // ignore err; test will skip if nil
|
|
webPath = "/" + path // e.g. /static/css/main.css
|
|
content = data
|
|
ok = true
|
|
}
|
|
return nil
|
|
})
|
|
return
|
|
}
|
|
|
|
// Requires at least one file under static/. Skips if none embedded.
|
|
func TestStaticFileServer(t *testing.T) {
|
|
webPath, wantBytes, ok := findFirstStaticFile()
|
|
if !ok {
|
|
t.Skip("no embedded static files to test")
|
|
}
|
|
|
|
//-----------------------------------------------------------------
|
|
// build router with sqlmock DB (not used in this test)
|
|
//-----------------------------------------------------------------
|
|
app, _ := newTestApp(t)
|
|
ts := httptest.NewServer(BuildRouter(app))
|
|
defer ts.Close()
|
|
|
|
res, err := http.Get(ts.URL + webPath)
|
|
if err != nil {
|
|
t.Fatalf("GET %s: %v", webPath, err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
t.Fatalf("status = %d; want 200", res.StatusCode)
|
|
}
|
|
|
|
gotBytes, _ := io.ReadAll(res.Body)
|
|
if len(gotBytes) == 0 || string(gotBytes) != string(wantBytes) {
|
|
t.Errorf("served file differs from embedded asset")
|
|
}
|
|
}
|