mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package download
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestDownloaderFile(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write([]byte("abc123"))
|
|
}))
|
|
defer ts.Close()
|
|
|
|
d := New()
|
|
out := filepath.Join(t.TempDir(), "x", "a.bin")
|
|
if err := d.File(context.Background(), ts.URL, out); err != nil {
|
|
t.Fatalf("File() error = %v", err)
|
|
}
|
|
|
|
b, err := os.ReadFile(out)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile() error = %v", err)
|
|
}
|
|
if string(b) != "abc123" {
|
|
t.Fatalf("contents = %q, want %q", string(b), "abc123")
|
|
}
|
|
}
|
|
|
|
func TestManifestDetection(t *testing.T) {
|
|
if !isManifestResponse("application/dash+xml", []byte("x")) {
|
|
t.Fatalf("expected dash content-type to be manifest")
|
|
}
|
|
if !isManifestResponse("application/octet-stream", []byte("<?xml version='1.0'?><MPD></MPD>")) {
|
|
t.Fatalf("expected MPD XML body to be manifest")
|
|
}
|
|
if !isManifestResponse("text/plain", []byte("#EXTM3U\n#EXT-X-VERSION:3")) {
|
|
t.Fatalf("expected HLS body to be manifest")
|
|
}
|
|
if isManifestResponse("audio/flac", []byte("fLaC")) {
|
|
t.Fatalf("did not expect flac to be manifest")
|
|
}
|
|
}
|