package server import ( "net/http" "net/http/httptest" "testing" "time" "pageswiki/internal/store" ) func TestDRStandbyGate(t *testing.T) { s := &Server{store: &store.Store{ReadOnly: true, StandbyReadUntil: time.Now().Add(time.Hour)}} h := s.withStandbyReadOnly(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) })) for _, tc := range []struct { method, path string status int }{ {"GET", "/readable-page", 200}, {"HEAD", "/readable-page", 200}, {"GET", "/health", 200}, {"GET", "/health/read", 200}, {"GET", "/health/write", 503}, {"POST", "/_/api/v1/objects", 503}, {"PUT", "/anything", 503}, {"DELETE", "/anything", 503}, {"MKCALENDAR", "/_/api/v1/caldav/new", 503}, {"PATCH", "/anything", 503}, {"GET", "/_/auth/login", 503}, {"GET", "/e/test", 503}, {"GET", "/:/compile/diagram/mermaid/x", 503}, {"GET", "/v/open-with", 503}, {"GET", "/_/api/v1/federation/manifest", 503}, {"REPORT", "/_/api/v1/caldav/", 503}, } { t.Run(tc.method+tc.path, func(t *testing.T) { w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest(tc.method, tc.path, nil)) if w.Code != tc.status { t.Fatalf("%d: %s", w.Code, w.Body.String()) } }) } s.store.StandbyReadUntil = time.Now().Add(-time.Second) w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("GET", "/readable-page", nil)) if w.Code != 503 { t.Fatal("expired snapshot served") } w = httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("GET", "/health/read", nil)) if w.Code != 503 { t.Fatal("expired snapshot ready") } } func TestDRPrimaryGatePassThrough(t *testing.T) { s := &Server{store: &store.Store{}} h := s.withStandbyReadOnly(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(202) })) w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("POST", "/anything", nil)) if w.Code != 202 { t.Fatal("primary write blocked") } }