go-webdav/client_test.go
Alex Dunmow f9b91e9a35 fork: module path git.dev.alexdunmow.com/blissful/go-webdav
Fork of emersion/go-webdav v0.8.0 for BlissCRM's calendar server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 22:47:46 +08:00

79 lines
2.1 KiB
Go

package webdav
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
"git.dev.alexdunmow.com/blissful/go-webdav/internal"
)
func TestClientRemoveAll(t *testing.T) {
for _, tc := range []struct {
name string
opts *RemoveAllOptions
wantIfMatch string
wantIfNoneMatch string
}{
{name: "nil options"},
{
name: "If-Match",
opts: &RemoveAllOptions{IfMatch: `"abc"`},
wantIfMatch: `"abc"`,
},
{
name: "If-None-Match",
opts: &RemoveAllOptions{IfNoneMatch: "*"},
wantIfNoneMatch: "*",
},
} {
t.Run(tc.name, func(t *testing.T) {
var gotMethod, gotIfMatch, gotIfNoneMatch string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotMethod = r.Method
gotIfMatch = r.Header.Get("If-Match")
gotIfNoneMatch = r.Header.Get("If-None-Match")
w.WriteHeader(http.StatusNoContent)
}))
defer ts.Close()
c, err := NewClient(nil, ts.URL)
if err != nil {
t.Fatalf("NewClient() = %v", err)
}
if err := c.RemoveAll(context.Background(), "/file", tc.opts); err != nil {
t.Fatalf("RemoveAll() = %v", err)
}
if gotMethod != http.MethodDelete {
t.Errorf("method = %q, want %q", gotMethod, http.MethodDelete)
}
if gotIfMatch != tc.wantIfMatch {
t.Errorf("If-Match = %q, want %q", gotIfMatch, tc.wantIfMatch)
}
if gotIfNoneMatch != tc.wantIfNoneMatch {
t.Errorf("If-None-Match = %q, want %q", gotIfNoneMatch, tc.wantIfNoneMatch)
}
})
}
}
func TestClientRemoveAllPreconditionFailed(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusPreconditionFailed)
}))
defer ts.Close()
c, err := NewClient(nil, ts.URL)
if err != nil {
t.Fatalf("NewClient() = %v", err)
}
err = c.RemoveAll(context.Background(), "/file", &RemoveAllOptions{IfMatch: `"abc"`})
var httpErr *internal.HTTPError
if !errors.As(err, &httpErr) || httpErr.Code != http.StatusPreconditionFailed {
t.Fatalf("RemoveAll() = %v, want HTTP error with status %v", err, http.StatusPreconditionFailed)
}
}