diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-02-04 06:49:26 -0800 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-02-04 06:49:26 -0800 |
commit | 776a3736e4a536ec32a82c26f39ca68cee9aeebe (patch) | |
tree | ddd7efe905185c5b0bde6a85bcf4362e703493e5 /actionpack/test | |
parent | b1faa35b523e40330b83eb474beae39404d00509 (diff) | |
parent | 3def1c8edb9062a5ca473bf32f0967daab951654 (diff) | |
download | rails-776a3736e4a536ec32a82c26f39ca68cee9aeebe.tar.gz rails-776a3736e4a536ec32a82c26f39ca68cee9aeebe.tar.bz2 rails-776a3736e4a536ec32a82c26f39ca68cee9aeebe.zip |
Merge pull request #4869 from sikachu/master-responder-fix
Fix override API response bug in respond_with
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 25 | ||||
-rw-r--r-- | actionpack/test/lib/controller/fake_models.rb | 10 |
2 files changed, 35 insertions, 0 deletions
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 60498822ff..69a8f4f213 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -593,6 +593,19 @@ class RenderJsonRespondWithController < RespondWithController format.json { render :json => RenderJsonTestException.new('boom') } end end + + def create + resource = ValidatedCustomer.new(params[:name], 1) + respond_with(resource) do |format| + format.json do + if resource.errors.empty? + render :json => { :valid => true } + else + render :json => { :valid => false } + end + end + end + end end class EmptyRespondWithController < ActionController::Base @@ -964,6 +977,18 @@ class RespondWithControllerTest < ActionController::TestCase assert_match(/"error":"RenderJsonTestException"/, @response.body) end + def test_api_response_with_valid_resource_respect_override_block + @controller = RenderJsonRespondWithController.new + post :create, :name => "sikachu", :format => :json + assert_equal '{"valid":true}', @response.body + end + + def test_api_response_with_invalid_resource_respect_override_block + @controller = RenderJsonRespondWithController.new + post :create, :name => "david", :format => :json + assert_equal '{"valid":false}', @response.body + end + def test_no_double_render_is_raised @request.accept = "text/html" assert_raise ActionView::MissingTemplate do diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb index f2362714d7..bbb4cc5ef3 100644 --- a/actionpack/test/lib/controller/fake_models.rb +++ b/actionpack/test/lib/controller/fake_models.rb @@ -34,6 +34,16 @@ end class GoodCustomer < Customer end +class ValidatedCustomer < Customer + def errors + if name =~ /Sikachu/i + [] + else + [{:name => "is invalid"}] + end + end +end + module Quiz class Question < Struct.new(:name, :id) extend ActiveModel::Naming |