From bcd0dee04845d62c09cbf60c62d190a8e988f6a5 Mon Sep 17 00:00:00 2001 From: Robin van der Vleuten Date: Thu, 12 Nov 2026 12:33:43 +0100 Subject: [PATCH] webdav: add support for If-Match/If-None-Match in Client.RemoveAll Client.RemoveAll had no way to send conditional headers, so a client could not delete a resource only if it still has a known ETag. Take a *RemoveAllOptions, the same type FileSystem.RemoveAll uses on the server side. A nil value keeps the previous unconditional behavior. --- client.go | 13 ++++++++- client_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 client_test.go diff --git a/client.go b/client.go index 0c3b93e..8ec1889 100644 --- a/client.go +++ b/client.go @@ -221,12 +221,23 @@ func (c *Client) Create(ctx context.Context, name string) (io.WriteCloser, error // RemoveAll deletes a file. If the file is a directory, all of its descendants // are recursively deleted as well. -func (c *Client) RemoveAll(ctx context.Context, name string) error { +func (c *Client) RemoveAll(ctx context.Context, name string, opts *RemoveAllOptions) error { + if opts == nil { + opts = new(RemoveAllOptions) + } + req, err := c.ic.NewRequest(http.MethodDelete, name, nil) if err != nil { return err } + if opts.IfMatch.IsSet() { + req.Header.Set("If-Match", string(opts.IfMatch)) + } + if opts.IfNoneMatch.IsSet() { + req.Header.Set("If-None-Match", string(opts.IfNoneMatch)) + } + resp, err := c.ic.Do(req.WithContext(ctx)) if err != nil { return err diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..9511fdc --- /dev/null +++ b/client_test.go @@ -0,0 +1,78 @@ +package webdav + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/emersion/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) + } +}