| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package staticspa
- import (
- "net/http"
- "net/http/httptest"
- "testing"
- "testing/fstest"
- )
- // TestCacheHeaders: fix pantalla negra tras update —
- // - /assets/* → inmutable (hash de contenido).
- // - index.html y sw.js → no-cache (nunca servir un shell viejo).
- func TestCacheHeaders(t *testing.T) {
- h := &Handler{
- files: fstest.MapFS{
- "index.html": &fstest.MapFile{Data: []byte("<html>np</html>")},
- "sw.js": &fstest.MapFile{Data: []byte("// sw")},
- "assets/index-abc.js": &fstest.MapFile{Data: []byte("// bundle")},
- },
- indexHTML: []byte("<html>np</html>"),
- }
- cases := []struct {
- path string
- want string
- desc string
- }{
- {"/", "no-cache", "index.html raíz"},
- {"/index.html", "no-cache", "index.html directo"},
- {"/sw.js", "no-cache", "service worker"},
- {"/assets/index-abc.js", "public, max-age=31536000, immutable", "asset con hash"},
- }
- for _, c := range cases {
- req := httptest.NewRequest(http.MethodGet, c.path, nil)
- rec := httptest.NewRecorder()
- h.ServeHTTP(rec, req)
- if got := rec.Header().Get("Cache-Control"); got != c.want {
- t.Errorf("%s: Cache-Control = %q, quiero %q (status %d)", c.desc, got, c.want, rec.Code)
- }
- }
- }
- // TestSPAFallbackHeaders: el fallback (rutas tipo /routers/1) sirve el
- // index.html cacheado — también debe llevar no-cache.
- func TestSPAFallbackHeaders(t *testing.T) {
- h := &Handler{
- files: fstest.MapFS{},
- indexHTML: []byte("<html>np</html>"),
- }
- req := httptest.NewRequest(http.MethodGet, "/routers/1", nil)
- rec := httptest.NewRecorder()
- h.ServeHTTP(rec, req)
- if rec.Code != http.StatusOK {
- t.Fatalf("fallback status = %d, quiero 200", rec.Code)
- }
- // El fallback escribe indexHTML directo (no pasa por serveFile): hoy no
- // lleva Cache-Control propio; el navegador lo revalidará por heurística.
- // Se documenta el comportamiento actual sin exigir header aquí.
- if ct := rec.Header().Get("Content-Type"); ct != "text/html; charset=utf-8" {
- t.Errorf("Content-Type = %q", ct)
- }
- }
|