Testing with Basic HTTP Authentication in Rails 2.0

January 11, 2008

 
No Gravatar

I ran into a little issue while writing functional tests in Rails 2.0, using the included HTTP authentication I’d find my controllers returning 401 errors since they weren’t authenticated. After poking around, I found a solution:

1
2
3
4
5
class Test::Unit::TestCase
 	def http_auth
	 @request.env[”HTTP_AUTHORIZATION”] = “Basic #{Base64.encode64(”foo:bar”)}”
	end
end

Then, in my actual tests I could call:

1
http_auth
blog comments powered by Disqus