Files
HouseholdBudgetBook/backend/api/static_test.go
T

141 lines
4.3 KiB
Go

package api
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func newStaticTestEngine(t *testing.T) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
dir := t.TempDir()
writeTestFile(t, filepath.Join(dir, "index.html"), "<!doctype html><div id=\"root\"></div>")
writeTestFile(t, filepath.Join(dir, "assets", "app.js"), "console.log('raw')")
writeTestFile(t, filepath.Join(dir, "assets", "app.js.br"), "br-payload")
writeTestFile(t, filepath.Join(dir, "assets", "app.js.gz"), "gz-payload")
engine := gin.New()
engine.Use(staticFileMiddleware(dir))
return engine
}
func writeTestFile(t *testing.T, path string, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", filepath.Dir(path), err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func getWithHeaders(engine *gin.Engine, target string, headers map[string]string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodGet, target, nil)
for key, value := range headers {
req.Header.Set(key, value)
}
recorder := httptest.NewRecorder()
engine.ServeHTTP(recorder, req)
return recorder
}
func TestStaticIndexIsNotCached(t *testing.T) {
recorder := getWithHeaders(newStaticTestEngine(t), "/", nil)
if recorder.Code != http.StatusOK {
t.Fatalf("GET / status = %d, want 200", recorder.Code)
}
if got := recorder.Header().Get("Cache-Control"); got != "no-cache, must-revalidate" {
t.Fatalf("Cache-Control = %q, want no-cache, must-revalidate", got)
}
if !strings.Contains(recorder.Body.String(), "id=\"root\"") {
t.Fatalf("index body = %q", recorder.Body.String())
}
}
func TestStaticAssetsAreImmutable(t *testing.T) {
recorder := getWithHeaders(newStaticTestEngine(t), "/assets/app.js", nil)
if recorder.Code != http.StatusOK {
t.Fatalf("GET /assets/app.js status = %d, want 200", recorder.Code)
}
if got := recorder.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
t.Fatalf("Cache-Control = %q, want immutable asset cache", got)
}
if got := recorder.Body.String(); got != "console.log('raw')" {
t.Fatalf("body = %q, want raw file without precompression", got)
}
if got := recorder.Header().Get("Content-Encoding"); got != "" {
t.Fatalf("Content-Encoding = %q, want empty", got)
}
}
func TestStaticServesBrotliWhenAccepted(t *testing.T) {
recorder := getWithHeaders(newStaticTestEngine(t), "/assets/app.js", map[string]string{
"Accept-Encoding": "gzip, deflate, br",
})
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", recorder.Code)
}
if got := recorder.Header().Get("Content-Encoding"); got != "br" {
t.Fatalf("Content-Encoding = %q, want br", got)
}
if got := recorder.Body.String(); got != "br-payload" {
t.Fatalf("body = %q, want brotli payload", got)
}
if got := recorder.Header().Get("Vary"); !strings.Contains(got, "Accept-Encoding") {
t.Fatalf("Vary = %q, want it to include Accept-Encoding", got)
}
}
func TestStaticFallsBackToGzip(t *testing.T) {
recorder := getWithHeaders(newStaticTestEngine(t), "/assets/app.js", map[string]string{
"Accept-Encoding": "gzip",
})
if got := recorder.Header().Get("Content-Encoding"); got != "gzip" {
t.Fatalf("Content-Encoding = %q, want gzip", got)
}
if got := recorder.Body.String(); got != "gz-payload" {
t.Fatalf("body = %q, want gzip payload", got)
}
}
func TestStaticSPAFallbackServesIndex(t *testing.T) {
recorder := getWithHeaders(newStaticTestEngine(t), "/transactions", nil)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", recorder.Code)
}
if !strings.Contains(recorder.Body.String(), "id=\"root\"") {
t.Fatalf("fallback body = %q, want index.html", recorder.Body.String())
}
}
func TestPreferredEncodingPrefersBrotli(t *testing.T) {
cases := map[string]string{
"": "",
"gzip": "gzip",
"deflate, gzip": "gzip",
"br": "br",
"gzip, br": "br",
"identity": "",
"gzip;q=1, br;q=0.5": "br",
"deflate": "",
}
for header, want := range cases {
if got := preferredEncoding(header); got != want {
t.Errorf("preferredEncoding(%q) = %q, want %q", header, got, want)
}
}
}