mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-05 04:56:37 +08:00
fix: retry failed discover artwork
This commit is contained in:
@@ -132,6 +132,52 @@ func TestImageProxyCachesFailedRemoteImageFetch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageProxyRemoveFailedAllowsRetry(t *testing.T) {
|
||||
var calls int32
|
||||
proxy := NewImageProxy(&config.Config{Cache: config.CacheConfig{CacheDir: filepath.Join(t.TempDir(), "cache")}}, zap.NewNop())
|
||||
proxy.client = &http.Client{Transport: imageRoundTripFunc(func(req *http.Request) (*http.Response, error) {
|
||||
call := atomic.AddInt32(&calls, 1)
|
||||
if call == 1 {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusBadGateway,
|
||||
Status: "502 Bad Gateway",
|
||||
Header: make(http.Header),
|
||||
Body: io.NopCloser(strings.NewReader("upstream unavailable")),
|
||||
Request: req,
|
||||
}, nil
|
||||
}
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Status: "200 OK",
|
||||
Header: http.Header{"Content-Type": []string{"image/jpeg"}},
|
||||
Body: io.NopCloser(strings.NewReader("poster-bytes")),
|
||||
Request: req,
|
||||
}, nil
|
||||
})}
|
||||
|
||||
raw := "https://image.tmdb.org/t/p/w500/retry-poster.jpg"
|
||||
rec := httptest.NewRecorder()
|
||||
if err := proxy.Serve(t.Context(), rec, httptest.NewRequest(http.MethodGet, "/api/img", nil), raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rec.Body.Len() != len(transparent1x1PNG) {
|
||||
t.Fatalf("first body length = %d, want placeholder %d", rec.Body.Len(), len(transparent1x1PNG))
|
||||
}
|
||||
if err := proxy.RemoveFailed(raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec = httptest.NewRecorder()
|
||||
if err := proxy.Serve(t.Context(), rec, httptest.NewRequest(http.MethodGet, "/api/img?v=retry", nil), raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := rec.Body.String(); got != "poster-bytes" {
|
||||
t.Fatalf("retried body = %q, want poster bytes", got)
|
||||
}
|
||||
if got := atomic.LoadInt32(&calls); got != 2 {
|
||||
t.Fatalf("upstream calls = %d, want 2 after retry", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageProxyCachesCloudResolvedImage(t *testing.T) {
|
||||
var calls int32
|
||||
proxy := NewImageProxy(&config.Config{Cache: config.CacheConfig{CacheDir: filepath.Join(t.TempDir(), "cache")}}, zap.NewNop())
|
||||
|
||||
Reference in New Issue
Block a user