Skip to content

Commit

Permalink
Improve cookie test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Dec 19, 2008
1 parent 3da1b94 commit cbee72d
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions actionpack/test/controller/cookie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def set_multiple_cookies
cookies["user_name"] = { "value" => "david", "expires" => Time.local(2005, 10, 10) }
cookies["login"] = "XJ-122"
end

def access_frozen_cookies
cookies["will"] = "work"
end
Expand All @@ -36,8 +36,8 @@ def authenticate_with_http_only
cookies["user_name"] = { :value => "david", :http_only => true }
end

def rescue_action(e)
raise unless ActionView::MissingTemplate # No templates here, and we don't care about the output
def rescue_action(e)
raise unless ActionView::MissingTemplate # No templates here, and we don't care about the output
end
end

Expand All @@ -51,40 +51,46 @@ def setup

def test_setting_cookie
get :authenticate
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], @response.headers["cookie"]
assert_equal "user_name=david; path=/", @response.headers["cookie"].to_s
assert_equal({"user_name" => ["david"]}, @response.cookies)
end

def test_setting_cookie_for_fourteen_days
get :authenticate_for_fourteen_days
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
end
assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["cookie"].to_s
assert_equal({"user_name" => ["david"]}, @response.cookies)
end

def test_setting_cookie_for_fourteen_days_with_symbols
get :authenticate_for_fourteen_days_with_symbols
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"]
assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["cookie"].to_s
assert_equal({"user_name" => ["david"]}, @response.cookies)
end

def test_setting_cookie_with_http_only
get :authenticate_with_http_only
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "http_only" => true) ], @response.headers["cookie"]
assert_equal CGI::Cookie::new("name" => "user_name", "value" => "david", "path" => "/", "http_only" => true).to_s, @response.headers["cookie"][0].to_s
end
assert_equal "user_name=david; path=/; HttpOnly", @response.headers["cookie"].to_s
assert_equal({"user_name" => ["david"]}, @response.cookies)
end

def test_multiple_cookies
get :set_multiple_cookies
assert_equal 2, @response.cookies.size
end
assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["cookie"][0].to_s
assert_equal "login=XJ-122; path=/", @response.headers["cookie"][1].to_s
assert_equal({"login" => ["XJ-122"], "user_name" => ["david"]}, @response.cookies)
end

def test_setting_test_cookie
assert_nothing_raised { get :access_frozen_cookies }
end

def test_expiring_cookie
get :logout
assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)) ], @response.headers["cookie"]
assert_equal CGI::Cookie::new("name" => "user_name", "value" => "", "expires" => Time.at(0)).value, []
end
assert_equal "user_name=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT", @response.headers["cookie"].to_s
assert_equal({"user_name" => []}, @response.cookies)
end

def test_cookiejar_accessor
@request.cookies["user_name"] = CGI::Cookie.new("name" => "user_name", "value" => "david", "expires" => Time.local(2025, 10, 10))
@controller.request = @request
Expand All @@ -100,11 +106,10 @@ def test_cookiejar_accessor_with_array_value
jar = ActionController::CookieJar.new(@controller)
assert_equal a, jar["pages"]
end

def test_delete_cookie_with_path
get :delete_cookie_with_path
assert_equal "/beaten", @response.headers["cookie"].first.path
assert_not_equal "/", @response.headers["cookie"].first.path
assert_equal "user_name=; path=/beaten; expires=Thu, 01 Jan 1970 00:00:00 GMT", @response.headers["cookie"].to_s
end

def test_cookie_to_s_simple_values
Expand Down

0 comments on commit cbee72d

Please sign in to comment.