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
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
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)
|
|
}
|
|
}
|