aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
authorBen Woosley <ben.woosley@gmail.com>2013-08-17 18:45:50 -0700
committerBen Woosley <ben.woosley@gmail.com>2013-08-17 19:03:45 -0700
commitf84c6201dd592f9ef0b5e8473951bf6ceec213e6 (patch)
tree509c4639d32ff57d59cc5de65d1b65295c1f845f /actionpack/test
parent6776980b9648383c7a4c87d291d7dd8fdce5b2d8 (diff)
downloadrails-f84c6201dd592f9ef0b5e8473951bf6ceec213e6.tar.gz
rails-f84c6201dd592f9ef0b5e8473951bf6ceec213e6.tar.bz2
rails-f84c6201dd592f9ef0b5e8473951bf6ceec213e6.zip
Fail informatively in #respond_with when no appropriate #api_behavior renderer is available.
Currently if a user calls #respond_with(csvable), but has not csv renderer available, Responder will just run through the default render behavior twice, raising ActionView::MissingTemplate both times. This changes ActionController::Metal::Responder#api_behavior to check in advance whether there is a renderer available, and raise ActionController::MissingRenderer if not.
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/controller/mime/respond_with_test.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/actionpack/test/controller/mime/respond_with_test.rb b/actionpack/test/controller/mime/respond_with_test.rb
index 582050b78c..29ddbff8d4 100644
--- a/actionpack/test/controller/mime/respond_with_test.rb
+++ b/actionpack/test/controller/mime/respond_with_test.rb
@@ -107,6 +107,20 @@ class RenderJsonRespondWithController < RespondWithController
end
end
+class CsvRespondWithController < ActionController::Base
+ respond_to :csv
+
+ class RespondWithCsv
+ def to_csv
+ "c,s,v"
+ end
+ end
+
+ def index
+ respond_with(RespondWithCsv.new)
+ end
+end
+
class EmptyRespondWithController < ActionController::Base
def index
respond_with(Customer.new("david", 13))
@@ -607,6 +621,23 @@ class RespondWithControllerTest < ActionController::TestCase
RespondWithController.responder = ActionController::Responder
end
+ def test_uses_renderer_if_an_api_behavior
+ ActionController::Renderers.add :csv do |obj, options|
+ send_data obj.to_csv, type: Mime::CSV
+ end
+ @controller = CsvRespondWithController.new
+ get :index, format: 'csv'
+ assert_equal Mime::CSV, @response.content_type
+ assert_equal "c,s,v", @response.body
+ end
+
+ def test_raises_missing_renderer_if_an_api_behavior_with_no_renderer
+ @controller = CsvRespondWithController.new
+ assert_raise ActionController::MissingRenderer do
+ get :index, format: 'csv'
+ end
+ end
+
def test_error_is_raised_if_no_respond_to_is_declared_and_respond_with_is_called
@controller = EmptyRespondWithController.new
@request.accept = "*/*"