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/test/dispatch/cookies_test.rb | 51 ++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'actionpack/test/dispatch') diff --git a/actionpack/test/dispatch/cookies_test.rb b/actionpack/test/dispatch/cookies_test.rb index c975c4f7ba..8e5fd97cc6 100644 --- a/actionpack/test/dispatch/cookies_test.rb +++ b/actionpack/test/dispatch/cookies_test.rb @@ -417,7 +417,7 @@ class CookiesTest < ActionController::TestCase 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 @@ class CookiesTest < ActionController::TestCase 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] -- cgit v1.2.3