Handler registered the page's five types with mime.AddExtensionType. The first call makes the mime package read and parse the system's whole type table (/etc/apache2/mime.types on macOS, ~18,000 allocations) and keep it: ~2 MB of an idle tray magpie's live heap (pprof inuse_space on a real-account tray run on mote). http.ServeContent asks the same table for any file whose Content-Type isn't set. Now assetTypes names every type the page's own files, plugin pages' files and provider icons are served as. servedAs sets it before ServeContent looks at the name, so neither the registration nor ServeContent reads the system table. The system's table (Windows' registry, which another program may change) still never decides how the page's files are read.
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package gui
|
|
|
|
import (
|
|
"io/fs"
|
|
"mime"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path"
|
|
"testing"
|
|
)
|
|
|
|
// The page's own files go out as assetTypes names them, whatever the
|
|
// system's table says (Windows' registry, which another program may have
|
|
// changed): their type is set before http.ServeContent would ask the mime
|
|
// package, which reads the system's whole table into the heap for good.
|
|
func TestAssetTypesHoldAgainstTheRegistry(t *testing.T) {
|
|
for ext := range assetTypes {
|
|
mime.AddExtensionType(ext, "application/x-other")
|
|
}
|
|
t.Cleanup(func() {
|
|
for ext, typ := range assetTypes {
|
|
mime.AddExtensionType(ext, typ)
|
|
}
|
|
})
|
|
h := Handler(nil, nil)
|
|
seen := map[string]bool{}
|
|
fs.WalkDir(staticFS(), ".", func(name string, d fs.DirEntry, err error) error {
|
|
ext := path.Ext(name)
|
|
// index.html is served as / (below); FileServer sends its own name there
|
|
if err != nil || d.IsDir() || seen[ext] || assetTypes[ext] == "" || name == "index.html" {
|
|
return err
|
|
}
|
|
seen[ext] = true
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/"+name, nil))
|
|
if got := rec.Header().Get("Content-Type"); rec.Code == http.StatusOK && got != assetTypes[ext] {
|
|
t.Errorf("%s served as %q, not %q", name, got, assetTypes[ext])
|
|
} else if rec.Code != http.StatusOK {
|
|
t.Errorf("%s: %d", name, rec.Code)
|
|
}
|
|
return nil
|
|
})
|
|
for _, ext := range []string{".js", ".css", ".svg", ".png", ".jpg", ".json", ".webm"} {
|
|
if !seen[ext] {
|
|
t.Errorf("no %s file served", ext)
|
|
}
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if got := rec.Header().Get("Content-Type"); got != "text/html; charset=utf-8" {
|
|
t.Errorf("/ served as %q", got)
|
|
}
|
|
}
|