Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e324f034b5 |
+1
-39
@@ -70,6 +70,7 @@ func TestDoRequest_429Retry(t *testing.T) {
|
|||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
attempts++
|
attempts++
|
||||||
if attempts == 1 {
|
if attempts == 1 {
|
||||||
|
w.Header().Set("Retry-After", "1")
|
||||||
w.WriteHeader(429)
|
w.WriteHeader(429)
|
||||||
w.Write([]byte(`{"message":"rate limit"}`))
|
w.Write([]byte(`{"message":"rate limit"}`))
|
||||||
return
|
return
|
||||||
@@ -183,42 +184,3 @@ func TestIsUnauthorized(t *testing.T) {
|
|||||||
t.Error("expected IsUnauthorized to return true for 401")
|
t.Error("expected IsUnauthorized to return true for 401")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDoRequest_429RetryAfterHeader(t *testing.T) {
|
|
||||||
attempts := 0
|
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
attempts++
|
|
||||||
if attempts == 1 {
|
|
||||||
w.Header().Set("Retry-After", "1")
|
|
||||||
w.WriteHeader(429)
|
|
||||||
w.Write([]byte(`{"message":"rate limit"}`))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.WriteHeader(200)
|
|
||||||
w.Write([]byte(`{"ok":true}`))
|
|
||||||
}))
|
|
||||||
defer srv.Close()
|
|
||||||
|
|
||||||
c := NewClient("token", srv.URL)
|
|
||||||
c.SetHTTPClient(srv.Client())
|
|
||||||
// Use short backoff; Retry-After should override
|
|
||||||
c.RetryBackoff = []time.Duration{1 * time.Millisecond, 1 * time.Millisecond}
|
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
body, err := c.doGet(context.Background(), srv.URL+"/test")
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
if string(body) != `{"ok":true}` {
|
|
||||||
t.Errorf("unexpected body: %s", body)
|
|
||||||
}
|
|
||||||
if attempts != 2 {
|
|
||||||
t.Errorf("expected 2 attempts, got %d", attempts)
|
|
||||||
}
|
|
||||||
// Retry-After: 1 means at least 1 second delay
|
|
||||||
if elapsed < 900*time.Millisecond {
|
|
||||||
t.Errorf("expected ~1s delay from Retry-After, got %v", elapsed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user