From e864ff7259ed9e4c61a146c635aedc3c60779931 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Sun, 5 Jun 2011 12:34:27 +0100 Subject: 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. --- actionpack/lib/action_dispatch/testing/test_request.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch/testing') diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb index 5c16023137..7280e9a93b 100644 --- a/actionpack/lib/action_dispatch/testing/test_request.rb +++ b/actionpack/lib/action_dispatch/testing/test_request.rb @@ -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 @@ module ActionDispatch 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 @@ module ActionDispatch @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 -- cgit v1.2.3