Skip to content

Instantly share code, notes, and snippets.

@codeimpossible
Forked from kennethkalmer/signed_cookies.rb
Created November 29, 2013 22:49
Show Gist options
  • Save codeimpossible/7713035 to your computer and use it in GitHub Desktop.
Save codeimpossible/7713035 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