Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions delete_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package disk

import (
"context"
"net/http"
"testing"
)

// The real API answers a delete with 204, or 202 for a large folder deleted
// in the background. Both mean success.
func TestDeleteResourceAcceptsTheRealStatusCodes(t *testing.T) {
for _, status := range []int{http.StatusNoContent, http.StatusAccepted, http.StatusOK} {
client := mockedHttpClient(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf("method = %s, want DELETE", r.Method)
}
w.WriteHeader(status)
if status == http.StatusAccepted {
_, _ = w.Write([]byte(`{"href": "https://cloud-api.yandex.net/v1/disk/operations/abc", "method": "GET"}`))
}
})
if err := client.DeleteResource(context.Background(), "disk:/old.txt", false); err != nil {
t.Errorf("status %d: %v", status, err)
}
}
}

func TestDeleteResourceReportsAMissingResource(t *testing.T) {
client := mockedHttpClient(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"error": "DiskNotFoundError", "description": "Resource not found."}`))
})
if err := client.DeleteResource(context.Background(), "disk:/missing.txt", false); err == nil {
t.Error("a 404 must be reported as an error")
}
}
5 changes: 3 additions & 2 deletions resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ func (c *Client) DeleteResource(ctx context.Context, path string, permanently bo
}
defer resp.Body.Close()

// Use centralized response handling
if _, err := c.handleResponse(resp, []int{200}); err != nil {
// The API answers 204 once the resource is gone, and 202 when a large
// folder is being deleted in the background. 200 is kept for proxies.
if _, err := c.handleResponse(resp, []int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}); err != nil {
return fmt.Errorf("delete request failed: %w", err)
}

Expand Down
Loading