在rails中测试已签名的Cookie [英] Testing signed cookies in rails
问题描述
我在Rails 3中使用已签名的Cookie,以在应用程序中启用记住我功能。一切正常,除非我无法功能测试cookie,因为比较 cookies ['remember_id']
给我加密的cookie和 cookie。
问题(至少在表面上)是在功能测试(ActionController :: TestCase)的上下文中,cookies对象是一个哈希,而当你使用控制器,它是一个ActionDispatch :: Cookies :: CookieJar对象。所以我们需要将它转换为一个CookieJar对象,以便我们可以使用signed方法将其转换为SignedCookieJar。
您可以将以下内容您的功能测试(在获取请求之后)将Cookie从哈希转换为CookieJar对象
@ request.cookies.merge! (cookies)
cookies = ActionDispatch :: Cookies :: CookieJar.build(@request)
I'm using signed cookies in Rails 3 to enable a "remember-me" feature in an application. Everything works except I'm not able to functional test the cookies, since comparing cookies['remember_id']
gives me the encrypted cookie, and cookies.signed
is not defined.
Any clues?
The problem (at least on the surface) is that in the context of a functional test (ActionController::TestCase), the "cookies" object is a Hash, whereas when you work with the controllers, it's a ActionDispatch::Cookies::CookieJar object. So we need to convert it to a CookieJar object so that we can use the "signed" method on it to convert it to a SignedCookieJar.
You can put the following into your functional tests (after a get request) to convert cookies from a Hash to a CookieJar object
@request.cookies.merge!(cookies)
cookies = ActionDispatch::Cookies::CookieJar.build(@request)
这篇关于在rails中测试已签名的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!