Skip to content

Commit

Permalink
Merge pull request #239 from MichaelEischer/empty-list
Browse files Browse the repository at this point in the history
Return empty array if there are no objects to list
  • Loading branch information
MichaelEischer committed Jun 17, 2023
2 parents ab45fb5 + ff81311 commit 3130a4b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions changelog/unreleased/issue-238
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bugfix: API: Return empty array when listing empty folders

Rest-server returned `null` when listing an empty folder. This has been changed
to returning an empty array in accordance with the REST protocol specification.
This change has no impact on restic users.

https://github.com/restic/rest-server/issues/238
https://github.com/restic/rest-server/pull/239
28 changes: 27 additions & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,30 @@ func TestResticErrorHandler(t *testing.T) {
}
}

func TestEmptyList(t *testing.T) {
mux, _, _, _, cleanup := createTestHandler(t, Server{
AppendOnly: true,
NoAuth: true,
Debug: true,
})
defer cleanup()

// create the repo
checkRequest(t, mux.ServeHTTP,
newRequest(t, "POST", "/?create=true", nil),
[]wantFunc{wantCode(http.StatusOK)})

for i := 1; i <= 2; i++ {
req := newRequest(t, "GET", "/data/", nil)
if i == 2 {
req.Header.Set("Accept", "application/vnd.x.restic.rest.v2")
}

checkRequest(t, mux.ServeHTTP, req,
[]wantFunc{wantCode(http.StatusOK), wantBody("[]")})
}
}

func TestListWithUnexpectedFiles(t *testing.T) {
mux, _, _, tempdir, cleanup := createTestHandler(t, Server{
AppendOnly: true,
Expand All @@ -398,7 +422,9 @@ func TestListWithUnexpectedFiles(t *testing.T) {

for i := 1; i <= 2; i++ {
req := newRequest(t, "GET", "/data/", nil)
req.Header.Set("Accept", "application/vnd.x.restic.rest.v2")
if i == 2 {
req.Header.Set("Accept", "application/vnd.x.restic.rest.v2")
}

checkRequest(t, mux.ServeHTTP, req,
[]wantFunc{wantCode(http.StatusOK)})
Expand Down
4 changes: 2 additions & 2 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (h *Handler) listBlobsV1(w http.ResponseWriter, r *http.Request) {
return
}

var names []string
names := []string{}
for _, i := range items {
if isHashed(objectType) {
if !i.IsDir() {
Expand Down Expand Up @@ -434,7 +434,7 @@ func (h *Handler) listBlobsV2(w http.ResponseWriter, r *http.Request) {
return
}

var blobs []Blob
blobs := []Blob{}
for _, i := range items {
if isHashed(objectType) {
if !i.IsDir() {
Expand Down

0 comments on commit 3130a4b

Please sign in to comment.