diff --git a/caldav/client.go b/caldav/client.go index 91d7c65..d68a5aa 100644 --- a/caldav/client.go +++ b/caldav/client.go @@ -432,8 +432,11 @@ func (c *Client) GetCalendarObject(ctx context.Context, path string) (*CalendarO return co, nil } -func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.Calendar) (*CalendarObject, error) { - // TODO: add support for If-None-Match and If-Match +// PutCalendarObject stores cal as the calendar object resource at path. +func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.Calendar, opts *PutCalendarObjectOptions) (*CalendarObject, error) { + if opts == nil { + opts = new(PutCalendarObjectOptions) + } // TODO: some servers want a Content-Length header, so we can't stream the // request body here. See the Radicale issue: @@ -449,6 +452,12 @@ func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.C return nil, err } req.Header.Set("Content-Type", ical.MIMEType) + 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 { diff --git a/caldav/client_ifmatch_test.go b/caldav/client_ifmatch_test.go new file mode 100644 index 0000000..76ed771 --- /dev/null +++ b/caldav/client_ifmatch_test.go @@ -0,0 +1,88 @@ +package caldav + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/emersion/go-ical" + + "github.com/emersion/go-webdav" + "github.com/emersion/go-webdav/internal" +) + +// minimalCalendar builds a single-VEVENT VCALENDAR valid enough to encode. +func minimalCalendar() *ical.Calendar { + cal := ical.NewCalendar() + cal.Props.SetText(ical.PropVersion, "2.0") + cal.Props.SetText(ical.PropProductID, "-//go-webdav//test//EN") + ev := ical.NewEvent() + ev.Props.SetText(ical.PropUID, "test-uid") + ev.Props.SetDateTime(ical.PropDateTimeStamp, time.Date(2026, 6, 24, 0, 0, 0, 0, time.UTC)) + cal.Children = append(cal.Children, ev.Component) + return cal +} + +// TestPutCalendarObjectSendsIfMatch verifies the client forwards the +// PutCalendarObjectOptions.IfMatch ETag as an If-Match request header and +// returns the server's new ETag. +func TestPutCalendarObjectSendsIfMatch(t *testing.T) { + var gotIfMatch string + var gotMethod string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotMethod = r.Method + gotIfMatch = r.Header.Get("If-Match") + w.Header().Set("ETag", `"new-etag"`) + w.WriteHeader(http.StatusCreated) + })) + defer srv.Close() + + c, err := NewClient(http.DefaultClient, srv.URL) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + + opts := &PutCalendarObjectOptions{IfMatch: webdav.ConditionalMatch(`"old-etag"`)} + co, err := c.PutCalendarObject(context.Background(), "/cal/test-uid.ics", minimalCalendar(), opts) + if err != nil { + t.Fatalf("PutCalendarObject: %v", err) + } + if gotMethod != http.MethodPut { + t.Errorf("method = %q, want PUT", gotMethod) + } + if gotIfMatch != `"old-etag"` { + t.Errorf("If-Match header = %q, want %q", gotIfMatch, `"old-etag"`) + } + if co.ETag != "new-etag" { + t.Errorf("returned ETag = %q, want %q", co.ETag, "new-etag") + } +} + +// TestPutCalendarObjectPreconditionFailed verifies a 412 response surfaces as +// an error whose status code consumers can read via webdav.HTTPErrorCode. +func TestPutCalendarObjectPreconditionFailed(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusPreconditionFailed) + })) + defer srv.Close() + + c, err := NewClient(http.DefaultClient, srv.URL) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + opts := &PutCalendarObjectOptions{IfMatch: webdav.ConditionalMatch(`"stale"`)} + _, err = c.PutCalendarObject(context.Background(), "/cal/test-uid.ics", minimalCalendar(), opts) + if err == nil { + t.Fatal("PutCalendarObject: want error on 412, got nil") + } + var httpErr *internal.HTTPError + if !errors.As(err, &httpErr) { + t.Fatalf("not an HTTP error: %v", err) + } + if httpErr.Code != http.StatusPreconditionFailed { + t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusPreconditionFailed) + } +} diff --git a/carddav/client.go b/carddav/client.go index 1ca281e..c0d57fa 100644 --- a/carddav/client.go +++ b/carddav/client.go @@ -384,8 +384,11 @@ func (c *Client) GetAddressObject(ctx context.Context, path string) (*AddressObj return ao, nil } -func (c *Client) PutAddressObject(ctx context.Context, path string, card vcard.Card) (*AddressObject, error) { - // TODO: add support for If-None-Match and If-Match +// PutAddressObject stores card as the address object resource at path. +func (c *Client) PutAddressObject(ctx context.Context, path string, card vcard.Card, opts *PutAddressObjectOptions) (*AddressObject, error) { + if opts == nil { + opts = new(PutAddressObjectOptions) + } // TODO: some servers want a Content-Length header, so we can't stream the // request body here. See the Radicale issue: @@ -408,6 +411,12 @@ func (c *Client) PutAddressObject(ctx context.Context, path string, card vcard.C return nil, err } req.Header.Set("Content-Type", vcard.MIMEType) + 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 { diff --git a/carddav/client_ifmatch_test.go b/carddav/client_ifmatch_test.go new file mode 100644 index 0000000..5f4e475 --- /dev/null +++ b/carddav/client_ifmatch_test.go @@ -0,0 +1,81 @@ +package carddav + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/emersion/go-vcard" + + "github.com/emersion/go-webdav" + "github.com/emersion/go-webdav/internal" +) + +func minimalCard() vcard.Card { + card := make(vcard.Card) + card.SetValue(vcard.FieldFormattedName, "Alice Gopher") + card.SetValue(vcard.FieldUID, "test-uid") + vcard.ToV4(card) + return card +} + +// TestPutAddressObjectSendsIfMatch verifies the client forwards +// PutAddressObjectOptions.IfMatch as an If-Match request header and returns the +// server's new ETag. +func TestPutAddressObjectSendsIfMatch(t *testing.T) { + var gotIfMatch, gotMethod string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotMethod = r.Method + gotIfMatch = r.Header.Get("If-Match") + w.Header().Set("ETag", `"new-etag"`) + w.WriteHeader(http.StatusCreated) + })) + defer srv.Close() + + c, err := NewClient(http.DefaultClient, srv.URL) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + opts := &PutAddressObjectOptions{IfMatch: webdav.ConditionalMatch(`"old-etag"`)} + ao, err := c.PutAddressObject(context.Background(), "/contacts/test-uid.vcf", minimalCard(), opts) + if err != nil { + t.Fatalf("PutAddressObject: %v", err) + } + if gotMethod != http.MethodPut { + t.Errorf("method = %q, want PUT", gotMethod) + } + if gotIfMatch != `"old-etag"` { + t.Errorf("If-Match header = %q, want %q", gotIfMatch, `"old-etag"`) + } + if ao.ETag != "new-etag" { + t.Errorf("returned ETag = %q, want %q", ao.ETag, "new-etag") + } +} + +// TestPutAddressObjectPreconditionFailed verifies a 412 response surfaces as an +// error readable via webdav.HTTPErrorCode. +func TestPutAddressObjectPreconditionFailed(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusPreconditionFailed) + })) + defer srv.Close() + + c, err := NewClient(http.DefaultClient, srv.URL) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + opts := &PutAddressObjectOptions{IfMatch: webdav.ConditionalMatch(`"stale"`)} + _, err = c.PutAddressObject(context.Background(), "/contacts/test-uid.vcf", minimalCard(), opts) + if err == nil { + t.Fatal("PutAddressObject: want error on 412, got nil") + } + var httpErr *internal.HTTPError + if !errors.As(err, &httpErr) { + t.Fatalf("not an HTTP error: %v", err) + } + if httpErr.Code != http.StatusPreconditionFailed { + t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusPreconditionFailed) + } +}