iie labs

Test::Unit and Basic Authentication

Posted about 11 years ago by Philip Ingram

Ok. I admit I have a love hate relationship with testing. My framework is Test::Unit and mocha. I’ve sworn off factories for now, until they prove more useful then fixtures. As of this writing, they haven’t in my humble.

Testing Basic Authentication in Test::Unit

I found a forum post from google, that lead me to another post, that had a comment (thanks mike) with this final solution. I take no credit for the code, but i do think it needs more ‘play’ on the interwebs. All i did was wrap it into a helper.


def login_as_admin
@request.env[‘HTTP_AUTHORIZATION’] =
ActionController::HttpAuthentication::Basic.encode_credentials(‘user’, ‘pas’)

Now you have a helper method to use anywhere you have used basic authentication to secure part of your site. Just put this snippet in the test in which you need it.

setup :login_as_admin


blog comments powered by Disqus