From a15635ce38bfba63db9c31c56da5da5ea0d83144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Tue, 3 Mar 2015 21:56:19 +0100 Subject: [PATCH 1/2] Fix a bug when a document has an empty body Add check on content length for an empty file --- lib/remote_storage/riak.rb | 5 ++++- spec/riak_spec.rb | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index 1add990..9b3c2c2 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -68,7 +68,10 @@ module RemoteStorage when "application/json" return object.data.to_json else - return serializer_for(object.content_type) ? object.data : object.raw_data + data = serializer_for(object.content_type) ? object.data : object.raw_data + + # Never return nil, always turn data into a string + return data.nil? ? '' : data end rescue ::Riak::HTTPFailedRequest server.halt 404 diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index d5e2183..4865e38 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -76,6 +76,7 @@ describe "App with Riak backend" do it "returns an empty body" do last_response.status.must_equal 200 last_response.body.must_equal "" + last_response.headers["Content-Length"].must_equal '0' end end end From 3851a88c810386c80d22cba683e2b630f5bde415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Fri, 13 Mar 2015 10:42:48 +0100 Subject: [PATCH 2/2] Add a describe block for the public file with content Also add a comment about why it's hard to check for nil because of Rack. --- spec/riak_spec.rb | 52 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index 4865e38..ff744c3 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -34,33 +34,35 @@ describe "App with Riak backend" do end describe "GET public data" do - before do - object = data_bucket.new("jimmy:public:foo") - object.content_type = "text/plain" - object.data = "some text data" - object.store + describe "file with content" do + before do + object = data_bucket.new("jimmy:public:foo") + object.content_type = "text/plain" + object.data = "some text data" + object.store - get "/jimmy/public/foo" - end + get "/jimmy/public/foo" + end - it "returns the value on all get requests" do - last_response.status.must_equal 200 - last_response.body.must_equal "some text data" - end + it "returns the value on all get requests" do + last_response.status.must_equal 200 + last_response.body.must_equal "some text data" + end - it "has an ETag header set" do - last_response.status.must_equal 200 - last_response.headers["ETag"].wont_be_nil - end + it "has an ETag header set" do + last_response.status.must_equal 200 + last_response.headers["ETag"].wont_be_nil + end - it "has a Content-Length header set" do - last_response.status.must_equal 200 - last_response.headers["Content-Length"].must_equal "14" - end + it "has a Content-Length header set" do + last_response.status.must_equal 200 + last_response.headers["Content-Length"].must_equal "14" + end - it "has caching headers set" do - last_response.status.must_equal 200 - last_response.headers["Expires"].must_equal "0" + it "has caching headers set" do + last_response.status.must_equal 200 + last_response.headers["Expires"].must_equal "0" + end end describe "empty file" do @@ -75,7 +77,11 @@ describe "App with Riak backend" do it "returns an empty body" do last_response.status.must_equal 200 - last_response.body.must_equal "" + # Rack::MockRequest turns the body into a string. We can't use + # `last_response.body` to check for nil, because: + # >> [nil].join + # => "" + last_response.body.must_equal '' last_response.headers["Content-Length"].must_equal '0' end end