//go:build linux || freebsd // +build linux freebsd package dataurl import ( "bytes" "context" "net/url" "os" "path/filepath" "testing" "codeberg.org/petrbalvin/goget/internal/core" ) func TestDecodePlainText(t *testing.T) { data, err := Decode("data:text/plain,hello%20world") if err != nil { t.Fatal(err) } if string(data) != "hello world" { t.Errorf("got %q, want %q", data, "hello world") } } func TestDecodeBase64(t *testing.T) { data, err := Decode("data:text/plain;base64,SGVsbG8=") if err != nil { t.Fatal(err) } if string(data) != "Hello" { t.Errorf("got %q, want %q", data, "Hello") } } func TestDecodeEmpty(t *testing.T) { data, err := Decode("data:,") if err != nil { t.Fatal(err) } if len(data) != 0 { t.Errorf("expected empty, got %d bytes", len(data)) } } func TestDecodeNotDataURL(t *testing.T) { data, err := Decode("https://example.com") if err != nil { t.Fatal(err) } if data != nil { t.Error("expected nil for non-data URL") } } func TestDownloadPlainText(t *testing.T) { var buf bytes.Buffer proto := NewProtocol() u, _ := url.Parse("data:text/plain,test123") result, err := proto.Download(context.Background(), &core.DownloadRequest{ URL: u, Writer: &buf, }) if err != nil { t.Fatal(err) } if result.BytesDownloaded != 7 { t.Errorf("bytes = %d, want 7", result.BytesDownloaded) } if buf.String() != "test123" { t.Errorf("got %q, want %q", buf.String(), "test123") } } func TestDownloadToFile(t *testing.T) { tmp := t.TempDir() dst := filepath.Join(tmp, "out.txt") proto := NewProtocol() u, _ := url.Parse("data:text/plain,hello") _, err := proto.Download(context.Background(), &core.DownloadRequest{ URL: u, Output: dst, }) if err != nil { t.Fatal(err) } data, _ := os.ReadFile(dst) if string(data) != "hello" { t.Errorf("got %q, want %q", data, "hello") } }