Files
SpotiFLAC-Mobile/go_backend/httputil_retry_test.go
T
zarzet cdc1ba0a68 feat(net): DoH fallback, uTLS pool flush on cleanup, retry hardening
- Resolve over DNS-over-HTTPS (1.1.1.1/8.8.8.8, cached, SSRF-filtered)
  when the OS resolver fails, on all transports and the uTLS dial path,
  so DNS-level ISP blocking no longer kills the connection outright
- Flush the pooled uTLS h2 conns from CloseIdleConnections so a network
  switch no longer leaves the first bypass request hanging on a dead conn
- Abort retry backoff sleeps on context cancellation
- Cap honored Retry-After at 2 minutes
- Restore the response body consumed by the 403/451 marker scan
2026-07-14 12:49:43 +07:00

52 lines
1.5 KiB
Go

package gobackend
import (
"context"
"io"
"net/http"
"strings"
"testing"
"time"
)
func TestRetryHardening(t *testing.T) {
resp := &http.Response{Header: http.Header{"Retry-After": []string{"3600"}}}
if d := getRetryAfterDuration(resp); d != maxRetryAfterDelay {
t.Fatalf("Retry-After 3600s clamped to %v, want %v", d, maxRetryAfterDelay)
}
// A 403 without ISP-blocking markers must reach the caller with a
// readable body even though the marker scan consumed the original.
client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 403,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("quota exceeded")),
Request: req,
}, nil
})}
got, err := DoRequestWithRetry(client, mustNewRequest(t, "https://example.com/x"), DefaultRetryConfig())
if err != nil || got.StatusCode != 403 {
t.Fatalf("DoRequestWithRetry = %#v/%v", got, err)
}
body, err := io.ReadAll(got.Body)
got.Body.Close()
if err != nil || string(body) != "quota exceeded" {
t.Fatalf("403 body = %q/%v, want restored body", body, err)
}
// Backoff sleeps must abort on context cancellation.
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(10 * time.Millisecond)
cancel()
}()
start := time.Now()
if err := sleepRetry(ctx, 5*time.Second); err == nil {
t.Fatal("sleepRetry ignored cancellation")
}
if time.Since(start) > time.Second {
t.Fatal("sleepRetry did not abort promptly on cancel")
}
}