Files
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

85 lines
2.1 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testSPAIndex = "<!doctype html><html><head></head><body>app</body></html>"
func newSPATestServer(t *testing.T, opts ...Option) *Server {
t.Helper()
s := testServer(t, 0, opts...)
assets := fstest.MapFS{
"index.html": {
Data: []byte(testSPAIndex),
},
"assets/index-abc123.js": {
Data: []byte("console.log('app');"),
},
}
s.spaFS = assets
s.spaHandler = http.FileServerFS(assets)
return s
}
func TestSPAIndexRequiresRevalidation(t *testing.T) {
s := newSPATestServer(t)
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "no-cache", w.Header().Get("Cache-Control"))
assert.Equal(t, testSPAIndex, w.Body.String())
}
func TestSPAFingerprintedAssetIsImmutable(t *testing.T) {
s := newSPATestServer(t)
req := httptest.NewRequest(
http.MethodGet, "/assets/index-abc123.js", nil,
)
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "public, max-age=31536000, immutable",
w.Header().Get("Cache-Control"))
assert.Equal(t, "console.log('app');", w.Body.String())
}
func TestSPAMissingAssetReturnsNotFound(t *testing.T) {
s := newSPATestServer(t)
req := httptest.NewRequest(
http.MethodGet, "/assets/obsolete.js", nil,
)
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, req)
require.Equal(t, http.StatusNotFound, w.Code)
assert.NotContains(t, w.Body.String(), testSPAIndex)
}
func TestSPAClientRouteFallsBackToRevalidatedIndex(t *testing.T) {
s := newSPATestServer(t, WithBasePath("/viewer"))
req := httptest.NewRequest(
http.MethodGet, "/viewer/sessions/example", nil,
)
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "no-cache", w.Header().Get("Cache-Control"))
assert.Contains(t, w.Body.String(), `<base href="/viewer/">`)
}