I’m using TestUnit and I want to test the remember me functionality (when user login).
the cookies variable(and also require/response .cookies) contain only the cookie value without the expire time.
Rails somehow tell the web-browser when the cookie should be expire, so I assume there must be a way to check the cookie expire time.
EDIT
test "set permanent cookie" do
post :create, email: 'email', password: 'password', remember_me: true
# cookies[:auth_token] = random_string
# @request.cookies[:auth_token] = also_random_string
# @response.cookies[:auth_token] = also_random_string
end
the problem is that I can get only the values of the cookies and not the hash that contain the expire time.
As you’ve noticed, the
cookiesHash only contains the values, not the expiration times, when you inspect it after yourpostcall (this has been the behavior since at least Rails 2.3).You have two options:
First, you could inspect the
response.headers["Set-Cookie"]item instead. It will include the expiration time in there. However, theSet-Cookievalue is just a single string, which you would need to then parse. For example,cookies["foo"] = {:value => "bar", :expires => Time.now + 10.years }would give you:The other option (taken from This Question/Answer), would be to stub the cookie jar and make sure it is sent an
expiresvalue: