class CookiesTest

Constants

SALT

Public Instance Methods

setup() click to toggle source
Calls superclass method
# File actionpack/test/dispatch/cookies_test.rb, line 292
def setup
  super

  @request.env["action_dispatch.key_generator"] = ActiveSupport::KeyGenerator.new(SALT, iterations: 2)

  @request.env["action_dispatch.signed_cookie_salt"] =
    @request.env["action_dispatch.authenticated_encrypted_cookie_salt"] = SALT

  @request.host = "www.nextangle.com"
end
test_can_set_request_cookies() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1199
def test_can_set_request_cookies
  @request.cookies["user_name"] = "david"
  get :noop
  assert_equal "david", cookies["user_name"]
  assert_equal "david", cookies[:user_name]

  get :noop
  assert_equal "david", cookies["user_name"]
  assert_equal "david", cookies[:user_name]

  @request.cookies[:user_name] = "andrew"
  get :noop
  assert_equal "andrew", cookies["user_name"]
  assert_equal "andrew", cookies[:user_name]
end
test_cookies_are_not_cleared() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1237
def test_cookies_are_not_cleared
  cookies.encrypted["foo"] = "bar"
  get :noop
  assert_equal "bar", @controller.encrypted_cookie
end
test_cookies_can_be_cleared() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1171
def test_cookies_can_be_cleared
  get :symbol_key
  assert_equal "david", cookies[:user_name]

  cookies.clear
  get :noop
  assert_nil cookies[:user_name]

  get :symbol_key
  assert_equal "david", cookies[:user_name]
end
test_cookies_hash_is_indifferent_access() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1136
def test_cookies_hash_is_indifferent_access
  get :symbol_key
  assert_equal "david", cookies[:user_name]
  assert_equal "david", cookies["user_name"]
  get :string_key
  assert_equal "dhh", cookies[:user_name]
  assert_equal "dhh", cookies["user_name"]
end
test_cookies_persist_throughout_request() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 410
def test_cookies_persist_throughout_request
  response = get :authenticate
  assert_match(/user_name=david/, response.headers["Set-Cookie"])
end
test_cookies_precedence_over_request_cookies() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1226
def test_cookies_precedence_over_request_cookies
  @request.cookies["user_name"] = "andrew"
  get :authenticate
  assert_equal "david", cookies["user_name"]
  assert_equal "david", cookies[:user_name]

  get :noop
  assert_equal "david", cookies["user_name"]
  assert_equal "david", cookies[:user_name]
end
test_cookies_retained_across_requests() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1157
def test_cookies_retained_across_requests
  get :symbol_key
  assert_cookie_header "user_name=david; path=/"
  assert_equal "david", cookies[:user_name]

  get :noop
  assert_not_includes @response.headers, "Set-Cookie"
  assert_equal "david", cookies[:user_name]

  get :noop
  assert_not_includes @response.headers, "Set-Cookie"
  assert_equal "david", cookies[:user_name]
end
test_multiple_cookies() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 367
def test_multiple_cookies
  get :set_multiple_cookies
  assert_equal 2, @response.cookies.size
  assert_cookie_header "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 -0000\nlogin=XJ-122; path=/"
  assert_equal({ "login" => "XJ-122", "user_name" => "david" }, @response.cookies)
end
test_raise_data_overflow() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 643
def test_raise_data_overflow
  assert_raise(ActionDispatch::Cookies::CookieOverflow) do
    get :raise_data_overflow
  end
end
test_raises_argument_error_if_missing_secret() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 665
def test_raises_argument_error_if_missing_secret
  assert_raise(ArgumentError, nil.inspect) {
    @request.env["action_dispatch.key_generator"] = ActiveSupport::LegacyKeyGenerator.new(nil)
    get :set_signed_cookie
  }

  assert_raise(ArgumentError, "".inspect) {
    @request.env["action_dispatch.key_generator"] = ActiveSupport::LegacyKeyGenerator.new("")
    get :set_signed_cookie
  }
end
test_raises_argument_error_if_secret_is_probably_insecure() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 677
def test_raises_argument_error_if_secret_is_probably_insecure
  assert_raise(ArgumentError, "password".inspect) {
    @request.env["action_dispatch.key_generator"] = ActiveSupport::LegacyKeyGenerator.new("password")
    get :set_signed_cookie
  }

  assert_raise(ArgumentError, "secret".inspect) {
    @request.env["action_dispatch.key_generator"] = ActiveSupport::LegacyKeyGenerator.new("secret")
    get :set_signed_cookie
  }

  assert_raise(ArgumentError, "12345678901234567890123456789".inspect) {
    @request.env["action_dispatch.key_generator"] = ActiveSupport::LegacyKeyGenerator.new("12345678901234567890123456789")
    get :set_signed_cookie
  }
end
test_setting_request_cookies_is_indifferent_access() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 1145
def test_setting_request_cookies_is_indifferent_access
  cookies.clear
  cookies[:user_name] = "andrew"
  get :string_key_mock
  assert_equal "david", cookies["user_name"]

  cookies.clear
  cookies["user_name"] = "andrew"
  get :symbol_key_mock
  assert_equal "david", cookies[:user_name]
end
test_setting_with_escapable_characters() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 321
def test_setting_with_escapable_characters
  get :set_with_with_escapable_characters
  assert_cookie_header "that+%26+guy=foo+%26+bar+%3D%3E+baz; path=/"
  assert_equal({ "that & guy" => "foo & bar => baz" }, @response.cookies)
end
test_tampered_cookies() click to toggle source
# File actionpack/test/dispatch/cookies_test.rb, line 649
def test_tampered_cookies
  assert_nothing_raised do
    get :tampered_cookies
    assert_response :success
  end
end

Private Instance Methods