1 require File.dirname(__FILE__) + '/../abstract_unit'
3 class CookieTest < Test::Unit::TestCase
4 class TestController < ActionController::Base
6 cookies["user_name"] = "david"
9 def authenticate_for_fourteen_days
10 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
13 def authenticate_for_fourteen_days_with_symbols
14 cookies[:user_name] = { :value => "david", :expires => Time.local(2005, 10, 10) }
17 def set_multiple_cookies
18 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
19 cookies["login"] = "XJ-122"
22 def access_frozen_cookies
23 cookies["will"] = "work"
27 cookies.delete("user_name")
30 def delete_cookie_with_path
31 cookies.delete("user_name", :path => '/beaten')
32 render :text => "hello world"
35 def authenticate_with_http_only
36 cookies["user_name"] = { :value => "david", :http_only => true }
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
54 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
57 def test_setting_cookie_for_fourteen_days
58 get :authenticate_for_fourteen_days
59 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
62 def test_setting_cookie_for_fourteen_days_with_symbols
63 get :authenticate_for_fourteen_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_with_http_only
68 get :authenticate_with_http_only
69 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "http_only" => true) ], @response.headers["cookie"]
70 assert_equal CGI::Cookie::new("name" => "user_name", "value" => "david", "path" => "/", "http_only" => true).to_s, @response.headers["cookie"].to_s
73 def test_multiple_cookies
74 get :set_multiple_cookies
75 assert_equal 2, @response.cookies.size
78 def test_setting_test_cookie
79 assert_nothing_raised { get :access_frozen_cookies }
82 def test_expiring_cookie
84 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)) ], @response.headers["cookie"]
87 def test_cookiejar_accessor
88 @request.cookies["user_name"] = CGI::Cookie.new("name" => "user_name", "value" => "david", "expires" => Time.local(2025, 10, 10))
89 @controller.request = @request
90 jar = ActionController::CookieJar.new(@controller)
91 assert_equal "david", jar["user_name"]
92 assert_equal nil, jar["something_else"]
95 def test_cookiejar_accessor_with_array_value
97 @request.cookies["pages"] = CGI::Cookie.new("name" => "pages", "value" => a, "expires" => Time.local(2025, 10, 10))
98 @controller.request = @request
99 jar = ActionController::CookieJar.new(@controller)
100 assert_equal a, jar["pages"]
103 def test_delete_cookie_with_path
104 get :delete_cookie_with_path
105 assert_equal "/beaten", @response.headers["cookie"].first.path
106 assert_not_equal "/", @response.headers["cookie"].first.path
109 def test_cookie_to_s_simple_values
110 assert_equal 'myname=myvalue; path=', CGI::Cookie.new('myname', 'myvalue').to_s
113 def test_cookie_to_s_hash
114 cookie_str = CGI::Cookie.new(
116 'value' => 'myvalue',
117 'domain' => 'mydomain',
119 'expires' => Time.utc(2007, 10, 20),
121 'http_only' => true).to_s
122 assert_equal 'myname=myvalue; domain=mydomain; path=mypath; expires=Sat, 20 Oct 2007 00:00:00 GMT; secure; HttpOnly', cookie_str
125 def test_cookie_to_s_hash_default_not_secure_not_http_only
126 cookie_str = CGI::Cookie.new(
128 'value' => 'myvalue',
129 'domain' => 'mydomain',
131 'expires' => Time.utc(2007, 10, 20))
132 assert cookie_str !~ /secure/
133 assert cookie_str !~ /HttpOnly/