2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2011-04-06 21:01:03 -04:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
class FlashHashTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@hash = Flash::FlashHash.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set_get
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash[:foo] = "zomg"
|
|
|
|
assert_equal "zomg", @hash[:foo]
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_keys
|
|
|
|
assert_equal [], @hash.keys
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["foo"] = "zomg"
|
|
|
|
assert_equal ["foo"], @hash.keys
|
2011-04-06 21:01:03 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["bar"] = "zomg"
|
|
|
|
assert_equal ["foo", "bar"].sort, @hash.keys.sort
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["foo"] = "bar"
|
|
|
|
@hash.update("foo" => "baz", "hello" => "world")
|
2011-04-06 21:01:03 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "baz", @hash["foo"]
|
|
|
|
assert_equal "world", @hash["hello"]
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
2014-11-10 22:04:06 -05:00
|
|
|
def test_key
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["foo"] = "bar"
|
2014-11-10 22:04:06 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert @hash.key?("foo")
|
2014-11-10 22:04:06 -05:00
|
|
|
assert @hash.key?(:foo)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_not @hash.key?("bar")
|
2014-11-10 22:04:06 -05:00
|
|
|
assert_not @hash.key?(:bar)
|
|
|
|
end
|
|
|
|
|
2011-04-06 21:01:03 -04:00
|
|
|
def test_delete
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["foo"] = "bar"
|
|
|
|
@hash.delete "foo"
|
2011-04-06 21:01:03 -04:00
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not @hash.key?("foo")
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nil @hash["foo"]
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_hash
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["foo"] = "bar"
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "foo" => "bar" }, @hash.to_hash)
|
2011-04-06 21:01:03 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash.to_hash["zomg"] = "aaron"
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not @hash.key?("zomg")
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "foo" => "bar" }, @hash.to_hash)
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
2012-10-23 15:15:53 -04:00
|
|
|
def test_to_session_value
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["foo"] = "bar"
|
|
|
|
assert_equal({ "discard" => [], "flashes" => { "foo" => "bar" } }, @hash.to_session_value)
|
2012-10-23 15:15:53 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash.now["qux"] = 1
|
|
|
|
assert_equal({ "flashes" => { "foo" => "bar" }, "discard" => [] }, @hash.to_session_value)
|
2015-01-29 07:18:55 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash.discard("foo")
|
2016-12-24 19:59:16 -05:00
|
|
|
assert_nil(@hash.to_session_value)
|
2012-10-23 15:15:53 -04:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-12-24 19:59:16 -05:00
|
|
|
assert_nil(@hash.to_session_value)
|
2012-10-23 15:15:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_from_session_value
|
2015-01-29 17:47:03 -05:00
|
|
|
# {"session_id"=>"f8e1b8152ba7609c28bbb17ec9263ba7", "flash"=>#<ActionDispatch::Flash::FlashHash:0x00000000000000 @used=#<Set: {"farewell"}>, @closed=false, @flashes={"greeting"=>"Hello", "farewell"=>"Goodbye"}, @now=nil>}
|
2016-08-06 12:54:50 -04:00
|
|
|
rails_3_2_cookie = "BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY4ZTFiODE1MmJhNzYwOWMyOGJiYjE3ZWM5MjYzYmE3BjsAVEkiCmZsYXNoBjsARm86JUFjdGlvbkRpc3BhdGNoOjpGbGFzaDo6Rmxhc2hIYXNoCToKQHVzZWRvOghTZXQGOgpAaGFzaHsGSSINZmFyZXdlbGwGOwBUVDoMQGNsb3NlZEY6DUBmbGFzaGVzewdJIg1ncmVldGluZwY7AFRJIgpIZWxsbwY7AFRJIg1mYXJld2VsbAY7AFRJIgxHb29kYnllBjsAVDoJQG5vdzA="
|
2012-10-23 15:15:53 -04:00
|
|
|
session = Marshal.load(Base64.decode64(rails_3_2_cookie))
|
2016-08-06 12:54:50 -04:00
|
|
|
hash = Flash::FlashHash.from_session_value(session["flash"])
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "greeting" => "Hello" }, hash.to_hash)
|
2016-12-24 19:59:16 -05:00
|
|
|
assert_nil(hash.to_session_value)
|
2012-10-23 15:15:53 -04:00
|
|
|
end
|
|
|
|
|
2014-02-08 23:56:40 -05:00
|
|
|
def test_from_session_value_on_json_serializer
|
2015-01-29 17:47:03 -05:00
|
|
|
decrypted_data = "{ \"session_id\":\"d98bdf6d129618fc2548c354c161cfb5\", \"flash\":{\"discard\":[\"farewell\"], \"flashes\":{\"greeting\":\"Hello\",\"farewell\":\"Goodbye\"}} }"
|
2014-02-08 23:56:40 -05:00
|
|
|
session = ActionDispatch::Cookies::JsonSerializer.load(decrypted_data)
|
2016-08-06 12:54:50 -04:00
|
|
|
hash = Flash::FlashHash.from_session_value(session["flash"])
|
2014-02-08 23:56:40 -05:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "greeting" => "Hello" }, hash.to_hash)
|
2016-12-24 19:59:16 -05:00
|
|
|
assert_nil(hash.to_session_value)
|
2015-01-29 17:47:03 -05:00
|
|
|
assert_equal "Hello", hash[:greeting]
|
|
|
|
assert_equal "Hello", hash["greeting"]
|
2014-02-08 23:56:40 -05:00
|
|
|
end
|
|
|
|
|
2011-04-06 21:01:03 -04:00
|
|
|
def test_empty?
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @hash
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["zomg"] = "bears"
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_not_empty @hash
|
2011-04-06 21:01:03 -04:00
|
|
|
@hash.clear
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @hash
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash["foo"] = "bar"
|
2011-04-06 21:01:03 -04:00
|
|
|
|
|
|
|
things = []
|
2016-10-28 23:05:58 -04:00
|
|
|
@hash.each do |k, v|
|
|
|
|
things << [k, v]
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal([%w{ hello world }, %w{ foo bar }].sort, things.sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash.replace("omg" => "aaron")
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "omg" => "aaron" }, @hash.to_hash)
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_discard_no_args
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
2011-04-06 21:01:03 -04:00
|
|
|
@hash.discard
|
2011-12-28 21:21:35 -05:00
|
|
|
|
2011-04-06 21:01:03 -04:00
|
|
|
@hash.sweep
|
|
|
|
assert_equal({}, @hash.to_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_discard_one_arg
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash["omg"] = "world"
|
|
|
|
@hash.discard "hello"
|
2011-12-28 21:21:35 -05:00
|
|
|
|
2011-04-06 21:01:03 -04:00
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "omg" => "world" }, @hash.to_hash)
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
2011-12-28 21:21:35 -05:00
|
|
|
|
|
|
|
def test_keep_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
2011-12-28 21:21:35 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hello" => "world" }, @hash.to_hash)
|
2011-12-28 21:21:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
2016-08-06 13:44:11 -04:00
|
|
|
@hash.update("hi" => "mom")
|
2011-12-28 21:21:35 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hello" => "world", "hi" => "mom" }, @hash.to_hash)
|
2011-12-28 21:21:35 -05:00
|
|
|
end
|
|
|
|
|
2011-12-28 21:33:44 -05:00
|
|
|
def test_update_delete_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash.delete "hello"
|
2016-08-06 13:44:11 -04:00
|
|
|
@hash.update("hello" => "mom")
|
2011-12-28 21:33:44 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hello" => "mom" }, @hash.to_hash)
|
2011-12-28 21:33:44 -05:00
|
|
|
end
|
|
|
|
|
2011-12-28 21:21:35 -05:00
|
|
|
def test_delete_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash["hi"] = "mom"
|
|
|
|
@hash.delete "hi"
|
2011-12-28 21:21:35 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hello" => "world" }, @hash.to_hash)
|
2011-12-28 21:21:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
2011-12-28 21:21:35 -05:00
|
|
|
@hash.clear
|
|
|
|
|
|
|
|
@hash.sweep
|
|
|
|
assert_equal({}, @hash.to_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
2016-08-06 13:44:11 -04:00
|
|
|
@hash.replace("hi" => "mom")
|
2011-12-28 21:21:35 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hi" => "mom" }, @hash.to_hash)
|
2011-12-28 21:21:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_discard_then_add
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash["omg"] = "world"
|
|
|
|
@hash.discard "hello"
|
|
|
|
@hash["hello"] = "world"
|
2011-12-28 21:21:35 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "omg" => "world", "hello" => "world" }, @hash.to_hash)
|
2011-12-28 21:21:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_keep_all_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
|
|
|
@hash["omg"] = "world"
|
|
|
|
@hash.discard "hello"
|
2011-12-28 21:21:35 -05:00
|
|
|
@hash.keep
|
|
|
|
|
|
|
|
@hash.sweep
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "omg" => "world", "hello" => "world" }, @hash.to_hash)
|
2011-12-28 21:21:35 -05:00
|
|
|
end
|
2011-12-28 21:33:44 -05:00
|
|
|
|
|
|
|
def test_double_sweep
|
2016-08-06 12:54:50 -04:00
|
|
|
@hash["hello"] = "world"
|
2011-12-28 21:33:44 -05:00
|
|
|
@hash.sweep
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "hello" => "world" }, @hash.to_hash)
|
2011-12-28 21:33:44 -05:00
|
|
|
|
|
|
|
@hash.sweep
|
|
|
|
assert_equal({}, @hash.to_hash)
|
|
|
|
end
|
2011-04-06 21:01:03 -04:00
|
|
|
end
|
|
|
|
end
|