File: test_authenticate.rb

package info (click to toggle)
libwww-mechanize-ruby 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 883
  • sloc: ruby: 6,621; makefile: 4
file content (71 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download
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
64
65
66
67
68
69
70
71
require "helper"
require 'pp'

class BasicAuthTest < Test::Unit::TestCase
  def setup
    @agent = Mechanize.new
  end

  def test_auth_success
    @agent.basic_auth('user', 'pass')
    page = @agent.get("http://localhost/basic_auth")
    assert_equal('You are authenticated', page.body)
  end

  def test_digest_auth_success
    @agent.basic_auth('user', 'pass')
    page = @agent.get("http://localhost/digest_auth")
    assert_equal('You are authenticated', page.body)
  end

  def test_no_duplicate_headers
    block_called = false
    @agent.pre_connect_hooks << lambda { |params|
      block_called = true
      params[:request].to_hash.each do |k,v|
        assert_equal(1, v.length)
      end
    }
    @agent.basic_auth('user', 'pass')
    page = @agent.get("http://localhost/digest_auth")
    assert block_called
  end

  def test_post_auth_success
    class << @agent
      alias :old_fetch_page :fetch_page
      attr_accessor :requests
      def fetch_page(args)
        @requests ||= []
        x = old_fetch_page(args)
        @requests << args[:verb]
        x
      end
    end
    @agent.basic_auth('user', 'pass')
    page = @agent.post("http://localhost/basic_auth")
    assert_equal('You are authenticated', page.body)
    assert_equal(2, @agent.requests.length)
    r1 = @agent.requests[0]
    r2 = @agent.requests[1]
    assert_equal(r1, r2)
  end

  def test_auth_bad_user_pass
    @agent.basic_auth('aaron', 'aaron')
    begin
      page = @agent.get("http://localhost/basic_auth")
    rescue Mechanize::ResponseCodeError => e
      assert_equal("401", e.response_code)
    end
  end

  def test_auth_failure
    begin
      page = @agent.get("http://localhost/basic_auth")
    rescue Mechanize::ResponseCodeError => e
      assert_equal("401", e.response_code)
    end
  end

end