Skip to content

Commit

Permalink
Merge pull request #31844 from igorkasyanchuk/add_to_hash_and_to_h_fo…
Browse files Browse the repository at this point in the history
…r_session_and_cookies

Consistent behavior for session and cookies with to_h and to_hash method
  • Loading branch information
kamipo committed Feb 28, 2018
1 parent 7e06e7a commit 50a6249
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Add alias method `to_hash` to `to_h` for `cookies`.
Add alias method `to_h` to `to_hash` for `session`.

*Igor Kasyanchuk*

* Update the default HSTS max-age value to 31536000 seconds (1 year)
to meet the minimum max-age requirement for https://hstspreload.org/.

Expand Down
3 changes: 3 additions & 0 deletions actionpack/lib/action_dispatch/middleware/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ def key?(name)
end
alias :has_key? :key?

# Returns the cookies as Hash.
alias :to_hash :to_h

def update(other_hash)
@cookies.update other_hash.stringify_keys
self
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/request/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def to_hash
load_for_read!
@delegate.dup.delete_if { |_, v| v.nil? }
end
alias :to_h :to_hash

# Updates the session with given Hash.
#
Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/dispatch/cookies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def test_key_is_to_s
assert_equal "bar", request.cookie_jar.fetch(:foo)
end

def test_to_hash
request.cookie_jar["foo"] = "bar"
assert_equal({ "foo" => "bar" }, request.cookie_jar.to_hash)
assert_equal({ "foo" => "bar" }, request.cookie_jar.to_h)
end

def test_fetch_type_error
assert_raises(KeyError) do
request.cookie_jar.fetch(:omglolwut)
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/dispatch/request/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_to_hash
s["foo"] = "bar"
assert_equal "bar", s["foo"]
assert_equal({ "foo" => "bar" }, s.to_hash)
assert_equal({ "foo" => "bar" }, s.to_h)
end

def test_create_merges_old
Expand Down

0 comments on commit 50a6249

Please sign in to comment.