diff --git a/backend/webdav/api/types.go b/backend/webdav/api/types.go index d49064050..5ed3dfecc 100644 --- a/backend/webdav/api/types.go +++ b/backend/webdav/api/types.go @@ -98,21 +98,19 @@ func (p *Prop) Code() int { return code } -// StatusOK examines the Status and returns an OK flag +// Parse a status of the form "HTTP/1.1 2xx" +var parseStatus2XX = regexp.MustCompile(`^HTTP/[\d.]+\s+2\d{2}`) + +// StatusOK returns true if there is at least one 2XX response. func (p *Prop) StatusOK() bool { - // Fetch status code as int - c := p.Code() - // Assume OK if no statuses received - if c == -1 { + if len(p.Status) == 0 { return true } - if c == 0 { - return false - } - if c >= 200 && c < 300 { - return true - + for _, statusStr := range p.Status { + if parseStatus2XX.MatchString(statusStr) { + return true + } } return false } diff --git a/backend/webdav/api/types_test.go b/backend/webdav/api/types_test.go new file mode 100644 index 000000000..bf9daf524 --- /dev/null +++ b/backend/webdav/api/types_test.go @@ -0,0 +1,29 @@ +package api + +import ( + "testing" +) + +func TestPropStatusOK(t *testing.T) { + tests := []struct { + name string + status []string + want bool + }{ + {"Empty status", []string{}, true}, + {"Single 200 OK", []string{"HTTP/1.1 200 OK"}, true}, + {"Single 404 Not Found", []string{"HTTP/1.1 404 Not Found"}, false}, + {"Mixed status with 404 first", []string{"HTTP/1.1 404 Not Found", "HTTP/1.1 200 OK"}, true}, + {"Mixed status with 200 first", []string{"HTTP/1.1 200 OK", "HTTP/1.1 404 Not Found"}, true}, + {"Multiple non-2xx statuses", []string{"HTTP/1.1 404 Not Found", "HTTP/1.1 403 Forbidden"}, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &Prop{Status: tt.status} + if got := p.StatusOK(); got != tt.want { + t.Errorf("Prop.StatusOK() = %v, want %v", got, tt.want) + } + }) + } +}