Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

CookieJar#deleted? predicate method #4613

Merged
merged 1 commit into from

2 participants

@pda

ActionDispatch::Cookies::CookieJar needs a way to query for deleted cookies.

Here's an example use case from a controller test:

get :destroy
assert cookies.deleted? "auth_token"

Cheers!
Paul

@pda pda ActionDispatch::Cookies::CookieJar#deleted? predicate method.
Necessary in controller tests to determine if the CookieJar will delete
the given cookie.
5169543
actionpack/lib/action_controller/test_case.rb
@@ -180,8 +180,7 @@ def recycle!
@env['action_dispatch.request.query_parameters'] = {}
@set_cookies ||= {}
@set_cookies.update(Hash[cookie_jar.instance_variable_get("@set_cookies").map{ |k,o| [k,o[:value]] }])
- deleted_cookies = cookie_jar.instance_variable_get("@delete_cookies")
- @set_cookies.reject!{ |k,v| deleted_cookies.include?(k) }
+ @set_cookies.reject!{ |k,v| cookie_jar.deleted?(k) }
@josevalim Owner

These are not exactly equivalent. The first one would delete any cookie regardless of the path or the domain, while the second one won't.

@pda
pda added a note

Good point. I'll eliminate that commit, leaving just the addition of the #deleted? method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pda

Removed change to actionpack/lib/action_controller/test_case.rb as @josevalim rightly points out using #deleted? wasn't exactly equivalent to the method using instance_variable_get.

@josevalim
Owner

Another option is to have deleted?(key) check if they key was deleted regardless of the options or add another method for that. Better than the instance variable get check.

@pda

I was initially reluctant to have #deleted? disregard the cookie scope options, but I can't really see a problem with def deleted?(key) not taking an options hash.

@josevalim josevalim merged commit adb7568 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 23, 2012
  1. @pda

    ActionDispatch::Cookies::CookieJar#deleted? predicate method.

    pda authored
    Necessary in controller tests to determine if the CookieJar will delete
    the given cookie.
This page is out of date. Refresh to see the latest.
View
9 actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -191,6 +191,15 @@ def delete(key, options = {})
value
end
+ # Whether the given cookie is to be deleted by this CookieJar.
+ # Like <tt>[]=</tt>, you can pass in an options hash to test if a
+ # deletion applies to a specific <tt>:path</tt>, <tt>:domain</tt> etc.
+ def deleted?(key, options = {})
+ options.symbolize_keys!
+ handle_options(options)
+ @delete_cookies[key.to_s] == options
+ end
+
# Removes all cookies on the client machine by calling <tt>delete</tt> for each cookie
def clear(options = {})
@cookies.each_key{ |k| delete(k, options) }
View
11 actionpack/test/dispatch/cookies_test.rb
@@ -245,6 +245,17 @@ def test_delete_cookie_with_path
assert_cookie_header "user_name=; path=/beaten; expires=Thu, 01-Jan-1970 00:00:00 GMT"
end
+ def test_deleted_cookie_predicate
+ cookies.delete("user_name")
+ assert cookies.deleted?("user_name")
+ assert_equal false, cookies.deleted?("another")
+ end
+
+ def test_deleted_cookie_predicate_with_mismatching_options
+ cookies.delete("user_name", :path => "/path")
+ assert_equal false, cookies.deleted?("user_name", :path => "/different")
+ end
+
def test_cookies_persist_throughout_request
response = get :authenticate
assert response.headers["Set-Cookie"] =~ /user_name=david/
Something went wrong with that request. Please try again.