Files
goget/internal/protocol/webdav/save_resume_test.go
T

62 lines
1.6 KiB
Go

//go:build linux || freebsd
// +build linux freebsd
package webdav
import (
"net/url"
"path/filepath"
"testing"
"codeberg.org/petrbalvin/goget/internal/core"
"codeberg.org/petrbalvin/goget/internal/output"
)
// TestSaveWebDAVResume verifies the partial-progress sidecar written
// by streamWebDAVBody on context cancellation. The ETag and
// Last-Modified headers are preserved so the next --resume attempt
// can send a matching If-Range and either continue or restart
// cleanly depending on the server's view of the file.
func TestSaveWebDAVResume(t *testing.T) {
tmp := t.TempDir()
out := filepath.Join(tmp, "file.bin")
u, _ := url.Parse("webdav://host/file.bin")
req := &core.DownloadRequest{
URL: u,
Output: out,
Resume: true,
}
resumed := true
saveWebDAVResume(req, 8192, 1<<20, `"etag-abc"`, "Mon, 01 Jan 2026 00:00:00 GMT", resumed)
meta, err := output.LoadResumeMetadata(out)
if err != nil {
t.Fatalf("LoadResumeMetadata: %v", err)
}
if meta == nil {
t.Fatal("expected resume metadata, got nil")
}
if meta.Downloaded != 8192 {
t.Errorf("Downloaded = %d, want 8192", meta.Downloaded)
}
if meta.Total != 1<<20 {
t.Errorf("Total = %d, want %d", meta.Total, 1<<20)
}
if meta.ETag != `"etag-abc"` {
t.Errorf("ETag = %q", meta.ETag)
}
if meta.LastModified != "Mon, 01 Jan 2026 00:00:00 GMT" {
t.Errorf("LastModified = %q", meta.LastModified)
}
// Resume=false must short-circuit the save.
out2 := filepath.Join(tmp, "noresume.bin")
req.Resume = false
saveWebDAVResume(req, 4096, 1000, "", "", false)
if _, err := output.LoadResumeMetadata(out2); err != nil {
t.Fatalf("LoadResumeMetadata (noresume): %v", err)
}
}