Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect Content-Type instead of Accept header in consume! #75

Merged
merged 5 commits into from
Aug 28, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/roar/rails/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,30 @@ def represents(format, options)
end


class UnsupportedMediaType < StandardError #:nodoc:
end


def consume!(model, options={})
format = formats.first # FIXME: i expected request.content_mime_type to do the job. copied from responder.rb. this will return the wrong format when the controller responds to :json and :xml and the Content-type is :xml (?)
content_type = request.content_type
if content_type.nil?
raise UnsupportedMediaType.new("Cannot consume input without content type")
end

format = Mime::Type.lookup(content_type).try(:symbol)

unless format
raise UnsupportedMediaType.new("Cannot consume unregistered media type '#{content_type}'")
end

parsing_method = compute_parsing_method(format)
representer = prepare_model_for(format, model, options)

representer.send(compute_parsing_method(format), incoming_string, options) # e.g. from_json("...")
if parsing_method && !representer.respond_to?(parsing_method)
raise UnsupportedMediaType.new("Cannot consume unsupported media type '#{content_type}'")
end

representer.send(parsing_method, incoming_string, options) # e.g. from_json("...")
model
end

Expand Down
3 changes: 3 additions & 0 deletions lib/roar/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module Roar
module Rails
class Railtie < ::Rails::Railtie
config.representer = ActiveSupport::OrderedOptions.new
config.action_dispatch.rescue_responses.merge!(
'Roar::Rails::ControllerAdditions::UnsupportedMediaType' => :unsupported_media_type
)

initializer "roar.set_configs" do |app|
::Roar::Representer.module_eval do
Expand Down
61 changes: 60 additions & 1 deletion test/consume_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,25 @@ class ConsumeTest < ActionController::TestCase
tests UnnamespaceSingersController

test "#consume parses incoming document and updates the model" do
post :consume_json, "{\"name\": \"Bumi\"}", :format => 'json'
@request.headers['Content-Type'] = 'application/json'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is that a "bug" in Rails AC::TestCase? Passing :format doesn't seem to set the format right (Content-type)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can fix that ourselves in Roar::Rails::TestCase#process!

post :consume_json, "{\"name\": \"Bumi\"}"
assert_equal %{#<struct Singer name="Bumi">}, @response.body
end
end

class ConsumeHalWithNoHalRespondTest < ActionController::TestCase
include Roar::Rails::TestCase

tests UnnamespaceSingersController

test "#consume parses hal document and updates the model" do
@request.headers['Content-Type'] = 'application/json+hal'
assert_raises Roar::Rails::ControllerAdditions::UnsupportedMediaType do
post :consume_json, "{\"name\": \"Bumi\"}"
end
end
end

class ConsumeWithConfigurationTest < ActionController::TestCase
include Roar::Rails::TestCase

Expand All @@ -44,9 +58,52 @@ def consume_json
tests SingersController

test "#consume uses ConsumeWithConfigurationTest::MusicianRepresenter to parse incoming document" do
@request.headers['Content-Type'] = 'application/json'
post :consume_json, %{{"called":"Bumi"}}, :format => :json
assert_equal %{#<struct Singer name="Bumi">}, @response.body
end

test "#do not consume missing content type" do
assert_raises Roar::Rails::ControllerAdditions::UnsupportedMediaType do
post :consume_json, "{\"name\": \"Bumi\"}"
end
end


test "#do not consume parses unknown content type" do
@request.headers['Content-Type'] = 'application/custom+json'
assert_raises Roar::Rails::ControllerAdditions::UnsupportedMediaType do
post :consume_json, "{\"name\": \"Bumi\"}"
end
end
end

class ConsumeHalTest < ActionController::TestCase
include Roar::Rails::TestCase

module MusicianRepresenter
include Roar::Representer::JSON::HAL
property :name
end


class SingersController < ActionController::Base
include Roar::Rails::ControllerAdditions
represents :hal, :entity => MusicianRepresenter

def consume_hal
singer = consume!(Singer.new)
render :text => singer.inspect
end
end

tests SingersController

test "#consume parses HAL document and updates the model" do
@request.headers['Content-Type'] = 'application/json+hal'
post :consume_hal, "{\"name\": \"Bumi\"}"
assert_equal %{#<struct Singer name="Bumi">}, @response.body
end
end

class ConsumeWithOptionsOverridingConfigurationTest < ActionController::TestCase
Expand All @@ -66,13 +123,15 @@ def consume_json
tests SingersController

test "#consume uses #represents config to parse incoming document" do
@request.headers['Content-Type'] = 'application/json'
post :consume_json, %{{"called":"Bumi"}}, :format => :json
assert_equal %{#<struct Singer name="Bumi">}, @response.body
end
end

class RequestBodyStringTest < ConsumeTest
test "#read rewinds before reading" do
@request.headers['Content-Type'] = 'application/json'
@request.instance_eval do
def body
incoming = super
Expand Down
4 changes: 4 additions & 0 deletions test/responder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ class MusicianController < BaseController
test "parsing uses decorating representer" do # FIXME: move to controller_test.
created_singer = nil

@request.headers['Content-Type'] = 'application/json'

put singer.to_json do
created_singer = consume!(Singer.new)
respond_with created_singer
Expand Down Expand Up @@ -325,6 +327,8 @@ class MusicianController < BaseController
test "passes options in #consume!" do
created_singer = nil

@request.headers['Content-Type'] = 'application/json'

put singer.to_json do
created_singer = consume!(Singer.new, :title => "Mr.")
respond_with created_singer
Expand Down