1 require 'abstract_unit'
3 class CookieTest
< ActionController
::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
.utc(2005, 10, 10,5) }
13 def authenticate_for_fourteen_days_with_symbols
14 cookies
[:user_name] = { :value => "david", :expires => Time
.utc(2005, 10, 10,5) }
17 def set_multiple_cookies
18 cookies
["user_name"] = { "value" => "david", "expires" => Time
.utc(2005, 10, 10,5) }
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", :httponly => true }
40 raise unless ActionView
::MissingTemplate # No templates here, and we don't care about the output
47 @request.host
= "www.nextangle.com"
50 def test_setting_cookie
52 assert_equal
["user_name=david; path=/"], @response.headers
["Set-Cookie"]
53 assert_equal({"user_name" => "david"}, @response.cookies
)
56 def test_setting_cookie_for_fourteen_days
57 get
:authenticate_for_fourteen_days
58 assert_equal
["user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT"], @response.headers
["Set-Cookie"]
59 assert_equal({"user_name" => "david"}, @response.cookies
)
62 def test_setting_cookie_for_fourteen_days_with_symbols
63 get
:authenticate_for_fourteen_days_with_symbols
64 assert_equal
["user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT"], @response.headers
["Set-Cookie"]
65 assert_equal({"user_name" => "david"}, @response.cookies
)
68 def test_setting_cookie_with_http_only
69 get
:authenticate_with_http_only
70 assert_equal
["user_name=david; path=/; HttpOnly"], @response.headers
["Set-Cookie"]
71 assert_equal({"user_name" => "david"}, @response.cookies
)
74 def test_multiple_cookies
75 get
:set_multiple_cookies
76 assert_equal
2, @response.cookies
.size
77 assert_equal
"user_name=david; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT", @response.headers
["Set-Cookie"][0]
78 assert_equal
"login=XJ-122; path=/", @response.headers
["Set-Cookie"][1]
79 assert_equal({"login" => "XJ-122", "user_name" => "david"}, @response.cookies
)
82 def test_setting_test_cookie
83 assert_nothing_raised
{ get
:access_frozen_cookies }
86 def test_expiring_cookie
88 assert_equal
["user_name=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"], @response.headers
["Set-Cookie"]
89 assert_equal({"user_name" => nil}, @response.cookies
)
92 def test_cookiejar_accessor
93 @request.cookies
["user_name"] = "david"
94 @controller.request
= @request
95 jar
= ActionController
::CookieJar.new(@controller)
96 assert_equal
"david", jar
["user_name"]
97 assert_equal
nil, jar
["something_else"]
100 def test_cookiejar_accessor_with_array_value
101 @request.cookies
["pages"] = %w
{1 2 3}
102 @controller.request
= @request
103 jar
= ActionController
::CookieJar.new(@controller)
104 assert_equal
%w
{1 2 3}, jar
["pages"]
107 def test_delete_cookie_with_path
108 get
:delete_cookie_with_path
109 assert_equal
["user_name=; path=/beaten; expires=Thu, 01-Jan-1970 00:00:00 GMT"], @response.headers
["Set-Cookie"]