1 require File.dirname(__FILE__) + '/../abstract_unit'
3 class CookieTest < Test::Unit::TestCase
4 class TestController < ActionController::Base
5 def authenticate_with_deprecated_writer
6 cookie "name" => "user_name", "value" => "david"
10 cookies["user_name"] = "david"
13 def authenticate_for_fourten_days
14 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
17 def authenticate_for_fourten_days_with_symbols
18 cookies[:user_name] = { :value => "david", :expires => Time.local(2005, 10, 10) }
21 def set_multiple_cookies
22 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
23 cookies["login"] = "XJ-122"
26 def access_frozen_cookies
27 cookies["will"] = "work"
31 cookies.delete("user_name")
34 def delete_cookie_with_path
35 cookies.delete("user_name", :path => '/beaten')
36 render_text "hello world"
40 raise unless ActionController::MissingTemplate # No templates here, and we don't care about the output
45 @request = ActionController::TestRequest.new
46 @response = ActionController::TestResponse.new
48 @controller = TestController.new
49 @request.host = "www.nextangle.com"
52 def test_setting_cookie_with_deprecated_writer
53 get :authenticate_with_deprecated_writer
54 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
57 def test_setting_cookie
59 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
62 def test_setting_cookie_for_fourteen_days
63 get :authenticate_for_fourten_days
64 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
67 def test_setting_cookie_for_fourteen_days_with_symbols
68 get :authenticate_for_fourten_days
69 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
72 def test_multiple_cookies
73 get :set_multiple_cookies
74 assert_equal 2, @response.cookies.size
77 def test_setting_test_cookie
78 assert_nothing_raised { get :access_frozen_cookies }
81 def test_expiring_cookie
83 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)) ], @response.headers["cookie"]
86 def test_cookiejar_accessor
87 @request.cookies["user_name"] = CGI::Cookie.new("name" => "user_name", "value" => "david", "expires" => Time.local(2025, 10, 10))
88 @controller.request = @request
89 jar = ActionController::CookieJar.new(@controller)
90 assert_equal "david", jar["user_name"]
91 assert_equal nil, jar["something_else"]
94 def test_delete_cookie_with_path
95 get :delete_cookie_with_path
96 assert_equal "/beaten", @response.headers["cookie"].first.path
97 assert_not_equal "/", @response.headers["cookie"].first.path