The code is now completely covered in specs
[lyrix.git] / vendor / rails / actionpack / test / controller / cookie_test.rb
blob44023b079117ba07735775622a51059e343c71fd
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"
7     end
9     def authenticate
10       cookies["user_name"] = "david"
11     end
13     def authenticate_for_fourten_days
14       cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
15     end
17     def authenticate_for_fourten_days_with_symbols
18       cookies[:user_name] = { :value => "david", :expires => Time.local(2005, 10, 10) }
19     end
21     def set_multiple_cookies
22       cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
23       cookies["login"]     = "XJ-122"
24     end
26     def access_frozen_cookies
27       cookies["will"] = "work"
28     end
30     def logout
31       cookies.delete("user_name")
32     end
34     def delete_cookie_with_path
35       cookies.delete("user_name", :path => '/beaten')
36       render_text "hello world"
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_with_deprecated_writer
53     get :authenticate_with_deprecated_writer
54     assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
55   end
57   def test_setting_cookie
58     get :authenticate
59     assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
60   end
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"]
65   end
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"]
70   end
72   def test_multiple_cookies
73     get :set_multiple_cookies
74     assert_equal 2, @response.cookies.size
75   end
77   def test_setting_test_cookie
78     assert_nothing_raised { get :access_frozen_cookies }
79   end
80   
81   def test_expiring_cookie
82     get :logout
83     assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)) ], @response.headers["cookie"]
84   end  
85   
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"]
92   end
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
98   end
99 end