From d4d02e3e778f5cbe6e67a438f7698f23ff8a8cb4 Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Sun, 30 Sep 2012 21:57:58 +0200 Subject: [PATCH] Fix handling of content-types that also contain the encoding --- lib/remote_storage/riak.rb | 4 ++-- spec/riak_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index 4f36699..7f5cdd4 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -36,7 +36,7 @@ module RemoteStorage object = data_bucket.get("#{user}:#{category}:#{key}") headers["Content-Type"] = object.content_type headers["Last-Modified"] = object.last_modified.to_s(:rfc822) - case object.content_type + case object.content_type[/^[^;\s]+/] when "application/json" return object.data.to_json else @@ -62,7 +62,7 @@ module RemoteStorage def put_data(user, category, key, data, content_type=nil) object = data_bucket.new("#{user}:#{category}:#{key}") object.content_type = content_type || "text/plain; charset=utf-8" - data = JSON.parse(data) if content_type == "application/json" + data = JSON.parse(data) if content_type[/^[^;\s]+/] == "application/json" if serializer_for(object.content_type) object.data = data else diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index 8b6eb3e..7209cc8 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -148,6 +148,31 @@ describe "App with Riak backend" do last_response.content_type.must_equal "text/magic" end end + + describe "with content type containing the encoding" do + before do + header "Content-Type", "application/json; charset=UTF-8" + put "/jimmy/documents/jason", '{"foo": "bar", "unhosted": 1}' + end + + it "saves the value (as JSON)" do + last_response.status.must_equal 200 + data_bucket.get("jimmy:documents:jason").data.must_be_kind_of Hash + data_bucket.get("jimmy:documents:jason").data.must_equal({"foo" => "bar", "unhosted" => 1}) + end + + it "uses the requested content type" do + data_bucket.get("jimmy:documents:jason").content_type.must_equal "application/json; charset=UTF-8" + end + + it "delivers the data correctly" do + header "Authorization", "Bearer 123" + get "/jimmy/documents/jason" + + last_response.body.must_equal '{"foo":"bar","unhosted":1}' + last_response.content_type.must_equal "application/json; charset=UTF-8" + end + end end describe "DELETE" do