Specs for If-Match header

This commit is contained in:
Garret Alfert
2013-10-26 03:31:54 +02:00
parent 189d202d66
commit 67435157ed

View File

@@ -239,6 +239,29 @@ describe "App with Riak backend" do
last_response.headers["ETag"].wont_be_nil last_response.headers["ETag"].wont_be_nil
last_response.headers["ETag"].wont_equal old_etag last_response.headers["ETag"].wont_equal old_etag
end end
describe "when If-Match header is set" do
it "allows the request if the header matches the current ETag" do
old_etag = last_response.headers["ETag"]
header "If-Match", old_etag
put "/jimmy/documents/archive/foo", "some awesome content"
last_response.status.must_equal 200
get "/jimmy/documents/archive/foo"
last_response.body.must_equal "some awesome content"
end
it "fails the request if the header does not match the current ETag" do
header "If-Match", "WONTMATCH"
put "/jimmy/documents/archive/foo", "some awesome content"
last_response.status.must_equal 412
get "/jimmy/documents/archive/foo"
last_response.body.must_equal "lorem ipsum"
end
end
end end
describe "exsting content without serializer registered for the given content-type" do describe "exsting content without serializer registered for the given content-type" do
@@ -396,28 +419,33 @@ describe "App with Riak backend" do
describe "DELETE" do describe "DELETE" do
before do before do
header "Authorization", "Bearer 123" header "Authorization", "Bearer 123"
delete "/jimmy/documents/foo"
end end
it "removes the key" do describe "basics" do
last_response.status.must_equal 204 before do
lambda { delete "/jimmy/documents/foo"
data_bucket.get("jimmy:documents:foo") end
}.must_raise Riak::HTTPFailedRequest
end
it "logs the operation" do it "removes the key" do
objects = [] last_response.status.must_equal 204
opslog_bucket.keys.each { |k| objects << opslog_bucket.get(k) rescue nil } lambda {
data_bucket.get("jimmy:documents:foo")
}.must_raise Riak::HTTPFailedRequest
end
log_entry = objects.select{|o| o.data["count"] == -1}.first it "logs the operation" do
log_entry.data["size"].must_equal(-22) objects = []
log_entry.data["category"].must_equal "documents" opslog_bucket.keys.each { |k| objects << opslog_bucket.get(k) rescue nil }
log_entry.indexes["user_id_bin"].must_include "jimmy"
end
it "sets the ETag header" do log_entry = objects.select{|o| o.data["count"] == -1}.first
last_response.headers["ETag"].wont_be_nil log_entry.data["size"].must_equal(-22)
log_entry.data["category"].must_equal "documents"
log_entry.indexes["user_id_bin"].must_include "jimmy"
end
it "sets the ETag header" do
last_response.headers["ETag"].wont_be_nil
end
end end
context "non-existing object" do context "non-existing object" do
@@ -425,10 +453,39 @@ describe "App with Riak backend" do
delete "/jimmy/documents/foozius" delete "/jimmy/documents/foozius"
end end
it "responds with 404" do
last_response.status.must_equal 404
end
it "doesn't log the operation" do it "doesn't log the operation" do
objects = [] objects = []
opslog_bucket.keys.each { |k| objects << opslog_bucket.get(k) rescue nil } opslog_bucket.keys.each { |k| objects << opslog_bucket.get(k) rescue nil }
objects.select{|o| o.data["count"] == -1}.size.must_equal 1 objects.select{|o| o.data["count"] == -1}.size.must_equal 0
end
end
context "when an If-Match header is given" do
it "allows the request if it matches the current ETag" do
get "/jimmy/documents/foo"
old_etag = last_response.headers["ETag"]
header "If-Match", old_etag
delete "/jimmy/documents/foo"
last_response.status.must_equal 204
get "/jimmy/documents/foo"
last_response.status.must_equal 404
end
it "fails the request if it does not match the current ETag" do
header "If-Match", "WONTMATCH"
delete "/jimmy/documents/foo"
last_response.status.must_equal 412
get "/jimmy/documents/foo"
last_response.status.must_equal 200
last_response.body.must_equal "some private text data"
end end
end end