Add backward compatibility for testing cookies
This commit restores the ability to assign cookies for testing via @request.env['HTTP_COOKIE'] and @request.cookies, e.g: @request.env['HTTP_COOKIE'] = 'user_name=david' get :index assert_equal 'david', cookies[:user_name] and @request.cookies[:user_name] = 'david' get :index assert_equal 'david', cookies[:user_name] Assigning via cookies[] is the preferred method and will take precedence over the other two methods. This is so that cookies set in controller actions have precedence and are carried over between calls to get, post, etc.
This commit is contained in:
parent
0a9270417c
commit
e864ff7259
@ -182,7 +182,14 @@ def recycle!
|
||||
@method = @request_method = nil
|
||||
@fullpath = @ip = @remote_ip = nil
|
||||
@env['action_dispatch.request.query_parameters'] = {}
|
||||
cookie_jar.reset!
|
||||
@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) }
|
||||
cookie_jar.update(rack_cookies)
|
||||
cookie_jar.update(cookies)
|
||||
cookie_jar.update(@set_cookies)
|
||||
cookie_jar.recycle!
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -228,7 +228,7 @@ def write(headers)
|
||||
@delete_cookies.each { |k, v| ::Rack::Utils.delete_cookie_header!(headers, k, v) }
|
||||
end
|
||||
|
||||
def reset! #:nodoc:
|
||||
def recycle! #:nodoc:
|
||||
@set_cookies.clear
|
||||
@delete_cookies.clear
|
||||
end
|
||||
|
@ -1,4 +1,5 @@
|
||||
require 'active_support/core_ext/object/blank'
|
||||
require 'active_support/core_ext/hash/indifferent_access'
|
||||
require 'active_support/core_ext/hash/reverse_merge'
|
||||
require 'rack/utils'
|
||||
|
||||
@ -14,7 +15,6 @@ def initialize(env = {})
|
||||
env = Rails.application.env_config.merge(env) if defined?(Rails.application)
|
||||
super(DEFAULT_ENV.merge(env))
|
||||
|
||||
@cookies = nil
|
||||
self.host = 'test.host'
|
||||
self.remote_addr = '0.0.0.0'
|
||||
self.user_agent = 'Rails Testing'
|
||||
@ -64,5 +64,11 @@ def accept=(mime_types)
|
||||
@env.delete('action_dispatch.request.accepts')
|
||||
@env['HTTP_ACCEPT'] = Array(mime_types).collect { |mime_type| mime_type.to_s }.join(",")
|
||||
end
|
||||
|
||||
alias :rack_cookies :cookies
|
||||
|
||||
def cookies
|
||||
@cookies ||= {}.with_indifferent_access
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -417,7 +417,7 @@ def test_deletings_cookie_with_several_preset_domains_using_other_domain
|
||||
assert_cookie_header "user_name=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
|
||||
end
|
||||
|
||||
|
||||
|
||||
def test_cookies_hash_is_indifferent_access
|
||||
get :symbol_key
|
||||
assert_equal "david", cookies[:user_name]
|
||||
@ -468,7 +468,54 @@ def test_cookies_can_be_cleared
|
||||
end
|
||||
|
||||
def test_can_set_http_cookie_header
|
||||
@request.env['HTTP_COOKIE'] = "user_name=david"
|
||||
@request.env['HTTP_COOKIE'] = 'user_name=david'
|
||||
get :noop
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
||||
get :noop
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
||||
@request.env['HTTP_COOKIE'] = 'user_name=andrew'
|
||||
get :noop
|
||||
assert_equal 'andrew', cookies['user_name']
|
||||
assert_equal 'andrew', cookies[:user_name]
|
||||
end
|
||||
|
||||
def test_can_set_request_cookies
|
||||
@request.cookies['user_name'] = 'david'
|
||||
get :noop
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
||||
get :noop
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
||||
@request.cookies[:user_name] = 'andrew'
|
||||
get :noop
|
||||
assert_equal 'andrew', cookies['user_name']
|
||||
assert_equal 'andrew', cookies[:user_name]
|
||||
end
|
||||
|
||||
def test_cookies_precedence_over_http_cookie
|
||||
@request.env['HTTP_COOKIE'] = 'user_name=andrew'
|
||||
get :authenticate
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
||||
get :noop
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
end
|
||||
|
||||
def test_cookies_precedence_over_request_cookies
|
||||
@request.cookies['user_name'] = 'andrew'
|
||||
get :authenticate
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
||||
get :noop
|
||||
assert_equal 'david', cookies['user_name']
|
||||
assert_equal 'david', cookies[:user_name]
|
||||
|
Loading…
Reference in New Issue
Block a user