DISQUS

ruby on rails blog: Testing with Basic HTTP Authentication in Rails 2.0 | ruby on rails blog

  • Hugues Lamy · 1 year ago
    Thanks for the tip. You just saved me couple of hours of fighting with the tests.

    Take Care.

    Hugues
  • Jonathan · 1 year ago
    Ce n'est pas un problème! (That's my year of Français I took coming out)

    I definitely spent too much time trying to figure this out...it's the simple things that are always a pain!
  • Diego · 1 year ago
    Thanks! Finally made my tests work..

    Regards
  • Jonathan · 1 year ago
    It's always kind of cool when random people read your blog and you can help them :)
  • Lane · 1 year ago
    Ahh, yes! I've been looking for this too. Thanks!
  • robert · 1 year ago
    thanks for this. helped in our functional tests of our REST services using http basic auth.
  • Ryan Cannon · 1 year ago
    A few months late, I found this via the Google. A couple notes:

    In case you're unfamiliar with testing Rails, this goes in test/test_helper.rb

    The function above doesn't actually let you test HTTP Authentication. To do that you'll need to do something like:

    def authenticate_with_http(username, password)
    @request.env[”HTTP_AUTHORIZATION”] =
    "Basic #{Base64.encode64("#{username}:#{password}")}"
    end
  • Jantzen Owens · 1 year ago
    This was just what I needed!

    Like unit testing you can do

    def setup
    http_auth
    end

    so all your test methods are authenticated.
  • joost · 10 months ago
    Instead of using Base64 directly, it's better to use:

    ActionController::HttpAuthentication::Basic.encode_credentials('username', 'password')