Home / Function/ test_digestauth_401_count_reset_on_redirect() — requests Function Reference

test_digestauth_401_count_reset_on_redirect() — requests Function Reference

Architecture documentation for the test_digestauth_401_count_reset_on_redirect() function in test_lowlevel.py from the requests codebase.

Entity Profile

Dependency Diagram

graph TD
  8434b1f0_81b9_349e_19e9_0160771cfc59["test_digestauth_401_count_reset_on_redirect()"]
  48561d17_8bd1_bf20_5710_ec3053a534d2["test_lowlevel.py"]
  8434b1f0_81b9_349e_19e9_0160771cfc59 -->|defined in| 48561d17_8bd1_bf20_5710_ec3053a534d2
  7ab3c4d7_8531_6cbe_e871_9880cb1ee6de["consume_socket_content()"]
  8434b1f0_81b9_349e_19e9_0160771cfc59 -->|calls| 7ab3c4d7_8531_6cbe_e871_9880cb1ee6de
  style 8434b1f0_81b9_349e_19e9_0160771cfc59 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

tests/test_lowlevel.py lines 127–189

def test_digestauth_401_count_reset_on_redirect():
    """Ensure we correctly reset num_401_calls after a successful digest auth,
    followed by a 302 redirect to another digest auth prompt.

    See https://github.com/psf/requests/issues/1979.
    """
    text_401 = (b'HTTP/1.1 401 UNAUTHORIZED\r\n'
                b'Content-Length: 0\r\n'
                b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'
                b', opaque="372825293d1c26955496c80ed6426e9e", '
                b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')

    text_302 = (b'HTTP/1.1 302 FOUND\r\n'
                b'Content-Length: 0\r\n'
                b'Location: /\r\n\r\n')

    text_200 = (b'HTTP/1.1 200 OK\r\n'
                b'Content-Length: 0\r\n\r\n')

    expected_digest = (b'Authorization: Digest username="user", '
                       b'realm="me@kennethreitz.com", '
                       b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')

    auth = requests.auth.HTTPDigestAuth('user', 'pass')

    def digest_response_handler(sock):
        # Respond to initial GET with a challenge.
        request_content = consume_socket_content(sock, timeout=0.5)
        assert request_content.startswith(b"GET / HTTP/1.1")
        sock.send(text_401)

        # Verify we receive an Authorization header in response, then redirect.
        request_content = consume_socket_content(sock, timeout=0.5)
        assert expected_digest in request_content
        sock.send(text_302)

        # Verify Authorization isn't sent to the redirected host,
        # then send another challenge.
        request_content = consume_socket_content(sock, timeout=0.5)
        assert b'Authorization:' not in request_content
        sock.send(text_401)

        # Verify Authorization is sent correctly again, and return 200 OK.
        request_content = consume_socket_content(sock, timeout=0.5)
        assert expected_digest in request_content
        sock.send(text_200)

        return request_content

    close_server = threading.Event()
    server = Server(digest_response_handler, wait_to_close_event=close_server)

    with server as (host, port):
        url = f'http://{host}:{port}/'
        r = requests.get(url, auth=auth)
        # Verify server succeeded in authenticating.
        assert r.status_code == 200
        # Verify Authorization was sent in final request.
        assert 'Authorization' in r.request.headers
        assert r.request.headers['Authorization'].startswith('Digest ')
        # Verify redirect happened as we expected.
        assert r.history[0].status_code == 302
        close_server.set()

Domain

Subdomains

Frequently Asked Questions

What does test_digestauth_401_count_reset_on_redirect() do?
test_digestauth_401_count_reset_on_redirect() is a function in the requests codebase, defined in tests/test_lowlevel.py.
Where is test_digestauth_401_count_reset_on_redirect() defined?
test_digestauth_401_count_reset_on_redirect() is defined in tests/test_lowlevel.py at line 127.
What does test_digestauth_401_count_reset_on_redirect() call?
test_digestauth_401_count_reset_on_redirect() calls 1 function(s): consume_socket_content.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free