From 15fdd7742f0874905228ce0b440229e054f32400 Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Fri, 14 Sep 2012 16:23:42 +0200 Subject: [PATCH] Fix custom content types to support arbitrary types (fixes #5) --- lib/remote_storage/riak.rb | 14 +++++++++-- spec/riak_spec.rb | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index 02289c1..1e7949a 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -25,7 +25,7 @@ module RemoteStorage when "application/json" return object.data.to_json else - return object.data + return serializer_for(object.content_type) ? object.data : object.raw_data end rescue ::Riak::HTTPFailedRequest halt 404 @@ -35,7 +35,11 @@ module RemoteStorage object = client.bucket("user_data").new("#{user}:#{category}:#{key}") object.content_type = content_type || "text/plain; charset=utf-8" data = JSON.parse(data) if content_type == "application/json" - object.data = data + if serializer_for(object.content_type) + object.data = data + else + object.raw_data = data + end object.indexes.merge!({:user_id_bin => [user]}) object.store rescue ::Riak::HTTPFailedRequest @@ -49,5 +53,11 @@ module RemoteStorage halt 404 end + private + + def serializer_for(content_type) + ::Riak::Serializers[content_type[/^[^;\s]+/]] + end + end end diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index d53d1b1..eb2015a 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -36,6 +36,27 @@ describe "App with Riak backend" do end end + describe "GET data with custom content type" do + before do + object = data_bucket.new("jimmy:public:magic") + object.content_type = "text/magic" + object.raw_data = "some text data" + object.store + end + + after do + data_bucket.delete("jimmy:public:magic") + end + + it "returns the value with the correct content type" do + get "/jimmy/public/magic" + + last_response.status.must_equal 200 + last_response.content_type.must_equal "text/magic" + last_response.body.must_equal "some text data" + end + end + describe "private data" do before do object = storage_client.bucket("user_data").new("jimmy:documents:foo") @@ -119,6 +140,35 @@ describe "App with Riak backend" do last_response.content_type.must_equal "application/json" end end + + describe "with arbitrary content type" do + before do + header "Authorization", "Bearer 123" + header "Content-Type", "text/magic" + put "/jimmy/documents/magic", "pure magic" + end + + after do + data_bucket.delete("jimmy:documents:magic") + end + + it "saves the value" do + last_response.status.must_equal 200 + data_bucket.get("jimmy:documents:magic").raw_data.must_equal "pure magic" + end + + it "uses the requested content type" do + data_bucket.get("jimmy:documents:magic").content_type.must_equal "text/magic" + end + + it "delivers the data correctly" do + header "Authorization", "Bearer 123" + get "/jimmy/documents/magic" + + last_response.body.must_equal "pure magic" + last_response.content_type.must_equal "text/magic" + end + end end describe "DELETE" do