feat(gitea): read retry once on 5xx GET

This commit is contained in:
Mathias Bergqvist
2026-05-04 23:04:55 +02:00
parent 39dc22ec3a
commit fb473262ba
2 changed files with 45 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
@@ -27,3 +28,37 @@ func TestClientGetsTokenInHeader(t *testing.T) {
assert.Contains(t, string(body), `"ok":true`)
assert.Equal(t, "token test-token", gotAuth)
}
func TestRetryOn5xxGetSucceedsOnSecondAttempt(t *testing.T) {
var attempts int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n := atomic.AddInt32(&attempts, 1)
if n == 1 {
http.Error(w, "boom", http.StatusServiceUnavailable)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"ok":true}`))
}))
defer srv.Close()
c := gitea.NewClient(srv.URL, "tok")
body, status, err := c.GetJSON(context.Background(), "/api/v1/test")
require.NoError(t, err)
assert.Equal(t, 200, status)
assert.Contains(t, string(body), `"ok":true`)
assert.Equal(t, int32(2), atomic.LoadInt32(&attempts))
}
func TestRetryOnPostNotRetried(t *testing.T) {
var attempts int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&attempts, 1)
http.Error(w, "boom", http.StatusServiceUnavailable)
}))
defer srv.Close()
c := gitea.NewClient(srv.URL, "tok")
_, _, _ = c.PostJSON(context.Background(), "/api/v1/test", []byte(`{}`))
assert.Equal(t, int32(1), atomic.LoadInt32(&attempts), "POST should not retry")
}