diff --git a/delete_status_test.go b/delete_status_test.go new file mode 100644 index 0000000..6339d82 --- /dev/null +++ b/delete_status_test.go @@ -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") + } +} diff --git a/resources.go b/resources.go index d2dbf20..5bb8c89 100644 --- a/resources.go +++ b/resources.go @@ -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) }