1 require '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 ActionView
::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_with_symbols
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"][0].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"]
85 assert_equal CGI
::Cookie::new("name" => "user_name", "value" => "", "expires" => Time
.at(0)).value
, []
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"]
96 def test_cookiejar_accessor_with_array_value
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"]
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
110 def test_cookie_to_s_simple_values
111 assert_equal
'myname=myvalue; path=', CGI
::Cookie.new('myname', 'myvalue').to_s
114 def test_cookie_to_s_hash
115 cookie_str
= CGI
::Cookie.new(
117 'value' => 'myvalue',
118 'domain' => 'mydomain',
120 'expires' => Time
.utc(2007, 10, 20),
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
126 def test_cookie_to_s_hash_default_not_secure_not_http_only
127 cookie_str
= CGI
::Cookie.new(
129 'value' => 'myvalue',
130 'domain' => 'mydomain',
132 'expires' => Time
.utc(2007, 10, 20))
133 assert cookie_str
!~
/secure/
134 assert cookie_str
!~
/HttpOnly/
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
)
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