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
|
require File.dirname(__FILE__) + "/helper"
class BasicAuthTest < Test::Unit::TestCase
def setup
@agent = WWW::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_post_auth_success
class << @agent
alias :old_fetch_request :fetch_request
attr_accessor :requests
def fetch_request(*args)
@requests ||= []
@requests << old_fetch_request(*args)
@requests.last
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 r1['Content-Type']
assert r2['Content-Type']
assert_equal(r1['Content-Type'], r2['Content-Type'])
assert r1['Content-Length']
assert r2['Content-Length']
assert_equal(r1['Content-Length'], r2['Content-Length'])
end
def test_auth_bad_user_pass
@agent.basic_auth('aaron', 'aaron')
begin
page = @agent.get("http://localhost/basic_auth")
rescue WWW::Mechanize::ResponseCodeError => e
assert_equal("401", e.response_code)
end
end
def test_auth_failure
begin
page = @agent.get("http://localhost/basic_auth")
rescue WWW::Mechanize::ResponseCodeError => e
assert_equal("401", e.response_code)
end
end
end
|