zeke (owner)

Revisions

gist: 221971 Download_button fork
public
Public Clone URL: git://gist.github.com/221971.git
Embed All Files: show embed
testing_http_auth_in_sinatra.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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