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.
This commit is contained in:
parent
c16f8a9a13
commit
bcd0dee048
13
client.go
13
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
|
// RemoveAll deletes a file. If the file is a directory, all of its descendants
|
||||||
// are recursively deleted as well.
|
// 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)
|
req, err := c.ic.NewRequest(http.MethodDelete, name, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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))
|
resp, err := c.ic.Do(req.WithContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
78
client_test.go
Normal file
78
client_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user