fix(02): write fixtures exclusively and only on successful flush (WR-01)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jakub Zych
2026-09-17 14:41:18 +02:00
parent 5994e671b3
commit 7e70afe819
5 changed files with 92 additions and 9 deletions

View File

@@ -305,6 +305,40 @@ func TestProxyDuplicateSessionName(t *testing.T) {
}
}
func TestProxyFailedSessionLeavesNoFixture(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.URL.Path == "/ok" {
_, _ = w.Write([]byte(`{"ok":true}`))
return
}
_, _ = w.Write([]byte(`{"token":"` + testJWT + `"}`))
}))
t.Cleanup(upstream.Close)
fixtures := t.TempDir()
proxy := newTestProxy(t, upstream.URL, fixtures, testRulesYAML())
srv := httptest.NewServer(proxy.Handler())
t.Cleanup(srv.Close)
doProxy(t, srv.URL, "partial", "/ok", "")
req, err := http.NewRequest(http.MethodGet, srv.URL+"/leak", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set(SessionHeader, "partial")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if err := proxy.Flush(); err == nil {
t.Fatal("failed session flush must surface the capture error")
}
if _, err := os.Stat(filepath.Join(fixtures, "nuxt", "partial.yaml")); !os.IsNotExist(err) {
t.Fatalf("partial session fixture committed: %v", err)
}
}
func newTestProxy(t *testing.T, upstream, fixtures, rulesYAML string) *Proxy {
t.Helper()
p, err := NewProxy(ProxyConfig{