|
| 1 | +package router |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// newStaticTestServer builds a gin engine wired exactly like the production |
| 16 | +// local-serving branch (registerStaticFileServer) over a throwaway dist dir |
| 17 | +// holding one hashed asset and an index.html. |
| 18 | +func newStaticTestServer(t *testing.T) *gin.Engine { |
| 19 | + t.Helper() |
| 20 | + gin.SetMode(gin.TestMode) |
| 21 | + |
| 22 | + dir := t.TempDir() |
| 23 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "index.html"), []byte("<!doctype html><title>app</title>"), 0o600)) |
| 24 | + require.NoError(t, os.Mkdir(filepath.Join(dir, "assets"), 0o750)) |
| 25 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "assets", "app-abc123.js"), []byte("export const x = 1;\n"), 0o600)) |
| 26 | + |
| 27 | + engine := gin.New() |
| 28 | + registerStaticFileServer(engine, dir) |
| 29 | + |
| 30 | + return engine |
| 31 | +} |
| 32 | + |
| 33 | +func getStatic(t *testing.T, engine *gin.Engine, path string) *httptest.ResponseRecorder { |
| 34 | + t.Helper() |
| 35 | + rec := httptest.NewRecorder() |
| 36 | + engine.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil)) |
| 37 | + |
| 38 | + return rec |
| 39 | +} |
| 40 | + |
| 41 | +func TestStaticFileServer(t *testing.T) { |
| 42 | + engine := newStaticTestServer(t) |
| 43 | + |
| 44 | + t.Run("existing hashed asset is served immutable", func(t *testing.T) { |
| 45 | + rec := getStatic(t, engine, "/assets/app-abc123.js") |
| 46 | + |
| 47 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 48 | + assert.Equal(t, "public, max-age=31536000, immutable", rec.Header().Get("Cache-Control")) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("missing asset is 404 and never cached as a permanent negative", func(t *testing.T) { |
| 52 | + rec := getStatic(t, engine, "/assets/missing-deadbeef.js") |
| 53 | + |
| 54 | + // 404 (not 301->HTML) so the browser module loader fails cleanly and |
| 55 | + // the SPA reloads; no-store (not immutable) so a transient rolling |
| 56 | + // deploy isn't cached as a permanent miss. |
| 57 | + assert.Equal(t, http.StatusNotFound, rec.Code) |
| 58 | + assert.Equal(t, "no-store", rec.Header().Get("Cache-Control")) |
| 59 | + assert.NotContains(t, rec.Header().Get("Cache-Control"), "immutable") |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("index.html is served revalidated", func(t *testing.T) { |
| 63 | + rec := getStatic(t, engine, "/") |
| 64 | + |
| 65 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 66 | + assert.Equal(t, "no-cache", rec.Header().Get("Cache-Control")) |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("SPA deep-link falls back to index.html with no-cache", func(t *testing.T) { |
| 70 | + rec := getStatic(t, engine, "/templates") |
| 71 | + |
| 72 | + assert.Equal(t, http.StatusOK, rec.Code) |
| 73 | + assert.Equal(t, "no-cache", rec.Header().Get("Cache-Control")) |
| 74 | + assert.Contains(t, rec.Body.String(), "<!doctype html>") |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("unknown non-asset path redirects to root", func(t *testing.T) { |
| 78 | + rec := getStatic(t, engine, "/favicon-not-there.ico") |
| 79 | + |
| 80 | + assert.Equal(t, http.StatusMovedPermanently, rec.Code) |
| 81 | + assert.Equal(t, "/", rec.Header().Get("Location")) |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("api paths are untouched by the static cache policy", func(t *testing.T) { |
| 85 | + rec := getStatic(t, engine, baseURL+"/anything") |
| 86 | + |
| 87 | + assert.Empty(t, rec.Header().Get("Cache-Control")) |
| 88 | + }) |
| 89 | +} |
0 commit comments