droboticflow/cmd/api/main_test.go

42 lines
1.5 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestSPAHandlerDoesNotFallbackMissingAssetsToIndex(t *testing.T) {
distDir := t.TempDir()
if err := os.WriteFile(filepath.Join(distDir, "index.html"), []byte("<!doctype html><div id=\"root\"></div>"), 0o644); err != nil {
t.Fatalf("write index: %v", err)
}
handler := spaHandler(http.NotFoundHandler(), distDir)
assetRecorder := httptest.NewRecorder()
handler.ServeHTTP(assetRecorder, httptest.NewRequest(http.MethodGet, "/assets/missing.js", nil))
if assetRecorder.Code != http.StatusNotFound {
t.Fatalf("missing asset status = %d, want %d", assetRecorder.Code, http.StatusNotFound)
}
routeRecorder := httptest.NewRecorder()
handler.ServeHTTP(routeRecorder, httptest.NewRequest(http.MethodGet, "/monitoring", nil))
if routeRecorder.Code != http.StatusOK {
t.Fatalf("SPA route status = %d, want %d", routeRecorder.Code, http.StatusOK)
}
if routeRecorder.Header().Get("Cache-Control") != "no-store" {
t.Fatalf("SPA route cache control = %q, want no-store", routeRecorder.Header().Get("Cache-Control"))
}
rootRecorder := httptest.NewRecorder()
handler.ServeHTTP(rootRecorder, httptest.NewRequest(http.MethodGet, "/", nil))
if rootRecorder.Code != http.StatusOK {
t.Fatalf("SPA root status = %d, want %d", rootRecorder.Code, http.StatusOK)
}
if rootRecorder.Header().Get("Cache-Control") != "no-store" {
t.Fatalf("SPA root cache control = %q, want no-store", rootRecorder.Header().Get("Cache-Control"))
}
}