Specs for ETag headers

This commit is contained in:
Garret Alfert
2013-10-25 23:09:04 +02:00
parent 5dd1d26b20
commit 189d202d66
3 changed files with 87 additions and 16 deletions

View File

@@ -124,8 +124,8 @@ module RemoteStorage
existing_object_size = object_size(object) existing_object_size = object_size(object)
etag = object.etag etag = object.etag
if match_requirement = server.env["HTTP_IF_MATCH"] if required_match = server.env["HTTP_IF_MATCH"]
server.halt 412 unless match_requirement == etag server.halt 412 unless required_match == etag
end end
if binary_key = object.meta["binary_key"] if binary_key = object.meta["binary_key"]

View File

@@ -44,6 +44,18 @@ describe "Directories" do
last_modified.day.must_equal now.day last_modified.day.must_equal now.day
end end
it "has an ETag header set" do
get "/jimmy/tasks/"
last_response.status.must_equal 200
last_response.headers["ETag"].wont_be_nil
# check that ETag stays the same
etag = last_response.headers["ETag"]
get "/jimmy/tasks/"
last_response.headers["ETag"].must_equal etag
end
it "has CORS headers set" do it "has CORS headers set" do
get "/jimmy/tasks/" get "/jimmy/tasks/"
@@ -55,6 +67,9 @@ describe "Directories" do
context "with sub-directories" do context "with sub-directories" do
before do before do
get "/jimmy/tasks/"
@old_etag = last_response.headers["ETag"]
put "/jimmy/tasks/home/laundry", "do the laundry" put "/jimmy/tasks/home/laundry", "do the laundry"
end end
@@ -71,6 +86,13 @@ describe "Directories" do
content["home/"].to_s.length.must_equal 13 content["home/"].to_s.length.must_equal 13
end end
it "updates the ETag of the parent directory" do
get "/jimmy/tasks/"
last_response.headers["ETag"].wont_be_nil
last_response.headers["ETag"].wont_equal @old_etag
end
context "for a different user" do context "for a different user" do
before do before do
auth = auth_bucket.new("alice:321") auth = auth_bucket.new("alice:321")
@@ -260,6 +282,13 @@ describe "Directories" do
content["tasks/"].must_be_kind_of Integer content["tasks/"].must_be_kind_of Integer
content["tasks/"].to_s.length.must_equal 13 content["tasks/"].to_s.length.must_equal 13
end end
it "has an ETag header set" do
get "/jimmy/"
last_response.status.must_equal 200
last_response.headers["ETag"].wont_be_nil
end
end end
context "for the public directory" do context "for the public directory" do
@@ -280,6 +309,13 @@ describe "Directories" do
content = JSON.parse(last_response.body) content = JSON.parse(last_response.body)
content.must_include "5apps" content.must_include "5apps"
end end
it "has an ETag header set" do
get "/jimmy/public/bookmarks/"
last_response.status.must_equal 200
last_response.headers["ETag"].wont_be_nil
end
end end
context "when directly authorized for the public directory" do context "when directly authorized for the public directory" do
@@ -449,6 +485,17 @@ describe "Directories" do
directory_bucket.get("jimmy:").wont_be_nil directory_bucket.get("jimmy:").wont_be_nil
end end
it "updates the ETag header of the parent directory" do
get "/jimmy/tasks/"
@old_etag = last_response.headers["ETag"]
delete "/jimmy/tasks/home/trash"
get "/jimmy/tasks/"
last_response.headers["ETag"].wont_be_nil
last_response.headers["ETag"].wont_equal @old_etag
end
describe "timestamps" do describe "timestamps" do
before do before do
@old_timestamp = (2.seconds.ago.to_f * 1000).to_i @old_timestamp = (2.seconds.ago.to_f * 1000).to_i

View File

@@ -32,6 +32,11 @@ describe "App with Riak backend" do
last_modified.year.must_equal now.year last_modified.year.must_equal now.year
last_modified.day.must_equal now.day last_modified.day.must_equal now.day
end end
it "has an ETag header set" do
last_response.status.must_equal 200
last_response.headers["ETag"].wont_be_nil
end
end end
describe "GET data with custom content type" do describe "GET data with custom content type" do
@@ -102,6 +107,10 @@ describe "App with Riak backend" do
data_bucket.get("jimmy:documents:bar").content_type.must_equal "text/plain; charset=utf-8" data_bucket.get("jimmy:documents:bar").content_type.must_equal "text/plain; charset=utf-8"
end end
it "sets the ETag header" do
last_response.headers["ETag"].wont_be_nil
end
it "indexes the data set" do it "indexes the data set" do
indexes = data_bucket.get("jimmy:documents:bar").indexes indexes = data_bucket.get("jimmy:documents:bar").indexes
indexes["user_id_bin"].must_be_kind_of Set indexes["user_id_bin"].must_be_kind_of Set
@@ -197,15 +206,18 @@ describe "App with Riak backend" do
describe "with existing content" do describe "with existing content" do
before do before do
put "/jimmy/documents/archive/foo", "lorem ipsum" put "/jimmy/documents/archive/foo", "lorem ipsum"
put "/jimmy/documents/archive/foo", "some awesome content"
end end
it "saves the value" do it "saves the value" do
put "/jimmy/documents/archive/foo", "some awesome content"
last_response.status.must_equal 200 last_response.status.must_equal 200
data_bucket.get("jimmy:documents/archive:foo").data.must_equal "some awesome content" data_bucket.get("jimmy:documents/archive:foo").data.must_equal "some awesome content"
end end
it "logs the operations" do it "logs the operations" do
put "/jimmy/documents/archive/foo", "some awesome content"
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 }
@@ -220,21 +232,29 @@ describe "App with Riak backend" do
update_entry.indexes["user_id_bin"].must_include "jimmy" update_entry.indexes["user_id_bin"].must_include "jimmy"
end end
describe "when no serializer is registered for the given content-type" do it "changes the ETag header" do
before do old_etag = last_response.headers["ETag"]
header "Content-Type", "text/html; charset=UTF-8" put "/jimmy/documents/archive/foo", "some awesome content"
put "/jimmy/documents/html", '<html></html>'
put "/jimmy/documents/html", '<html><body></body></html>'
end
it "saves the value" do last_response.headers["ETag"].wont_be_nil
last_response.status.must_equal 200 last_response.headers["ETag"].wont_equal old_etag
data_bucket.get("jimmy:documents:html").raw_data.must_equal "<html><body></body></html>" end
end end
it "uses the requested content type" do describe "exsting content without serializer registered for the given content-type" do
data_bucket.get("jimmy:documents:html").content_type.must_equal "text/html; charset=UTF-8" before do
end header "Content-Type", "text/html; charset=UTF-8"
put "/jimmy/documents/html", '<html></html>'
put "/jimmy/documents/html", '<html><body></body></html>'
end
it "saves the value" do
last_response.status.must_equal 200
data_bucket.get("jimmy:documents:html").raw_data.must_equal "<html><body></body></html>"
end
it "uses the requested content type" do
data_bucket.get("jimmy:documents:html").content_type.must_equal "text/html; charset=UTF-8"
end end
end end
@@ -396,6 +416,10 @@ describe "App with Riak backend" do
log_entry.indexes["user_id_bin"].must_include "jimmy" log_entry.indexes["user_id_bin"].must_include "jimmy"
end end
it "sets the ETag header" do
last_response.headers["ETag"].wont_be_nil
end
context "non-existing object" do context "non-existing object" do
before do before do
delete "/jimmy/documents/foozius" delete "/jimmy/documents/foozius"