harden qobuz and downloader reliability edge cases

This commit is contained in:
2026-04-21 18:54:10 +02:00
parent 0161c01a4c
commit de4e561377
8 changed files with 424 additions and 35 deletions

View File

@@ -3,6 +3,8 @@ package download
import (
"context"
"crypto/cipher"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
@@ -120,3 +122,39 @@ func TestFileDeezerEncrypted(t *testing.T) {
t.Fatalf("decrypted file mismatch")
}
}
func TestDownloaderFileTruncatedResponseRemovesPartialFile(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Length", "10")
_, _ = w.Write([]byte("abc"))
}))
defer ts.Close()
d := NewWithOptions(true, false)
out := filepath.Join(t.TempDir(), "x", "a.bin")
err := d.File(context.Background(), ts.URL, out)
if err == nil || !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("expected unexpected EOF, got %v", err)
}
if _, statErr := os.Stat(out); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("expected no partial file, stat err=%v", statErr)
}
}
func TestFileDeezerEncryptedTruncatedResponseRemovesPartialFile(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Length", "4096")
_, _ = w.Write([]byte("short"))
}))
defer ts.Close()
d := NewWithOptions(true, false)
out := filepath.Join(t.TempDir(), "x", "a.flac")
err := d.FileDeezerEncrypted(context.Background(), ts.URL, out, "3135556")
if err == nil || !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("expected unexpected EOF, got %v", err)
}
if _, statErr := os.Stat(out); !errors.Is(statErr, os.ErrNotExist) {
t.Fatalf("expected no partial file, stat err=%v", statErr)
}
}