Fix custom content types to support arbitrary types (fixes #5)

This commit is contained in:
galfert 2012-09-14 16:23:42 +02:00
parent 956f6fd3b3
commit 15fdd7742f
2 changed files with 62 additions and 2 deletions

View File

@ -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"
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

View File

@ -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