Skip to content

Instantly share code, notes, and snippets.

@kennethkalmer
Created May 14, 2012 16:30
Show Gist options
  • Save kennethkalmer/2694930 to your computer and use it in GitHub Desktop.
Save kennethkalmer/2694930 to your computer and use it in GitHub Desktop.
Rspec controller specs & signed cookies (http://stackoverflow.com/a/9094206/284612)
#
# An improvement on http://stackoverflow.com/a/9094206/284612
#
# Place this file in spec/support/signed_cookies.rb
#
module SignedCookies
def signed_cookie(name, opts={})
verifier = ActiveSupport::MessageVerifier.new(request.env["action_dispatch.secret_token".freeze])
if opts[:value]
@request.cookies[name] = verifier.generate(opts[:value])
else
verifier.verify(cookies[name])
end
end
end
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# omitted...
# Include our #signed_cookie helper method in our controller specs
config.include SignedCookies, :type => :controller
end
@mjobin-mdsol
Copy link

getting

*** ActiveSupport::MessageVerifier::InvalidSignature Exception: ActiveSupport::MessageVerifier::InvalidSignature

Also, this code seems to allow to set and look into cookies, but will it work to verify cookies were set the normal way in controller code?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment