go-webdav/caldav/client_ifmatch_test.go
Henry Stern 4ce863ae71 caldav, carddav: support If-Match/If-None-Match on client PUT
PutCalendarObject and PutAddressObject sent no conditional request
headers, so a client could not perform an optimistic-concurrency write
(overwrite only if the resource still carries an expected ETag). Add an
options parameter to both, reusing the existing server-side
PutCalendarObjectOptions / PutAddressObjectOptions types so the client
and Backend interface mirror each other. A nil opts preserves the prior
unconditional behaviour; a non-nil opts sets If-Match / If-None-Match.

Signed-off-by: Henry Stern <henry@stern.ca>
Assisted-by: Claude Opus 4.8 <noreply@anthropic.com>
Closes: https://github.com/emersion/go-webdav/issues/130
2026-11-12 17:55:00 +01:00

89 lines
2.8 KiB
Go

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)
}
}