DEV Community

Discussion on: Testing signed and encrypted cookies in Rails

Collapse
 
philnash profile image
Phil Nash

Ah, you're right. I've switched those asserts around.

I am mostly writing my tests in RSpec, with RSpec-Rails, at the moment. Their documentation recommends using cookies over response.cookies and that's worked for me so far.

While writing this up, it was confusing what the difference between cookies and response.cookies is within the minitest versions. I might have to dig into that further to work out exactly what is going on.

Collapse
 
chaadow profile image
Chad Lee B.

Alright! I'm currently in the process of refactoring some tests using minitest (with Rails), will let you know what would potentially work for me (hopefully).

I've spelunked the rails code source, #cookies in minitest always refers to the request's cookies ( even after the HTTP call), and is a Rack::Test::CookieJar instance, whereas response.cookies is a plain ruby hash containing the responses's cookies. I'm going to instantiate a rails Cookie Jar as suggested in your article, but using response.cookies as the second argument.

Collapse
 
chaadow profile image
Chad Lee B. • Edited

Update: I've figured out how to make it work in my tests ( was having weird issues but application related, not rails related)

Basically to make this work in Rails/minitest, one would do:

jar = ActionDispatch::Cookies::CookieJar.build(request, response.cookies)
Enter fullscreen mode Exit fullscreen mode

We would need to use response.cookies. No need to add .to_hash because response.cookies is already a hash.

Unlike Rspec, cookies in Minitest ( or Rails custom version of minitest) always refer to the request cookies which is an instance of Rack::Test::CookieJar

Thread Thread
 
bubbaspaarx profile image
Edward Sparks • Edited

This is the answer I have been looking for. I had originally used

jar = ActionDispatch::Cookies::CookieJar.build(request, cookies.to_hash).signed
Enter fullscreen mode Exit fullscreen mode

But realised late on that the cookies.to_hash was empty as I never set cookies in Rack::Test::CookieJar

Thank you for this