Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / cookie_test.rb
1 require 'abstract_unit'
2
3 class CookieTest < Test::Unit::TestCase
4 class TestController < ActionController::Base
5 def authenticate
6 cookies["user_name"] = "david"
7 end
8
9 def authenticate_for_fourteen_days
10 cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
11 end
12
13 def authenticate_for_fourteen_days_with_symbols
14 cookies[:user_name] = { :value => "david", :expires => Time.local(2005, 10, 10) }
15 end
16
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
25
26 def logout
27 cookies.delete("user_name")
28 end
29
30 def delete_cookie_with_path
31 cookies.delete("user_name", :path => '/beaten')
32 render :text => "hello world"
33 end
34
35 def authenticate_with_http_only
36 cookies["user_name"] = { :value => "david", :http_only => true }
37 end
38
39 def rescue_action(e)
40 raise unless ActionView::MissingTemplate # No templates here, and we don't care about the output
41 end
42 end
43
44 def setup
45 @request = ActionController::TestRequest.new
46 @response = ActionController::TestResponse.new
47
48 @controller = TestController.new
49 @request.host = "www.nextangle.com"
50 end
51
52 def test_setting_cookie
53 get :authenticate
54 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
55 end
56
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
61
62 def test_setting_cookie_for_fourteen_days_with_symbols
63 get :authenticate_for_fourteen_days_with_symbols
64 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
65 end
66
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"][0].to_s
71 end
72
73 def test_multiple_cookies
74 get :set_multiple_cookies
75 assert_equal 2, @response.cookies.size
76 end
77
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 assert_equal CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)).value, []
86 end
87
88 def test_cookiejar_accessor
89 @request.cookies["user_name"] = CGI::Cookie.new("name" => "user_name", "value" => "david", "expires" => Time.local(2025, 10, 10))
90 @controller.request = @request
91 jar = ActionController::CookieJar.new(@controller)
92 assert_equal "david", jar["user_name"]
93 assert_equal nil, jar["something_else"]
94 end
95
96 def test_cookiejar_accessor_with_array_value
97 a = %w{1 2 3}
98 @request.cookies["pages"] = CGI::Cookie.new("name" => "pages", "value" => a, "expires" => Time.local(2025, 10, 10))
99 @controller.request = @request
100 jar = ActionController::CookieJar.new(@controller)
101 assert_equal a, jar["pages"]
102 end
103
104 def test_delete_cookie_with_path
105 get :delete_cookie_with_path
106 assert_equal "/beaten", @response.headers["cookie"].first.path
107 assert_not_equal "/", @response.headers["cookie"].first.path
108 end
109
110 def test_cookie_to_s_simple_values
111 assert_equal 'myname=myvalue; path=', CGI::Cookie.new('myname', 'myvalue').to_s
112 end
113
114 def test_cookie_to_s_hash
115 cookie_str = CGI::Cookie.new(
116 'name' => 'myname',
117 'value' => 'myvalue',
118 'domain' => 'mydomain',
119 'path' => 'mypath',
120 'expires' => Time.utc(2007, 10, 20),
121 'secure' => true,
122 'http_only' => true).to_s
123 assert_equal 'myname=myvalue; domain=mydomain; path=mypath; expires=Sat, 20 Oct 2007 00:00:00 GMT; secure; HttpOnly', cookie_str
124 end
125
126 def test_cookie_to_s_hash_default_not_secure_not_http_only
127 cookie_str = CGI::Cookie.new(
128 'name' => 'myname',
129 'value' => 'myvalue',
130 'domain' => 'mydomain',
131 'path' => 'mypath',
132 'expires' => Time.utc(2007, 10, 20))
133 assert cookie_str !~ /secure/
134 assert cookie_str !~ /HttpOnly/
135 end
136
137 def test_cookies_should_not_be_split_on_ampersand_values
138 cookies = CGI::Cookie.parse('return_to=http://rubyonrails.org/search?term=api&scope=all&global=true')
139 assert_equal({"return_to" => ["http://rubyonrails.org/search?term=api&scope=all&global=true"]}, cookies)
140 end
141
142 def test_cookies_should_not_be_split_on_values_with_newlines
143 cookies = CGI::Cookie.new("name" => "val", "value" => "this\nis\na\ntest")
144 assert cookies.size == 1
145 end
146 end