How do you spec a digest auth in Sinatra?

authentication, digest-authentication, rspec, ruby, sinatra

Solution

The Sinatra FAQ has an example using Test::Unit and Basic Auth. I've never used RSpec with Sinatra but it should be easy to translate the example from Test::Unit.

Even though the example use Basic Auth, Digest Auth can be tested the same way. The only difference is you need to use Rack::Test's digest_authorize method instead. For example, the last test in the FAQ would become this:

def test_with_proper_credentials
  digest_authorize 'admin', 'admin'
  get '/protected'
  assert_equal 200, last_response.status
  assert_equal "You're welcome", last_response.body
end

Problem

I have a digest auth set up like the example from the sinatrarb website. ``` #config.ru require './main' app = Rack::Auth::Digest::MD5.new(Sinatra::Application) do |username| {'foo' => 'bar'}[username] end app.realm = 'Protected Area' app.opaque = 'secretkey' run app ``` I was wondering if anyone knows how or can point me to a guide rspecing this. Thanks.

Original source