Testing HTTP Authentication

Posted by Luke Francl
on Tuesday, June 30

If you ever need to test HTTP Authentication in your functional tests, here is how you do it:

1
2
3
4
5
6
def test_http_auth
  @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("quentin", "password")
  get :show, :id => @foobar.id

  assert_response :success
end

This is much like testing SSL.

Hat tip: Philipp Führer for Functional test for HTTP Basic Authentication in Rails 2.

Comments

Leave a response

  1. DamienJuly 01, 2009 @ 02:26 AM

    And with RSPec

    1
    2
    3
    4
    5
    6
    7
    
    it "should test http authentication" do
      request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials("quentin", "password")
      
      get :show, :id => @foobar.id
      response.should be_success
    
    end