MCPcopy Index your code
hub / github.com/python/cpython / test_basic_and_digest_auth_handlers

Method test_basic_and_digest_auth_handlers

Lib/test/test_urllib2.py:1665–1713  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1663 )
1664
1665 def test_basic_and_digest_auth_handlers(self):
1666 # HTTPDigestAuthHandler raised an exception if it couldn't handle a 40*
1667 # response (https://bugs.python.org/issue1479302), where it should instead
1668 # return None to allow another handler (especially
1669 # HTTPBasicAuthHandler) to handle the response.
1670
1671 # Also (https://bugs.python.org/issue14797027, RFC 2617 section 1.2), we must
1672 # try digest first (since it's the strongest auth scheme), so we record
1673 # order of calls here to check digest comes first:
1674 class RecordingOpenerDirector(OpenerDirector):
1675 def __init__(self):
1676 OpenerDirector.__init__(self)
1677 self.recorded = []
1678
1679 def record(self, info):
1680 self.recorded.append(info)
1681
1682 class TestDigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
1683 def http_error_401(self, *args, **kwds):
1684 self.parent.record("digest")
1685 urllib.request.HTTPDigestAuthHandler.http_error_401(self,
1686 *args, **kwds)
1687
1688 class TestBasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
1689 def http_error_401(self, *args, **kwds):
1690 self.parent.record("basic")
1691 urllib.request.HTTPBasicAuthHandler.http_error_401(self,
1692 *args, **kwds)
1693
1694 opener = RecordingOpenerDirector()
1695 password_manager = MockPasswordManager()
1696 digest_handler = TestDigestAuthHandler(password_manager)
1697 basic_handler = TestBasicAuthHandler(password_manager)
1698 realm = "ACME Networks"
1699 http_handler = MockHTTPHandlerRedirect(
1700 401, 'WWW-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
1701 opener.add_handler(basic_handler)
1702 opener.add_handler(digest_handler)
1703 opener.add_handler(http_handler)
1704
1705 # check basic auth isn't blocked by digest handler failing
1706 self._test_basic_auth(opener, basic_handler, "Authorization",
1707 realm, http_handler, password_manager,
1708 "http://acme.example.com/protected",
1709 "http://acme.example.com/protected",
1710 )
1711 # check digest was tried before basic (twice, because
1712 # _test_basic_auth called .open() twice)
1713 self.assertEqual(opener.recorded, ["digest", "basic"]*2)
1714
1715 def test_unsupported_auth_digest_handler(self):
1716 opener = OpenerDirector()

Callers

nothing calls this directly

Calls 8

_test_basic_authMethod · 0.95
MockPasswordManagerClass · 0.85
add_handlerMethod · 0.80
assertEqualMethod · 0.45

Tested by

no test coverage detected