diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-08-14 22:32:40 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-08-15 12:32:02 -0700 |
commit | 1310231c15742bf7d99e2f143d88b383c32782d3 (patch) | |
tree | 7f32ae8257410a19e1b9d4b189cc65803879c8e3 /actionpack/test | |
parent | 9b552fb300c4606fe517eadaa30708e9d75498a6 (diff) | |
download | rails-1310231c15742bf7d99e2f143d88b383c32782d3.tar.gz rails-1310231c15742bf7d99e2f143d88b383c32782d3.tar.bz2 rails-1310231c15742bf7d99e2f143d88b383c32782d3.zip |
Got tests to pass with some more changes.
* request.formats is much simpler now
* For XHRs or Accept headers with a single item, we use the Accept header
* For other requests, we use params[:format] or fallback to HTML
* This is primarily to work around the fact that browsers provide completely
broken Accept headers, so we have to whitelist the few cases we can
specifically isolate and treat other requests as coming from the browser
* For APIs, we can support single-item Accept headers, which disambiguates
from the browsers
* Requests to an action that only has an XML template from the browser will
no longer find the template. This worked previously because most browsers
provide a catch-all */*, but this was mostly accidental behavior. If you
want to serve XML, either use the :xml format in links, or explicitly
specify the XML template: render "template.xml".
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/content_type_test.rb | 2 | ||||
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 51 | ||||
-rw-r--r-- | actionpack/test/controller/render_js_test.rb | 6 | ||||
-rw-r--r-- | actionpack/test/dispatch/mime_type_test.rb | 2 | ||||
-rw-r--r-- | actionpack/test/dispatch/request_test.rb | 63 | ||||
-rw-r--r-- | actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml | 1 | ||||
-rw-r--r-- | actionpack/test/lib/fixture_template.rb | 20 | ||||
-rw-r--r-- | actionpack/test/new_base/content_type_test.rb | 4 | ||||
-rw-r--r-- | actionpack/test/new_base/render_layout_test.rb | 2 | ||||
-rw-r--r-- | actionpack/test/new_base/render_rjs_test.rb | 7 | ||||
-rw-r--r-- | actionpack/test/new_base/render_template_test.rb | 2 | ||||
-rw-r--r-- | actionpack/test/template/javascript_helper_test.rb | 4 | ||||
-rw-r--r-- | actionpack/test/template/prototype_helper_test.rb | 4 | ||||
-rw-r--r-- | actionpack/test/template/render_test.rb | 2 |
14 files changed, 79 insertions, 91 deletions
diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb index 511788aec8..c249788c67 100644 --- a/actionpack/test/controller/content_type_test.rb +++ b/actionpack/test/controller/content_type_test.rb @@ -46,7 +46,7 @@ class ContentTypeController < ActionController::Base def render_default_content_types_for_respond_to respond_to do |format| format.html { render :text => "hello world!" } - format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" } + format.xml { render :action => "render_default_content_types_for_respond_to" } format.js { render :text => "hello world!" } format.rss { render :text => "hello world!", :content_type => Mime::XML } end diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 2e2dba5aae..3f00b9ba2f 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -79,29 +79,20 @@ class RespondToController < ActionController::Base end end - def custom_constant_handling - Mime::Type.register("text/x-mobile", :mobile) + Mime::Type.register("text/x-mobile", :mobile) + def custom_constant_handling respond_to do |type| type.html { render :text => "HTML" } type.mobile { render :text => "Mobile" } end - ensure - Mime::SET.delete(:mobile) - Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) } end def custom_constant_handling_without_block - Mime::Type.register("text/x-mobile", :mobile) - respond_to do |type| type.html { render :text => "HTML" } type.mobile end - - ensure - Mime::SET.delete(:mobile) - Mime.module_eval { remove_const :MOBILE if const_defined?(:MOBILE) } end def handle_any @@ -125,32 +116,24 @@ class RespondToController < ActionController::Base end end + Mime::Type.register_alias("text/html", :iphone) + def iphone_with_html_response_type - Mime::Type.register_alias("text/html", :iphone) request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone" respond_to do |type| type.html { @type = "Firefox" } type.iphone { @type = "iPhone" } end - - ensure - Mime::SET.delete(:iphone) - Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } end def iphone_with_html_response_type_without_layout - Mime::Type.register_alias("text/html", :iphone) request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" respond_to do |type| type.html { @type = "Firefox"; render :action => "iphone_with_html_response_type" } type.iphone { @type = "iPhone" ; render :action => "iphone_with_html_response_type" } end - - ensure - Mime::SET.delete(:iphone) - Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } end def rescue_action(e) @@ -213,18 +196,20 @@ class RespondToControllerTest < ActionController::TestCase def test_js_or_html @request.accept = "text/javascript, text/html" - get :js_or_html + xhr :get, :js_or_html assert_equal 'JS', @response.body - get :html_or_xml + @request.accept = "text/javascript, text/html" + xhr :get, :html_or_xml assert_equal 'HTML', @response.body - get :just_xml + @request.accept = "text/javascript, text/html" + xhr :get, :just_xml assert_response 406 end def test_json_or_yaml - get :json_or_yaml + xhr :get, :json_or_yaml assert_equal 'JSON', @response.body get :json_or_yaml, :format => 'json' @@ -246,13 +231,13 @@ class RespondToControllerTest < ActionController::TestCase def test_js_or_anything @request.accept = "text/javascript, */*" - get :js_or_html + xhr :get, :js_or_html assert_equal 'JS', @response.body - get :html_or_xml + xhr :get, :html_or_xml assert_equal 'HTML', @response.body - get :just_xml + xhr :get, :just_xml assert_equal 'XML', @response.body end @@ -291,14 +276,16 @@ class RespondToControllerTest < ActionController::TestCase end def test_with_atom_content_type + @request.accept = "" @request.env["CONTENT_TYPE"] = "application/atom+xml" - get :made_for_content_type + xhr :get, :made_for_content_type assert_equal "ATOM", @response.body end def test_with_rss_content_type + @request.accept = "" @request.env["CONTENT_TYPE"] = "application/rss+xml" - get :made_for_content_type + xhr :get, :made_for_content_type assert_equal "RSS", @response.body end @@ -795,12 +782,8 @@ class PostController < AbstractPostController protected def with_iphone - Mime::Type.register_alias("text/html", :iphone) request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone" yield - ensure - Mime::SET.delete(:iphone) - Mime.module_eval { remove_const :IPHONE if const_defined?(:IPHONE) } end end diff --git a/actionpack/test/controller/render_js_test.rb b/actionpack/test/controller/render_js_test.rb index d02fd3fd4c..bc850de733 100644 --- a/actionpack/test/controller/render_js_test.rb +++ b/actionpack/test/controller/render_js_test.rb @@ -13,7 +13,7 @@ class TestController < ActionController::Base # let's just rely on the template end - def partial + def show_partial render :partial => 'partial' end end @@ -33,7 +33,7 @@ class RenderTest < ActionController::TestCase end def test_should_render_js_partial - xhr :get, :partial, :format => 'js' + xhr :get, :show_partial, :format => 'js' assert_equal 'partial js', @response.body - end + end end
\ No newline at end of file diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index 4ea0fedb8f..0832943d4c 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -56,7 +56,7 @@ class MimeTypeTest < ActiveSupport::TestCase test "type convenience methods" do # Don't test Mime::ALL, since it Mime::ALL#html? == true - types = Mime::SET.symbols.uniq - [:all] + types = Mime::SET.symbols.uniq - [:all, :iphone] # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE types.delete_if { |type| !Mime.const_defined?(type.to_s.upcase) } diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb index b626063df4..239fda98e0 100644 --- a/actionpack/test/dispatch/request_test.rb +++ b/actionpack/test/dispatch/request_test.rb @@ -366,12 +366,12 @@ class RequestTest < ActiveSupport::TestCase end test "XMLHttpRequest" do - with_accept_header false do - request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest' - request.expects(:parameters).at_least_once.returns({}) - assert request.xhr? - assert_equal Mime::JS, request.format - end + request = stub_request 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest', + 'HTTP_ACCEPT' => + [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(",") + request.expects(:parameters).at_least_once.returns({}) + assert request.xhr? + assert_equal Mime::JS, request.format end test "content type" do @@ -420,37 +420,34 @@ class RequestTest < ActiveSupport::TestCase end test "formats with accept header" do - with_accept_header true do - request = stub_request 'HTTP_ACCEPT' => 'text/html' - request.expects(:parameters).at_least_once.returns({}) - assert_equal [ Mime::HTML ], request.formats - - request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8' - request.expects(:parameters).at_least_once.returns({}) - assert_equal with_set(Mime::XML, Mime::HTML, Mime::ALL), request.formats - end + request = stub_request 'HTTP_ACCEPT' => 'text/html' + request.expects(:parameters).at_least_once.returns({}) + assert_equal [ Mime::HTML ], request.formats - with_accept_header false do - request = stub_request - request.expects(:parameters).at_least_once.returns({ :format => :txt }) - assert_equal with_set(Mime::TEXT), request.formats - end + request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8', + 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" + request.expects(:parameters).at_least_once.returns({}) + assert_equal with_set(Mime::XML), request.formats + + request = stub_request + request.expects(:parameters).at_least_once.returns({ :format => :txt }) + assert_equal with_set(Mime::TEXT), request.formats end test "negotiate_mime" do - with_accept_header true do - request = stub_request 'HTTP_ACCEPT' => 'text/html' - request.expects(:parameters).at_least_once.returns({}) - - assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON]) - assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML]) - assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL]) - - request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8' - request.expects(:parameters).at_least_once.returns({}) - assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV]) - assert_equal Mime::CSV, request.negotiate_mime([Mime::CSV, Mime::YAML]) - end + request = stub_request 'HTTP_ACCEPT' => 'text/html', + 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" + + request.expects(:parameters).at_least_once.returns({}) + + assert_equal nil, request.negotiate_mime([Mime::XML, Mime::JSON]) + assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::HTML]) + assert_equal Mime::HTML, request.negotiate_mime([Mime::XML, Mime::ALL]) + + request = stub_request 'CONTENT_TYPE' => 'application/xml; charset=UTF-8', + 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest" + request.expects(:parameters).at_least_once.returns({}) + assert_equal Mime::XML, request.negotiate_mime([Mime::XML, Mime::CSV]) end protected diff --git a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml deleted file mode 100644 index 25dc746886..0000000000 --- a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +++ /dev/null @@ -1 +0,0 @@ -<hello>world</hello>
\ No newline at end of file diff --git a/actionpack/test/lib/fixture_template.rb b/actionpack/test/lib/fixture_template.rb index ee526b5de5..8da92180d1 100644 --- a/actionpack/test/lib/fixture_template.rb +++ b/actionpack/test/lib/fixture_template.rb @@ -4,7 +4,7 @@ module ActionView #:nodoc: super(options) @hash = hash end - + def find_templates(name, details, prefix, partial) if regexp = details_to_regexp(name, details, prefix, partial) cached(regexp) do @@ -16,26 +16,26 @@ module ActionView #:nodoc: end end end - + private - + def formats_regexp @formats_regexp ||= begin formats = Mime::SET.symbols '(?:' + formats.map { |l| "\\.#{Regexp.escape(l.to_s)}" }.join('|') + ')?' end end - + def handler_regexp e = TemplateHandlers.extensions.map{|h| "\\.#{Regexp.escape(h.to_s)}"}.join("|") - "(?:#{e})?" + "(?:#{e})" end - + def details_to_regexp(name, details, prefix, partial) path = "" path << "#{prefix}/" unless prefix.empty? path << (partial ? "_#{name}" : name) - + extensions = "" [:locales, :formats].each do |k| extensions << if exts = details[k] @@ -47,7 +47,7 @@ module ActionView #:nodoc: %r'^#{Regexp.escape(path)}#{extensions}#{handler_regexp}$' end - + # TODO: fix me # :api: plugin def path_to_details(path) @@ -56,10 +56,10 @@ module ActionView #:nodoc: partial = m[1] == '_' details = (m[2]||"").split('.').reject { |e| e.empty? } handler = Template.handler_class_for_extension(m[3]) - + format = Mime[details.last] && details.pop.to_sym locale = details.last && details.pop.to_sym - + return handler, :format => format, :locale => locale, :partial => partial end end diff --git a/actionpack/test/new_base/content_type_test.rb b/actionpack/test/new_base/content_type_test.rb index cfc03a3024..ceee508224 100644 --- a/actionpack/test/new_base/content_type_test.rb +++ b/actionpack/test/new_base/content_type_test.rb @@ -75,7 +75,7 @@ module ContentType end test "sets Content-Type as application/xml when rendering *.xml.erb" do - get "/content_type/implied/i_am_xml_erb" + get "/content_type/implied/i_am_xml_erb", "format" => "xml" assert_header "Content-Type", "application/xml; charset=utf-8" end @@ -87,7 +87,7 @@ module ContentType end test "sets Content-Type as application/xml when rendering *.xml.builder" do - get "/content_type/implied/i_am_xml_builder" + get "/content_type/implied/i_am_xml_builder", "format" => "xml" assert_header "Content-Type", "application/xml; charset=utf-8" end diff --git a/actionpack/test/new_base/render_layout_test.rb b/actionpack/test/new_base/render_layout_test.rb index 279b807a5f..933eef58e7 100644 --- a/actionpack/test/new_base/render_layout_test.rb +++ b/actionpack/test/new_base/render_layout_test.rb @@ -83,7 +83,7 @@ module ControllerLayouts testing ControllerLayouts::MismatchFormatController test "if JS is selected, an HTML template is not also selected" do - get :index + get :index, "format" => "js" assert_response "$(\"test\").omg();" end diff --git a/actionpack/test/new_base/render_rjs_test.rb b/actionpack/test/new_base/render_rjs_test.rb index bd4c87b3bf..3d3e516905 100644 --- a/actionpack/test/new_base/render_rjs_test.rb +++ b/actionpack/test/new_base/render_rjs_test.rb @@ -21,24 +21,23 @@ module RenderRjs def index_locale old_locale, I18n.locale = I18n.locale, :da end - end class TestBasic < SimpleRouteCase testing BasicController test "rendering a partial in an RJS template should pick the JS template over the HTML one" do - get :index + get :index, "format" => "js" assert_response("$(\"customer\").update(\"JS Partial\");") end test "replacing an element with a partial in an RJS template should pick the HTML template over the JS one" do - get :index_html + get :index_html, "format" => "js" assert_response("$(\"customer\").update(\"HTML Partial\");") end test "replacing an element with a partial in an RJS template with a locale should pick the localed HTML template" do - get :index_locale, :format => :js + get :index_locale, "format" => "js" assert_response("$(\"customer\").update(\"Danish HTML Partial\");") end diff --git a/actionpack/test/new_base/render_template_test.rb b/actionpack/test/new_base/render_template_test.rb index 94ea38fc7b..967cbd07b0 100644 --- a/actionpack/test/new_base/render_template_test.rb +++ b/actionpack/test/new_base/render_template_test.rb @@ -73,7 +73,7 @@ module RenderTemplate end test "rendering a builder template" do - get :builder_template + get :builder_template, "format" => "xml" assert_response "<html>\n <p>Hello</p>\n</html>\n" end end diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 8caabfc3e1..f0f686f6e2 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -7,6 +7,10 @@ class JavaScriptHelperTest < ActionView::TestCase attr_accessor :formats, :output_buffer + def reset_formats(format) + @format = format + end + def setup super @template = self diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index acbf311212..313a769088 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -36,6 +36,10 @@ class Author::Nested < Author; end class PrototypeHelperBaseTest < ActionView::TestCase attr_accessor :formats, :output_buffer + def reset_formats(format) + @format = format + end + def setup super @template = self diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index ff65404c79..c86d5215cd 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -190,6 +190,8 @@ module RenderTestCases def test_render_missing_xml_partial_and_raise_missing_template @view.formats = [:xml] assert_raise(ActionView::MissingTemplate) { @view.render(:partial => "test/layout_for_partial") } + ensure + @view.formats = nil end def test_render_inline |