Is it possible to verify the domain that a cookie was set for using Rails integration tests?
-
05-07-2019 - |
Question
I'm using the following technique to set a wild-card cookie for our domain:
cookies['root_domain_flash_warning'] = {
'value' => 'mistakes have been made!',
'domain' => ".#{APP_DOMAIN}",
'expires' => 2.minutes.from_now
}
Which seems to work handily. Unfortunately I can't seem to look up anything but the value associated with the 'value' key in the hash passed to CookieJar#[]= because of the fact that CookieJar#[] doesn't actually return the options hash passed to its reciprocal method.
Does anyone know of a way of verifying the domain a cookie has been set for in a functional test?
Solution
You could check the value of the Set-Cookie
header which can be accessed in @response.headers['Set-Cookie']
.
Try something along these lines:
def test_something
get '/my_action'
assert_equal ["root_domain_flash_warning=mistakes+have+been+made!; domain=.mydomain.com; path=/; expires=Fri, 07-Aug-2009 11:42:21 GMT"], @response.headers['Set-Cookie']
end
You can't extract these values from the cookies
method, unfortunately. In the ActionController::Integration::Session#process
source (actionpack/lib/action_controller/integration.rb
), the integration test cookies are processes as below, which only extracts the value
part of the input hash:
(@headers['Set-Cookie'] || "").split("\n").each do |cookie|
name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2]
@cookies[name] = value
end