aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/mime_responds_test.rb
diff options
context:
space:
mode:
authorPrem Sichanugrist <s@sikachu.com>2012-02-03 11:47:47 -0500
committerPrem Sichanugrist <s@sikachu.com>2012-02-03 14:15:51 -0500
commit3def1c8edb9062a5ca473bf32f0967daab951654 (patch)
tree5d896f957681b58eb6f4ac10da53a74e7a106833 /actionpack/test/controller/mime_responds_test.rb
parentd709b124d1815340f4a2229b05362ea37298288f (diff)
downloadrails-3def1c8edb9062a5ca473bf32f0967daab951654.tar.gz
rails-3def1c8edb9062a5ca473bf32f0967daab951654.tar.bz2
rails-3def1c8edb9062a5ca473bf32f0967daab951654.zip
Fix override API response bug in respond_with
Default responder was only using the given respond block when user requested for HTML format, or JSON/XML format with valid resource. This fix the responder so that it will use the given block regardless of the validity of the resource. Note that in this case you'll have to check for object's validity by yourself in the controller. Fixes #4796
Diffstat (limited to 'actionpack/test/controller/mime_responds_test.rb')
-rw-r--r--actionpack/test/controller/mime_responds_test.rb25
1 files changed, 25 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