Skip to content

Instantly share code, notes, and snippets.

@chrisgaunt
Forked from kennethkalmer/signed_cookies.rb
Created April 8, 2014 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisgaunt/10111697 to your computer and use it in GitHub Desktop.
Save chrisgaunt/10111697 to your computer and use it in GitHub Desktop.
#
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment