Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / cookie_test.rb
blob80799e1c05a0e6d51938b9dad0c47131becbe048
1 require File.dirname(__FILE__) + '/../abstract_unit'
3 class CookieTest < Test::Unit::TestCase
4   class TestController < ActionController::Base
5     def authenticate
6       cookies["user_name"] = "david"
7     end
9     def authenticate_for_fourteen_days
10       cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
11     end
13     def authenticate_for_fourteen_days_with_symbols
14       cookies[:user_name] = { :value => "david", :expires => Time.local(2005, 10, 10) }
15     end
17     def set_multiple_cookies
18       cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
19       cookies["login"]     = "XJ-122"
20     end
21     
22     def access_frozen_cookies
23       cookies["will"] = "work"
24     end
26     def logout
27       cookies.delete("user_name")
28     end
30     def delete_cookie_with_path
31       cookies.delete("user_name", :path => '/beaten')
32       render :text => "hello world"
33     end
35     def authenticate_with_http_only
36       cookies["user_name"] = { :value => "david", :http_only => true }
37     end
39     def rescue_action(e) 
40       raise unless ActionController::MissingTemplate # No templates here, and we don't care about the output 
41     end
42   end
44   def setup
45     @request  = ActionController::TestRequest.new
46     @response = ActionController::TestResponse.new
48     @controller = TestController.new
49     @request.host = "www.nextangle.com"
50   end
52   def test_setting_cookie
53     get :authenticate
54     assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
55   end
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"]
60   end
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"]
65   end
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
71   end
73   def test_multiple_cookies
74     get :set_multiple_cookies
75     assert_equal 2, @response.cookies.size
76   end
78   def test_setting_test_cookie
79     assert_nothing_raised { get :access_frozen_cookies }
80   end
81   
82   def test_expiring_cookie
83     get :logout
84     assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)) ], @response.headers["cookie"]
85   end  
86   
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"]
93   end
95   def test_cookiejar_accessor_with_array_value
96     a = %w{1 2 3}
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"]
101   end
102   
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
107   end
109   def test_cookie_to_s_simple_values
110     assert_equal 'myname=myvalue; path=', CGI::Cookie.new('myname', 'myvalue').to_s
111   end
113   def test_cookie_to_s_hash
114     cookie_str = CGI::Cookie.new(
115       'name' => 'myname',
116       'value' => 'myvalue',
117       'domain' => 'mydomain',
118       'path' => 'mypath',
119       'expires' => Time.utc(2007, 10, 20),
120       'secure' => true,
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
123   end
125   def test_cookie_to_s_hash_default_not_secure_not_http_only
126     cookie_str = CGI::Cookie.new(
127       'name' => 'myname',
128       'value' => 'myvalue',
129       'domain' => 'mydomain',
130       'path' => 'mypath',
131       'expires' => Time.utc(2007, 10, 20))
132     assert cookie_str !~ /secure/
133     assert cookie_str !~ /HttpOnly/
134   end