Merge pull request #6082 from brainopia/smarter_cookie_jar

Stream cookies only if needed
This commit is contained in:
José Valim 2012-04-30 06:27:01 -07:00
commit 09de707f25
2 changed files with 46 additions and 15 deletions

@ -82,7 +82,7 @@ class Cookies
TOKEN_KEY = "action_dispatch.secret_token".freeze TOKEN_KEY = "action_dispatch.secret_token".freeze
# Raised when storing more than 4K of session data. # Raised when storing more than 4K of session data.
class CookieOverflow < StandardError; end CookieOverflow = Class.new StandardError
class CookieJar #:nodoc: class CookieJar #:nodoc:
include Enumerable include Enumerable
@ -153,7 +153,7 @@ def handle_options(options) #:nodoc:
end end
elsif options[:domain].is_a? Array elsif options[:domain].is_a? Array
# if host matches one of the supplied domains without a dot in front of it # if host matches one of the supplied domains without a dot in front of it
options[:domain] = options[:domain].find {|domain| @host.include? domain[/^\.?(.*)$/, 1] } options[:domain] = options[:domain].find {|domain| @host.include? domain.sub(/^\./, '') }
end end
end end
@ -168,12 +168,14 @@ def []=(key, options)
options = { :value => value } options = { :value => value }
end end
@cookies[key.to_s] = value
handle_options(options) handle_options(options)
@set_cookies[key.to_s] = options if @cookies[key.to_s] != value or options[:expires]
@delete_cookies.delete(key.to_s) @cookies[key.to_s] = value
@set_cookies[key.to_s] = options
@delete_cookies.delete(key.to_s)
end
value value
end end
@ -181,8 +183,9 @@ def []=(key, options)
# and setting its expiration date into the past. Like <tt>[]=</tt>, you can pass in # and setting its expiration date into the past. Like <tt>[]=</tt>, you can pass in
# an options hash to delete cookies with extra data such as a <tt>:path</tt>. # an options hash to delete cookies with extra data such as a <tt>:path</tt>.
def delete(key, options = {}) def delete(key, options = {})
options.symbolize_keys! return unless @cookies.has_key? key.to_s
options.symbolize_keys!
handle_options(options) handle_options(options)
value = @cookies.delete(key.to_s) value = @cookies.delete(key.to_s)

@ -38,6 +38,8 @@ def logout
head :ok head :ok
end end
alias delete_cookie logout
def delete_cookie_with_path def delete_cookie_with_path
cookies.delete("user_name", :path => '/beaten') cookies.delete("user_name", :path => '/beaten')
head :ok head :ok
@ -179,6 +181,18 @@ def test_setting_cookie
assert_equal({"user_name" => "david"}, @response.cookies) assert_equal({"user_name" => "david"}, @response.cookies)
end end
def test_setting_the_same_value_to_cookie
request.cookies[:user_name] = 'david'
get :authenticate
assert response.cookies.empty?
end
def test_setting_the_same_value_to_permanent_cookie
request.cookies[:user_name] = 'Jamie'
get :set_permanent_cookie
assert response.cookies, 'user_name' => 'Jamie'
end
def test_setting_with_escapable_characters def test_setting_with_escapable_characters
get :set_with_with_escapable_characters get :set_with_with_escapable_characters
assert_cookie_header "that+%26+guy=foo+%26+bar+%3D%3E+baz; path=/" assert_cookie_header "that+%26+guy=foo+%26+bar+%3D%3E+baz; path=/"
@ -235,23 +249,33 @@ def test_setting_test_cookie
end end
def test_expiring_cookie def test_expiring_cookie
request.cookies[:user_name] = 'Joe'
get :logout get :logout
assert_cookie_header "user_name=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT" assert_cookie_header "user_name=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
assert_equal({"user_name" => nil}, @response.cookies) assert_equal({"user_name" => nil}, @response.cookies)
end end
def test_delete_cookie_with_path def test_delete_cookie_with_path
request.cookies[:user_name] = 'Joe'
get :delete_cookie_with_path get :delete_cookie_with_path
assert_cookie_header "user_name=; path=/beaten; expires=Thu, 01-Jan-1970 00:00:00 GMT" assert_cookie_header "user_name=; path=/beaten; expires=Thu, 01-Jan-1970 00:00:00 GMT"
end end
def test_delete_unexisting_cookie
request.cookies.clear
get :delete_cookie
assert @response.cookies.empty?
end
def test_deleted_cookie_predicate def test_deleted_cookie_predicate
cookies[:user_name] = 'Joe'
cookies.delete("user_name") cookies.delete("user_name")
assert cookies.deleted?("user_name") assert cookies.deleted?("user_name")
assert_equal false, cookies.deleted?("another") assert_equal false, cookies.deleted?("another")
end end
def test_deleted_cookie_predicate_with_mismatching_options def test_deleted_cookie_predicate_with_mismatching_options
cookies[:user_name] = 'Joe'
cookies.delete("user_name", :path => "/path") cookies.delete("user_name", :path => "/path")
assert_equal false, cookies.deleted?("user_name", :path => "/different") assert_equal false, cookies.deleted?("user_name", :path => "/different")
end end
@ -284,6 +308,7 @@ def test_permanent_signed_cookie
end end
def test_delete_and_set_cookie def test_delete_and_set_cookie
request.cookies[:user_name] = 'Joe'
get :delete_and_set_cookie get :delete_and_set_cookie
assert_cookie_header "user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT" assert_cookie_header "user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT"
assert_equal({"user_name" => "david"}, @response.cookies) assert_equal({"user_name" => "david"}, @response.cookies)
@ -387,6 +412,7 @@ def test_cookie_with_all_domain_option_using_ipv6_address
end end
def test_deleting_cookie_with_all_domain_option def test_deleting_cookie_with_all_domain_option
request.cookies[:user_name] = 'Joe'
get :delete_cookie_with_domain get :delete_cookie_with_domain
assert_response :success assert_response :success
assert_cookie_header "user_name=; domain=.nextangle.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT" assert_cookie_header "user_name=; domain=.nextangle.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
@ -413,6 +439,7 @@ def test_cookie_with_all_domain_option_using_host_with_port_and_tld_length
end end
def test_deleting_cookie_with_all_domain_option_and_tld_length def test_deleting_cookie_with_all_domain_option_and_tld_length
request.cookies[:user_name] = 'Joe'
get :delete_cookie_with_domain_and_tld get :delete_cookie_with_domain_and_tld
assert_response :success assert_response :success
assert_cookie_header "user_name=; domain=.nextangle.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT" assert_cookie_header "user_name=; domain=.nextangle.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
@ -441,6 +468,7 @@ def test_cookie_with_several_preset_domains_using_shared_domain
def test_deletings_cookie_with_several_preset_domains_using_one_of_these_domains def test_deletings_cookie_with_several_preset_domains_using_one_of_these_domains
@request.host = "example2.com" @request.host = "example2.com"
request.cookies[:user_name] = 'Joe'
get :delete_cookie_with_domains get :delete_cookie_with_domains
assert_response :success assert_response :success
assert_cookie_header "user_name=; domain=example2.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT" assert_cookie_header "user_name=; domain=example2.com; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
@ -448,19 +476,19 @@ def test_deletings_cookie_with_several_preset_domains_using_one_of_these_domains
def test_deletings_cookie_with_several_preset_domains_using_other_domain def test_deletings_cookie_with_several_preset_domains_using_other_domain
@request.host = "other-domain.com" @request.host = "other-domain.com"
request.cookies[:user_name] = 'Joe'
get :delete_cookie_with_domains get :delete_cookie_with_domains
assert_response :success assert_response :success
assert_cookie_header "user_name=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT" assert_cookie_header "user_name=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
end end
def test_cookies_hash_is_indifferent_access def test_cookies_hash_is_indifferent_access
get :symbol_key get :symbol_key
assert_equal "david", cookies[:user_name] assert_equal "david", cookies[:user_name]
assert_equal "david", cookies['user_name'] assert_equal "david", cookies['user_name']
get :string_key get :string_key
assert_equal "dhh", cookies[:user_name] assert_equal "dhh", cookies[:user_name]
assert_equal "dhh", cookies['user_name'] assert_equal "dhh", cookies['user_name']
end end