Created
October 30, 2009 00:06
-
-
Save zeke/221971 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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