diff options
Diffstat (limited to 'actionpack/test/controller/flash_test.rb')
-rw-r--r-- | actionpack/test/controller/flash_test.rb | 109 |
1 files changed, 70 insertions, 39 deletions
diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index b063d769a4..dc641c19ab 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -1,11 +1,11 @@ -require 'abstract_unit' -require 'active_support/key_generator' +require "abstract_unit" +require "active_support/key_generator" class FlashTest < ActionController::TestCase class TestController < ActionController::Base def set_flash flash["that"] = "hello" - render :inline => "hello" + render inline: "hello" end def set_flash_now @@ -14,32 +14,32 @@ class FlashTest < ActionController::TestCase flash.now["foo"] ||= "err" @flashy = flash.now["that"] @flash_copy = {}.update flash - render :inline => "hello" + render inline: "hello" end def attempt_to_use_flash_now @flash_copy = {}.update flash @flashy = flash["that"] - render :inline => "hello" + render inline: "hello" end def use_flash @flash_copy = {}.update flash @flashy = flash["that"] - render :inline => "hello" + render inline: "hello" end def use_flash_and_keep_it @flash_copy = {}.update flash @flashy = flash["that"] flash.keep - render :inline => "hello" + render inline: "hello" end def use_flash_and_update_it flash.update("this" => "hello again") @flash_copy = {}.update flash - render :inline => "hello" + render inline: "hello" end def use_flash_after_reset_session @@ -49,11 +49,11 @@ class FlashTest < ActionController::TestCase @flashy_that_reset = flash["that"] flash["this"] = "good-bye" @flashy_this = flash["this"] - render :inline => "hello" + render inline: "hello" end # methods for test_sweep_after_halted_action_chain - before_action :halt_and_redir, only: 'filter_halting_action' + before_action :halt_and_redir, only: "filter_halting_action" def std_action @flash_copy = {}.update(flash) @@ -66,34 +66,34 @@ class FlashTest < ActionController::TestCase def halt_and_redir flash["foo"] = "bar" - redirect_to :action => "std_action" + redirect_to action: "std_action" @flash_copy = {}.update(flash) end def redirect_with_alert - redirect_to '/nowhere', :alert => "Beware the nowheres!" + redirect_to "/nowhere", alert: "Beware the nowheres!" end def redirect_with_notice - redirect_to '/somewhere', :notice => "Good luck in the somewheres!" + redirect_to "/somewhere", notice: "Good luck in the somewheres!" end def render_with_flash_now_alert flash.now.alert = "Beware the nowheres now!" - render :inline => "hello" + render inline: "hello" end def render_with_flash_now_notice flash.now.notice = "Good luck in the somewheres now!" - render :inline => "hello" + render inline: "hello" end def redirect_with_other_flashes - redirect_to '/wonderland', :flash => { :joyride => "Horses!" } + redirect_to "/wonderland", flash: { joyride: "Horses!" } end def redirect_with_foo_flash - redirect_to "/wonderland", :foo => 'for great justice' + redirect_to "/wonderland", foo: "for great justice" end end @@ -172,17 +172,17 @@ class FlashTest < ActionController::TestCase def test_keep_and_discard_return_values flash = ActionDispatch::Flash::FlashHash.new - flash.update(:foo => :foo_indeed, :bar => :bar_indeed) + flash.update(foo: :foo_indeed, bar: :bar_indeed) assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed assert_nil flash.discard(:unknown) # non existent key passed - assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.discard().to_hash) # nothing passed - assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.discard(nil).to_hash) # nothing passed + assert_equal({ "foo" => :foo_indeed, "bar" => :bar_indeed }, flash.discard().to_hash) # nothing passed + assert_equal({ "foo" => :foo_indeed, "bar" => :bar_indeed }, flash.discard(nil).to_hash) # nothing passed assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed assert_nil flash.keep(:unknown) # non existent key passed - assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.keep().to_hash) # nothing passed - assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.keep(nil).to_hash) # nothing passed + assert_equal({ "foo" => :foo_indeed, "bar" => :bar_indeed }, flash.keep().to_hash) # nothing passed + assert_equal({ "foo" => :foo_indeed, "bar" => :bar_indeed }, flash.keep(nil).to_hash) # nothing passed end def test_redirect_to_with_alert @@ -227,7 +227,7 @@ class FlashTest < ActionController::TestCase add_flash_types :foo end subclass_controller_with_no_flash_type = Class.new(test_controller_with_flash_type_foo) - assert subclass_controller_with_no_flash_type._flash_types.include?(:foo) + assert_includes subclass_controller_with_no_flash_type._flash_types, :foo end def test_does_not_add_flash_type_to_parent_class @@ -239,8 +239,8 @@ class FlashTest < ActionController::TestCase end class FlashIntegrationTest < ActionDispatch::IntegrationTest - SessionKey = '_myapp_session' - Generator = ActiveSupport::LegacyKeyGenerator.new('b3c631c314c0bbca50c1b2843150fe33') + SessionKey = "_myapp_session" + Generator = ActiveSupport::LegacyKeyGenerator.new("b3c631c314c0bbca50c1b2843150fe33") class TestController < ActionController::Base add_flash_types :bar @@ -256,22 +256,29 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest end def use_flash - render :inline => "flash: #{flash["that"]}" + render inline: "flash: #{flash["that"]}" end def set_bar flash[:bar] = "for great justice" head :ok end + + def set_flash_optionally + flash.now.notice = params[:flash] + if stale? etag: "abe" + render inline: "maybe flash" + end + end end def test_flash with_test_route_set do - get '/set_flash' + get "/set_flash" assert_response :success assert_equal "hello", @request.flash["that"] - get '/use_flash' + get "/use_flash" assert_response :success assert_equal "flash: hello", @response.body end @@ -279,7 +286,7 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest def test_just_using_flash_does_not_stream_a_cookie_back with_test_route_set do - get '/use_flash' + get "/use_flash" assert_response :success assert_nil @response.headers["Set-Cookie"] assert_equal "flash: ", @response.body @@ -288,25 +295,47 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest def test_setting_flash_does_not_raise_in_following_requests with_test_route_set do - env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } - get '/set_flash', env: env - get '/set_flash', env: env + env = { "action_dispatch.request.flash_hash" => ActionDispatch::Flash::FlashHash.new } + get "/set_flash", env: env + get "/set_flash", env: env end end def test_setting_flash_now_does_not_raise_in_following_requests with_test_route_set do - env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new } - get '/set_flash_now', env: env - get '/set_flash_now', env: env + env = { "action_dispatch.request.flash_hash" => ActionDispatch::Flash::FlashHash.new } + get "/set_flash_now", env: env + get "/set_flash_now", env: env end end def test_added_flash_types_method with_test_route_set do - get '/set_bar' + get "/set_bar" assert_response :success - assert_equal 'for great justice', @controller.bar + assert_equal "for great justice", @controller.bar + end + end + + def test_flash_factored_into_etag + with_test_route_set do + get "/set_flash_optionally" + no_flash_etag = response.etag + + get "/set_flash_optionally", params: { flash: "hello!" } + hello_flash_etag = response.etag + + assert_not_equal no_flash_etag, hello_flash_etag + + get "/set_flash_optionally", params: { flash: "hello!" } + another_hello_flash_etag = response.etag + + assert_equal another_hello_flash_etag, hello_flash_etag + + get "/set_flash_optionally", params: { flash: "goodbye!" } + goodbye_flash_etag = response.etag + + assert_not_equal another_hello_flash_etag, goodbye_flash_etag end end @@ -323,11 +352,13 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest def with_test_route_set with_routing do |set| set.draw do - get ':action', :to => FlashIntegrationTest::TestController + ActiveSupport::Deprecation.silence do + get ":action", to: FlashIntegrationTest::TestController + end end @app = self.class.build_app(set) do |middleware| - middleware.use ActionDispatch::Session::CookieStore, :key => SessionKey + middleware.use ActionDispatch::Session::CookieStore, key: SessionKey middleware.use ActionDispatch::Flash middleware.delete ActionDispatch::ShowExceptions end |