| package webui |
|
|
| import ( |
| "net/http" |
| "net/http/httptest" |
| "os" |
| "path/filepath" |
| "strings" |
| "testing" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func TestServeFromDiskPinsContentType(t *testing.T) { |
| staticDir := t.TempDir() |
| assetsDir := filepath.Join(staticDir, "assets") |
| if err := os.MkdirAll(assetsDir, 0o755); err != nil { |
| t.Fatalf("mkdir assets: %v", err) |
| } |
|
|
| files := map[string]string{ |
| "index.html": "<!doctype html><html></html>", |
| "assets/index.css": "body{}", |
| "assets/index.js": "console.log(1)", |
| "assets/icon.svg": `<svg xmlns="http://www.w3.org/2000/svg"></svg>`, |
| "assets/source.js.map": `{"version":3}`, |
| } |
| for rel, body := range files { |
| full := filepath.Join(staticDir, filepath.FromSlash(rel)) |
| if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { |
| t.Fatalf("mkdir %s: %v", rel, err) |
| } |
| if err := os.WriteFile(full, []byte(body), 0o644); err != nil { |
| t.Fatalf("write %s: %v", rel, err) |
| } |
| } |
|
|
| h := &Handler{StaticDir: staticDir} |
|
|
| cases := []struct { |
| urlPath string |
| wantPrefix string |
| wantCacheCtl string |
| }{ |
| {"/admin/assets/index.css", "text/css", "public, max-age=31536000, immutable"}, |
| {"/admin/assets/index.js", "text/javascript", "public, max-age=31536000, immutable"}, |
| {"/admin/assets/icon.svg", "image/svg+xml", "public, max-age=31536000, immutable"}, |
| {"/admin/assets/source.js.map", "application/json", "public, max-age=31536000, immutable"}, |
| |
| |
| |
| {"/admin/", "text/html", "no-store, must-revalidate"}, |
| } |
|
|
| for _, tc := range cases { |
| t.Run(tc.urlPath, func(t *testing.T) { |
| req := httptest.NewRequest(http.MethodGet, tc.urlPath, nil) |
| rec := httptest.NewRecorder() |
| h.serveFromDisk(rec, req, staticDir) |
|
|
| if rec.Code != http.StatusOK { |
| t.Fatalf("status = %d, want 200", rec.Code) |
| } |
| ct := rec.Header().Get("Content-Type") |
| if !strings.HasPrefix(ct, tc.wantPrefix) { |
| t.Fatalf("Content-Type = %q, want prefix %q", ct, tc.wantPrefix) |
| } |
| if got := rec.Header().Get("Cache-Control"); got != tc.wantCacheCtl { |
| t.Fatalf("Cache-Control = %q, want %q", got, tc.wantCacheCtl) |
| } |
| }) |
| } |
| } |
|
|
| |
| |
| |
| |
| func TestSetStaticContentTypeUnknownExtensionFallsThrough(t *testing.T) { |
| rec := httptest.NewRecorder() |
| setStaticContentType(rec, "/tmp/data.unknownext") |
| if got := rec.Header().Get("Content-Type"); got != "" { |
| t.Fatalf("Content-Type = %q, want empty for unknown extension", got) |
| } |
| } |
|
|
| |
| |
| |
| func TestSetStaticContentTypeIsCaseInsensitive(t *testing.T) { |
| rec := httptest.NewRecorder() |
| setStaticContentType(rec, "/tmp/STYLE.CSS") |
| if got := rec.Header().Get("Content-Type"); !strings.HasPrefix(got, "text/css") { |
| t.Fatalf("Content-Type = %q, want text/css prefix", got) |
| } |
| } |
|
|