1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/controller/mime/accept_format_test.rb
Edouard CHIN 91b6253ade Mime::Mimes#symbols should return a always up to date reference:
- Original issue was reported in https://github.com/rails/rails/issues/38094
  and a fix attempted in https://github.com/rails/rails/pull/38126 but
  it's not the proper fix I think.

  TL;DR Is that `ActionView::Base.default_formats` holds a copy of
  mime symbols at the time ActionView::Base is loaded.
  So if you try to register mime types after ActionView Base is loaded
  then it won't work.

  ```ruby
    ActionView::Base.default_formats ||= Mime::SET.symbols # Note that this is automatically done when ActionView get loaded 22483b86a6/actionpack/lib/action_dispatch.rb (L117)

   Mime::Type.register_alias "application/xhtml+xml", :foobar
   puts ActionView::base.defaults_formats.include?(:foobar) # => false
  ```

  Same issue if you try to unregister a mime after ActionView is loaded.
  That's what was happening in the flaky test:

  ```
   Mime::Type.register_alias "application/xhtml+xml", :foobar
   ActionView::Base.default_formats ||= Mime::SET.symbols

   puts ActionView::base.defaults_formats.include?(:foobar) # => true

   Mime::Type.unregister(:foobar)
   puts ActionView::base.defaults_formats.include?(:foobar) # => true
  ```

  ### Solution

  Return a refence to `@symbols` which is updated each time a new mime is
  registered/unregistered.
2020-01-02 18:05:33 +01:00

93 lines
2.2 KiB
Ruby

# frozen_string_literal: true
require "abstract_unit"
class StarStarMimeController < ActionController::Base
layout nil
def index
render
end
end
class StarStarMimeControllerTest < ActionController::TestCase
def test_javascript_with_format
@request.accept = "text/javascript"
get :index, format: "js"
assert_match "function addition(a,b){ return a+b; }", @response.body
end
def test_javascript_with_no_format
@request.accept = "text/javascript"
get :index
assert_match "function addition(a,b){ return a+b; }", @response.body
end
def test_javascript_with_no_format_only_star_star
@request.accept = "*/*"
get :index
assert_match "function addition(a,b){ return a+b; }", @response.body
end
end
class AbstractPostController < ActionController::Base
self.view_paths = File.expand_path("../../fixtures/post_test", __dir__)
end
# For testing layouts which are set automatically
class PostController < AbstractPostController
around_action :with_iphone
def index
respond_to(:html, :iphone, :js)
end
private
def with_iphone
request.format = "iphone" if request.env["HTTP_ACCEPT"] == "text/iphone"
yield
end
end
class SuperPostController < PostController
end
class MimeControllerLayoutsTest < ActionController::TestCase
tests PostController
def setup
super
@request.host = "www.example.com"
Mime::Type.register_alias("text/html", :iphone)
end
def teardown
super
Mime::Type.unregister(:iphone)
end
def test_missing_layout_renders_properly
get :index
assert_equal '<html><div id="html">Hello Firefox</div></html>', @response.body
@request.accept = "text/iphone"
get :index
assert_equal "Hello iPhone", @response.body
end
def test_format_with_inherited_layouts
@controller = SuperPostController.new
get :index
assert_equal '<html><div id="html">Super Firefox</div></html>', @response.body
@request.accept = "text/iphone"
get :index
assert_equal '<html><div id="super_iphone">Super iPhone</div></html>', @response.body
end
def test_non_navigational_format_with_no_template_fallbacks_to_html_template_with_no_layout
get :index, format: :js
assert_equal "Hello Firefox", @response.body
end
end