Functional test for HTTP Basic Authentication in Rails 2 – flip's

Functional test for HTTP Basic Authentication in Rails 2

If you want to provide a username and password for a HTTP Basic Authentication in one of your functional tests, simply use the following:

def test_should_get_index
  @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
  get :index
  assert_response :success
  assert_not_nil assigns(:articles)
end

Keywords: rails test, functionals, authorization, basic http authentication, how to test

8 Responses to “Functional test for HTTP Basic Authentication in Rails 2”

  1. Thanks ….

    I did it through this article …

    Congrats ….

  2. thanks a lot.. searched a lot and this is my favourite solution :)

  3. I second Tim S. Saved me a lot of time.

  4. Thanks for the info!

    One additional suggestion: use ActionController::HttpAuthentication::Basic.encode_credentials. That way you don’t have to manually concatenate „Basic „, etc.

    Details are here: http://tek-zappeln.blogspot.com/2009/04/rails-2-functional-tests-with-http.html

  5. You’re a life saver! Thanks.

  6. Thanks buddy I was looking for this.

  7. Oh man this was a lifesaver thought I was gonna have to use something else.

  8. This post still help people (like me)!
    Thanks a lot

Leave a Reply