package caldav import ( "context" "io" "io/ioutil" "net/http/httptest" "strings" "testing" "github.com/emersion/go-ical" ) var propFindSupportedCalendarComponentRequest = ` ` var testPropFindSupportedCalendarComponentCases = map[*Calendar][]string{ &Calendar{Path: "/user/calendars/cal"}: []string{"VEVENT"}, &Calendar{Path: "/user/calendars/cal", SupportedComponentSet: []string{"VTODO"}}: []string{"VTODO"}, &Calendar{Path: "/user/calendars/cal", SupportedComponentSet: []string{"VEVENT", "VTODO"}}: []string{"VEVENT", "VTODO"}, } func TestPropFindSupportedCalendarComponent(t *testing.T) { for calendar, expected := range testPropFindSupportedCalendarComponentCases { req := httptest.NewRequest("PROPFIND", calendar.Path, nil) req.Body = io.NopCloser(strings.NewReader(propFindSupportedCalendarComponentRequest)) req.Header.Set("Content-Type", "application/xml") w := httptest.NewRecorder() handler := Handler{Backend: testBackend{calendar: calendar}} handler.ServeHTTP(w, req) res := w.Result() defer res.Body.Close() data, err := ioutil.ReadAll(res.Body) if err != nil { t.Error(err) } resp := string(data) for _, comp := range expected { // Would be nicer to do a proper XML-decoding here, but this is probably good enough for now. if !strings.Contains(resp, comp) { t.Errorf("Expected component: %v not found in response:\n%v", comp, resp) } } } } var propFindUserPrincipal = ` ` func TestPropFindRoot(t *testing.T) { req := httptest.NewRequest("PROPFIND", "/", strings.NewReader(propFindUserPrincipal)) req.Header.Set("Content-Type", "application/xml") w := httptest.NewRecorder() calendar := &Calendar{} handler := Handler{Backend: testBackend{calendar: calendar}} handler.ServeHTTP(w, req) res := w.Result() defer res.Body.Close() data, err := ioutil.ReadAll(res.Body) if err != nil { t.Error(err) } resp := string(data) if !strings.Contains(resp, `/user/`) { t.Errorf("No user-principal returned when doing a PROPFIND against root, response:\n%s", resp) } } type testBackend struct { calendar *Calendar } func (t testBackend) Calendar(ctx context.Context) (*Calendar, error) { return t.calendar, nil } func (t testBackend) CalendarHomeSetPath(ctx context.Context) (string, error) { return "/user/calendars/", nil } func (t testBackend) CurrentUserPrincipal(ctx context.Context) (string, error) { return "/user/", nil } func (t testBackend) DeleteCalendarObject(ctx context.Context, path string) error { return nil } func (t testBackend) GetCalendarObject(ctx context.Context, path string, req *CalendarCompRequest) (*CalendarObject, error) { return nil, nil } func (t testBackend) PutCalendarObject(ctx context.Context, path string, calendar *ical.Calendar, opts *PutCalendarObjectOptions) (string, error) { return "", nil } func (t testBackend) ListCalendarObjects(ctx context.Context, req *CalendarCompRequest) ([]CalendarObject, error) { return nil, nil } func (t testBackend) QueryCalendarObjects(ctx context.Context, query *CalendarQuery) ([]CalendarObject, error) { return nil, nil }