Tuesday, April 21, 2009

Rails 2 Functional Tests with HTTP Basic Authentication

I set up http basic authentication, as a quick way to show user-specific data for a demo I'm going to be doing.  Of course this broke my functional tests.

I found a solution here (thanks Flip!) -- manually adding an entry to session.env.  However, before I found that, I was digging through some of the rails code, and found a way to create the encoded credentials, which eliminates the string concatenation of Flip's solution (and may be a bit more robust going forward, in case the encoding changes in the future):

    @request.env['HTTP_AUTHORIZATION'] = 
             Basic.encode_credentials('username', 'password')

     (sorry about the line breaks -- tried to make it readable 
      without wrapping)

If anyone comes up with a more elegant solution, please let me know!

1 comment:

  1. Dude you are a lifesaver!!! this information was hard to find, thank god it's here. I'll probably be blogging about this soon. with full credit to you.