Skip to content

Instantly share code, notes, and snippets.

@zeke
Created October 30, 2009 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeke/221971 to your computer and use it in GitHub Desktop.
Save zeke/221971 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'sinatra/test/unit'
require 'application'
require 'base64'
class ApplicationTest < Test::Unit::TestCase
def test_without_authentication
get '/protected'
assert_equal 401, @response.status
end
def test_with_bad_credentials
get '/protected', {}, {'HTTP_AUTHORIZATION' => encode_credentials('go', 'away')}
assert_equal 401, @response.status
end
def test_with_proper_credentials
get '/protected', {}, {'HTTP_AUTHORIZATION'=> encode_credentials('admin', 'admin')}
assert_equal 200, @response.status
assert_equal "You're welcome", @response.body
end
private
def encode_credentials(username, password)
"Basic " + Base64.encode64("#{username}:#{password}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment