File: basic_test.rb

package info (click to toggle)
ruby-httpauth 0.2.1%2Bgh-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 192 kB
  • ctags: 127
  • sloc: ruby: 925; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,769 bytes parent folder | download | duplicates (3)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# encoding: us-ascii

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

require 'test/unit'
require 'httpauth/basic'
require 'httpauth/exceptions'

class BasicTest < Test::Unit::TestCase
  def setup
    @secret_bob_credentials = 'Basic Ym9iOnNlY3JldA=='
    @authorizations = {
      @secret_bob_credentials => %w[bob secret],
      'Basic bWFyY8OpbDpnb2Q=' => ["marc\303\251l", 'god']
      }
    @authentications = {
      "Basic realm=\"Admin\"" => 'Admin',
      "Basic realm=\"Admin Pages\"" => 'Admin Pages',
      "Basic realm=\"open=false\"" => 'open=false'
    }
  end

  def test_unpack_authorization
    @authorizations.each do |packed, unpacked|
      assert_equal unpacked, HTTPAuth::Basic.unpack_authorization(packed)
    end
  end

  def test_pack_authorization
    @authorizations.each do |packed, unpacked|
      assert_equal packed, HTTPAuth::Basic.pack_authorization(*unpacked)
    end
  end

  def test_get_credentials
    env = {'HTTP_AUTHORIZATION' => @secret_bob_credentials}
    assert_equal @authorizations[@secret_bob_credentials], HTTPAuth::Basic.get_credentials(env)
  end

  def test_pack_challenge
    @authentications.each do |packed, unpacked|
      assert_equal packed, HTTPAuth::Basic.pack_challenge(unpacked)
    end
  end

  def test_unpack_challenge
    @authentications.each do |packed, unpacked|
      assert_equal unpacked, HTTPAuth::Basic.unpack_challenge(packed)
    end
  end

  def test_invalid_input
    assert_raise(HTTPAuth::UnwellformedHeader) do
      HTTPAuth::Basic.unpack_challenge('Basic relm')
    end
    assert_raise(ArgumentError) do
      HTTPAuth::Basic.unpack_challenge('Digest data')
    end
    assert_raise(ArgumentError) do
      HTTPAuth::Basic.unpack_authorization('Digest data')
    end
  end
end