Sha256: bb04a8b24062eb0f1317b172d8aa2bb887eee3e0591ef008580bc523c4f30056

Contents?: true

Size: 1.99 KB

Versions: 122

Compression:

Stored size: 1.99 KB

Contents

require 'rack/auth/basic'
require 'rack/lint'
require 'rack/mock'

describe Rack::Auth::Basic do
  def realm
    'WallysWorld'
  end

  def unprotected_app
    Rack::Lint.new lambda { |env|
      [ 200, {'Content-Type' => 'text/plain'}, ["Hi #{env['REMOTE_USER']}"] ]
    }
  end

  def protected_app
    app = Rack::Auth::Basic.new(unprotected_app) { |username, password| 'Boss' == username }
    app.realm = realm
    app
  end

  before do
    @request = Rack::MockRequest.new(protected_app)
  end

  def request_with_basic_auth(username, password, &block)
    request 'HTTP_AUTHORIZATION' => 'Basic ' + ["#{username}:#{password}"].pack("m*"), &block
  end

  def request(headers = {})
    yield @request.get('/', headers)
  end

  def assert_basic_auth_challenge(response)
    response.should.be.a.client_error
    response.status.should.equal 401
    response.should.include 'WWW-Authenticate'
    response.headers['WWW-Authenticate'].should =~ /Basic realm="#{Regexp.escape(realm)}"/
    response.body.should.be.empty
  end

  should 'challenge correctly when no credentials are specified' do
    request do |response|
      assert_basic_auth_challenge response
    end
  end

  should 'rechallenge if incorrect credentials are specified' do
    request_with_basic_auth 'joe', 'password' do |response|
      assert_basic_auth_challenge response
    end
  end

  should 'return application output if correct credentials are specified' do
    request_with_basic_auth 'Boss', 'password' do |response|
      response.status.should.equal 200
      response.body.to_s.should.equal 'Hi Boss'
    end
  end

  should 'return 400 Bad Request if different auth scheme used' do
    request 'HTTP_AUTHORIZATION' => 'Digest params' do |response|
      response.should.be.a.client_error
      response.status.should.equal 400
      response.should.not.include 'WWW-Authenticate'
    end
  end

  it 'takes realm as optional constructor arg' do
    app = Rack::Auth::Basic.new(unprotected_app, realm) { true }
    realm.should == app.realm
  end
end

Version data entries

122 entries across 103 versions & 13 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
classiccms-0.7.4 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
classiccms-0.7.3 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
classiccms-0.7.2 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
classiccms-0.7.1 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
classiccms-0.7.0 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/test/spec_auth_basic.rb
challah-0.9.0 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
classiccms-0.6.9 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
jquery_regex-0.0.1 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
challah-rolls-0.2.0 vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/rack-1.4.1/test/spec_auth_basic.rb