release / go (push) Successful in 1m48s
release / web (push) Successful in 1m19s
release / integration (push) Successful in 2m38s
release / govulncheck (push) Successful in 20s
release / android (push) Successful in 4m29s
release / Build signed APK (push) Successful in 4m57s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
Without the option a 401 or 403 already fell to the default branch, which answers ErrTransient without a retry, the same as the option's own branch. The only difference was "(auth)" in the message. TheAudioDB and both Last.fm providers set it; none read the message. The reason it gave, that a bad key retries later instead of settling the row as "none", now sits on the default branch, and the client's status table checks 401 and 403. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
182 lines
5.1 KiB
Go
182 lines
5.1 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHTTPClient_GetJSON_Success(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"name":"alice"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{Name: "test"})
|
|
var got struct{ Name string }
|
|
if err := c.GetJSON(context.Background(), srv.URL, &got); err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
if got.Name != "alice" {
|
|
t.Errorf("Name = %q, want alice", got.Name)
|
|
}
|
|
}
|
|
|
|
// A 404 settles the lookup as not found; any other failure, including a
|
|
// refused key and a body that isn't JSON, is transient and asked again later,
|
|
// so a bad or expired key never settles a row as having no picture.
|
|
func TestHTTPClient_GetJSON_AnswersNotFoundOrTransient(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
status int
|
|
body string
|
|
want error
|
|
}{
|
|
{"404", http.StatusNotFound, "", ErrNotFound},
|
|
{"401", http.StatusUnauthorized, "", ErrTransient},
|
|
{"403", http.StatusForbidden, "", ErrTransient},
|
|
{"a body that isn't JSON", http.StatusOK, "not json", ErrTransient},
|
|
} {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(tc.status)
|
|
_, _ = w.Write([]byte(tc.body))
|
|
}))
|
|
c := NewHTTPClient(HTTPOptions{Name: "test"})
|
|
var out struct{ Name string }
|
|
err := c.GetJSON(context.Background(), srv.URL, &out)
|
|
srv.Close()
|
|
if !errors.Is(err, tc.want) {
|
|
t.Errorf("%s: err = %v, want %v", tc.name, err, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHTTPClient_GetJSON_RetryOn429ThenSuccess(t *testing.T) {
|
|
var calls atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
n := calls.Add(1)
|
|
if n < 3 {
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"ok":true}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{
|
|
Name: "test",
|
|
MaxRetries: 3,
|
|
BaseBackoff: 1 * time.Millisecond, // tiny so the test is fast
|
|
})
|
|
var got struct{ OK bool }
|
|
if err := c.GetJSON(context.Background(), srv.URL, &got); err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
if calls.Load() != 3 {
|
|
t.Errorf("calls = %d, want 3", calls.Load())
|
|
}
|
|
if !got.OK {
|
|
t.Errorf("OK = false, want true")
|
|
}
|
|
}
|
|
|
|
func TestHTTPClient_GetJSON_RetryOn5xxThenExhausted(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{
|
|
Name: "test",
|
|
MaxRetries: 2,
|
|
BaseBackoff: 1 * time.Millisecond,
|
|
})
|
|
var out any
|
|
err := c.GetJSON(context.Background(), srv.URL, &out)
|
|
if !errors.Is(err, ErrTransient) {
|
|
t.Errorf("err = %v, want ErrTransient", err)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClient_GetJSON_ContextCancel(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{
|
|
Name: "test",
|
|
MaxRetries: 5,
|
|
BaseBackoff: 200 * time.Millisecond,
|
|
})
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
var out any
|
|
err := c.GetJSON(ctx, srv.URL, &out)
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Errorf("err = %v, want context.DeadlineExceeded", err)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClient_GetImage_Success(t *testing.T) {
|
|
want := []byte("\x89PNG\x0d\x0a\x1a\x0a-fake-png-bytes")
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "image/png")
|
|
_, _ = w.Write(want)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{Name: "test"})
|
|
got, err := c.GetImage(context.Background(), srv.URL)
|
|
if err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
if string(got) != string(want) {
|
|
t.Errorf("body mismatch")
|
|
}
|
|
}
|
|
|
|
func TestHTTPClient_GetImage_404IsNotFound(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{Name: "test"})
|
|
_, err := c.GetImage(context.Background(), srv.URL)
|
|
if !errors.Is(err, ErrNotFound) {
|
|
t.Errorf("err = %v, want ErrNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClient_RateLimit(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(struct{ OK bool }{true})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := NewHTTPClient(HTTPOptions{
|
|
Name: "test",
|
|
MinInterval: 50 * time.Millisecond,
|
|
})
|
|
var out struct{ OK bool }
|
|
t0 := time.Now()
|
|
if err := c.GetJSON(context.Background(), srv.URL, &out); err != nil {
|
|
t.Fatalf("first call: %v", err)
|
|
}
|
|
if err := c.GetJSON(context.Background(), srv.URL, &out); err != nil {
|
|
t.Fatalf("second call: %v", err)
|
|
}
|
|
elapsed := time.Since(t0)
|
|
if elapsed < 50*time.Millisecond {
|
|
t.Errorf("elapsed %v, want >= 50ms (rate limit)", elapsed)
|
|
}
|
|
}
|