657be3c4e446c6b63ca85c9a084c01f7ea7e8260
[feedcatcher.git] / vendor / rails / actionpack / test / controller / cookie_test.rb
1 require 'abstract_unit'
2
3 class CookieTest < ActionController::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.utc(2005, 10, 10,5) }
11 end
12
13 def authenticate_for_fourteen_days_with_symbols
14 cookies[:user_name] = { :value => "david", :expires => Time.utc(2005, 10, 10,5) }
15 end
16
17 def set_multiple_cookies
18 cookies["user_name"] = { "value" => "david", "expires" => Time.utc(2005, 10, 10,5) }
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", :httponly => 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 tests TestController
45
46 def setup
47 @request.host = "www.nextangle.com"
48 end
49
50 def test_setting_cookie
51 get :authenticate
52 assert_equal ["user_name=david; path=/"], @response.headers["Set-Cookie"]
53 assert_equal({"user_name" => "david"}, @response.cookies)
54 end
55
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)
60 end
61
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)
66 end
67
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)
72 end
73
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)
80 end
81
82 def test_setting_test_cookie
83 assert_nothing_raised { get :access_frozen_cookies }
84 end
85
86 def test_expiring_cookie
87 get :logout
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)
90 end
91
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"]
98 end
99
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"]
105 end
106
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"]
110 end
111 end