Saturday, January 03, 2009

Ruby net/http+SSL+Basic Auth

Since one post from this blog is (or at least used to be) a little chunk of official documentation for Basic Auth in Ruby, I decided I should write this down also.

Socialtext is switching some of the SaaS servers to be https all the time. So I had to rejigger my selenium test-runner script, because it reads (test data) from and writes (test results) to the wiki.

I found this very elegant example.

My take on it looks like

def put_test_results_to_wiki
http = Net::HTTP.new(@test_host,443)
req = Net::HTTP::Put.new(@put_loc, initheader = {'Content-Type' =>'text/x.socialtext-wiki'})
http.use_ssl = true
req.basic_auth @test_user, @test_pass
req.body = ".pre\n" + @content + "\n.pre"
response = http.request(req)
puts response
end


What is a little peculiar about this is that Basic Auth is very much optional for SSL connections. Normally the client would do a GET, the server would return a cookie, and the client would use that cookie in subsequent transactions. I found a nice example of that here, but as of today the server seems to be down.