From 84e8b350a3c3b9dbf4333fb21979688b3eb1f19e Mon Sep 17 00:00:00 2001 From: Patrick Toomey Date: Thu, 26 Jul 2018 10:29:57 -0600 Subject: Raises exception when respond_to called multiple times in incompatible way Nesting respond_to calls can lead to unexpected behavior, so it should be avoided. Currently, the first respond_to format match sets the content-type for the resulting response. But, if a nested respond_to occurs, it is possible to match on a different format. For example: respond_to do |outer_type| outer_type.js do respond_to do |inner_type| inner_type.html { render body: "HTML" } end end end Browsers will often include */* in their Accept headers. In the above example, such a request would result in the outer_type.js match setting the content- type of the response to text/javascript, while the inner_type.html match will cause the actual response to return "HTML". This change tries to minimize potential breakage by only raising an exception if the nested respond_to calls are in conflict with each other. So, something like the following example would not raise an exception: respond_to do |outer_type| outer_type.js do respond_to do |inner_type| inner_type.js { render body: "JS" } end end end While the above is nested, it doesn't affect the content-type of the response. --- actionpack/test/controller/mime/respond_to_test.rb | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/mime/respond_to_test.rb b/actionpack/test/controller/mime/respond_to_test.rb index 771eccb29b..1163775d3c 100644 --- a/actionpack/test/controller/mime/respond_to_test.rb +++ b/actionpack/test/controller/mime/respond_to_test.rb @@ -102,6 +102,26 @@ class RespondToController < ActionController::Base end end + def using_conflicting_nested_js_then_html + respond_to do |outer_type| + outer_type.js do + respond_to do |inner_type| + inner_type.html { render body: "HTML" } + end + end + end + end + + def using_non_conflicting_nested_js_then_js + respond_to do |outer_type| + outer_type.js do + respond_to do |inner_type| + inner_type.js { render body: "JS" } + end + end + end + end + def custom_type_handling respond_to do |type| type.html { render body: "HTML" } @@ -430,6 +450,20 @@ class RespondToControllerTest < ActionController::TestCase assert_equal "

Hello world!

\n", @response.body end + def test_using_conflicting_nested_js_then_html + @request.accept = "*/*" + assert_raises(ActionController::RespondToMismatchError) do + get :using_conflicting_nested_js_then_html + end + end + + def test_using_non_conflicting_nested_js_then_js + @request.accept = "*/*" + get :using_non_conflicting_nested_js_then_js + assert_equal "text/javascript", @response.content_type + assert_equal "JS", @response.body + end + def test_with_atom_content_type @request.accept = "" @request.env["CONTENT_TYPE"] = "application/atom+xml" -- cgit v1.2.3