Hi.
I’m trying to inspect an authentication cookie inside of a test.
My goal is to test a remember me functionality, so I would like to test
for the expiration date of the set cookie.
This turns out to be extremely difficult (for me at least).
The farthest I managed to get so far is the following:
describe “cookies” do
let(:user) { FactoryGirl.create(:user)}
before { controller.sign_in(user) }
specify { cookies.should be_nil }
end
which fails as follows:
- UsersController specify and controller variables
Failure/Error: specify { cookies.should be_nil }
expected: nil
got: #<ActionDispatch::Cookies::CookieJar:0x0000000381f670
@secret=“1a5f7c9bbff386831db09dfdfc3a4c6b820e3ae8ddf210cc246d0df4cde6492e7c05132eca91bbe0a56b5cf9b266367b834f016c899d1bf6c5ef1f6cdbf78de5”,
@set_cookies={“remember_token”=>{:value=>“odLMhlC4aXMmRGw62naMcA”,
:expires=>Thu, 21 Apr 2033 07:04:12 UTC +00:00, :path=>"/"}},
@delete_cookies={}, @host=“test.host”, @secure=false, @closed=false,
@cookies={“remember_token”=>“odLMhlC4aXMmRGw62naMcA”},
@permanent=#<ActionDispatch::Cookies::PermanentCookieJar:0x00000003828d10
@parent_jar=#<ActionDispatch::Cookies::CookieJar:0x0000000381f670 …>,
@secret=“1a5f7c9bbff386831db09dfdfc3a4c6b820e3ae8ddf210cc246d0df4cde6492e7c05132eca91bbe0a56b5cf9b266367b834f016c899d1bf6c5ef1f6cdbf78de5”>>./spec/controllers/users_controller_spec.rb:212:in `block (3
levels) in <top (required)>’
As you can see the information I’m interested in is buried inside this
‘cookies’ object in an instance variable @set_cookies
@set_cookies={“remember_token”=>{:value=>“odLMhlC4aXMmRGw62naMcA”,
:expires=>Thu, 21 Apr 2033 07:04:12 UTC +00:00, :path=>"/"}
However, so far I am unable to extract the relevant data (i.e. the
‘expires’) out of this object.
How does one do that?
I read somewhere online that the cookies are buried inside the
‘response.headers’ object, but at least inside the users controller this
object is an empty hash.
describe “cookies” do
before { get user_path(user) }
specify {response.headers[“Set-Cookie”].should =~ /remember_token/}
end
Interestingly enough the same test as an integration test (under the
spec directory) does yield a non empty hash but the cookie i’m looking
for is not there, only the session cookie.
So there it is, what seems to me like a pretty elementary check turns
out to be very hard to implement in Rspec.
I find that hard to believe and thus expect that this is just a
testimony of my poor coding abilities.
Can anyone please shed some light on this issue?
What am I doing wrong here?
How does one test for cookies’ ‘expire’ in Rspec?
How can I extract the ‘expire’ data from the cookie object above?
Thanks a lot!
–Assaf.