diff --git a/lib/remote_storage/swift.rb b/lib/remote_storage/swift.rb index 142eacb..0975f79 100644 --- a/lib/remote_storage/swift.rb +++ b/lib/remote_storage/swift.rb @@ -180,6 +180,7 @@ module RemoteStorage def delete_data(user, directory, key) url = url_for_key(user, directory, key) + not_found = false existing_metadata = redis.hgetall "rs:m:#{user}:#{directory}/#{key}" @@ -187,15 +188,22 @@ module RemoteStorage server.halt 412, "Precondition Failed" unless required_match == %Q("#{existing_metadata["e"]}") end - do_delete_request(url) + begin + do_delete_request(url) + rescue RestClient::ResourceNotFound + not_found = true + end + log_size_difference(user, existing_metadata["s"], 0) delete_metadata_objects(user, directory, key) delete_dir_objects(user, directory) - server.headers["Etag"] = %Q("#{existing_metadata["e"]}") - server.halt 200 - rescue RestClient::ResourceNotFound - server.halt 404, "Not Found" + if not_found + server.halt 404, "Not Found" + else + server.headers["Etag"] = %Q("#{existing_metadata["e"]}") + server.halt 200 + end end private diff --git a/spec/swift/app_spec.rb b/spec/swift/app_spec.rb index 5c8d24f..38df13d 100644 --- a/spec/swift/app_spec.rb +++ b/spec/swift/app_spec.rb @@ -397,14 +397,44 @@ describe "App" do last_response.headers["ETag"].must_equal "\"bla\"" end - it "returns a 404 when item doesn't exist" do - raises_exception = ->(url, headers) { raise RestClient::ResourceNotFound.new } - RestClient.stub :delete, raises_exception do - delete "/phil/food/steak" + context "when item doesn't exist" do + before do + purge_redis + + put_stub = OpenStruct.new(headers: { + etag: "bla", + last_modified: "Fri, 04 Mar 2016 12:20:18 GMT" + }) + + RestClient.stub :put, put_stub do + put "/phil/food/steak", "si" + end + + raises_exception = ->(url, headers) { raise RestClient::ResourceNotFound.new } + RestClient.stub :delete, raises_exception do + delete "/phil/food/steak" + end end - last_response.status.must_equal 404 - last_response.body.must_equal "Not Found" + it "returns a 404" do + last_response.status.must_equal 404 + last_response.body.must_equal "Not Found" + end + + it "deletes any metadata that might still exist" do + raises_exception = ->(url, headers) { raise RestClient::ResourceNotFound.new } + RestClient.stub :delete, raises_exception do + delete "/phil/food/steak" + end + + metadata = redis.hgetall "rs:m:phil:food/steak" + metadata.must_be_empty + + redis.smembers("rs:m:phil:food/:items").must_be_empty + redis.hgetall("rs:m:phil:food/").must_be_empty + + redis.smembers("rs:m:phil:/:items").must_be_empty + end end describe "If-Match header" do