Skip to content
Myr

Merge pull request #3766 from nateprewitt/1979_test_401

Before adoption: this commit came in with the history the project adopted, so no landing records it.

testing HTTPDigestAuth hooks

authored by Cory Benfield · committed by GitHub · parents 4683f16, 1da1213 · Browse files · 1 file+112 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+112 added−1 removed

  • tests:+112 added−1 removed

1 file+112 added−1 removed

modified:tests/test_lowlevel.py+112 added-1 removed
3 unmodified lines
4import pytest5import threading6import requests78from tests.testserver.server import Server8from tests.testserver.server import Server, consume_socket_content910from .utils import override_environ1112
11 unmodified lines
24 assert r.status_code == 20025 assert r.request.headers['Transfer-Encoding'] == 'chunked'262728def test_digestauth_401_count_reset_on_redirect():29 """Ensure we correctly reset num_401_calls after a successful digest auth,30 followed by a 302 redirect to another digest auth prompt.3132 See https://github.com/kennethreitz/requests/issues/1979.33 """34 text_401 = (b'HTTP/1.1 401 UNAUTHORIZED\r\n'35 b'Content-Length: 0\r\n'36 b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'37 b', opaque="372825293d1c26955496c80ed6426e9e", '38 b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')3940 text_302 = (b'HTTP/1.1 302 FOUND\r\n'41 b'Content-Length: 0\r\n'42 b'Location: /\r\n\r\n')4344 text_200 = (b'HTTP/1.1 200 OK\r\n'45 b'Content-Length: 0\r\n\r\n')4647 expected_digest = (b'Authorization: Digest username="user", '48 b'realm="me@kennethreitz.com", '49 b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')5051 auth = requests.auth.HTTPDigestAuth('user', 'pass')5253 def digest_response_handler(sock):54 # Respond to initial GET with a challenge.55 request_content = consume_socket_content(sock, timeout=0.5)56 assert request_content.startswith(b"GET / HTTP/1.1")57 sock.send(text_401)5859 # Verify we receive an Authorization header in response, then redirect.60 request_content = consume_socket_content(sock, timeout=0.5)61 assert expected_digest in request_content62 sock.send(text_302)6364 # Verify Authorization isn't sent to the redirected host,65 # then send another challenge.66 request_content = consume_socket_content(sock, timeout=0.5)67 assert b'Authorization:' not in request_content68 sock.send(text_401)6970 # Verify Authorization is sent correctly again, and return 200 OK.71 request_content = consume_socket_content(sock, timeout=0.5)72 assert expected_digest in request_content73 sock.send(text_200)7475 return request_content7677 close_server = threading.Event()78 server = Server(digest_response_handler, wait_to_close_event=close_server)7980 with server as (host, port):81 url = 'http://{0}:{1}/'.format(host, port)82 r = requests.get(url, auth=auth)83 # Verify server succeeded in authenticating.84 assert r.status_code == 20085 # Verify Authorization was sent in final request.86 assert 'Authorization' in r.request.headers87 assert r.request.headers['Authorization'].startswith('Digest ')88 # Verify redirect happened as we expected.89 assert r.history[0].status_code == 30290 close_server.set()919293def test_digestauth_401_only_sent_once():94 """Ensure we correctly respond to a 401 challenge once, and then95 stop responding if challenged again.96 """97 text_401 = (b'HTTP/1.1 401 UNAUTHORIZED\r\n'98 b'Content-Length: 0\r\n'99 b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'100 b', opaque="372825293d1c26955496c80ed6426e9e", '101 b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')102103 expected_digest = (b'Authorization: Digest username="user", '104 b'realm="me@kennethreitz.com", '105 b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')106107 auth = requests.auth.HTTPDigestAuth('user', 'pass')108109 def digest_failed_response_handler(sock):110 # Respond to initial GET with a challenge.111 request_content = consume_socket_content(sock, timeout=0.5)112 assert request_content.startswith(b"GET / HTTP/1.1")113 sock.send(text_401)114115 # Verify we receive an Authorization header in response, then116 # challenge again.117 request_content = consume_socket_content(sock, timeout=0.5)118 assert expected_digest in request_content119 sock.send(text_401)120121 # Verify the client didn't respond to second challenge.122 request_content = consume_socket_content(sock, timeout=0.5)123 assert request_content == b''124125 return request_content126127 close_server = threading.Event()128 server = Server(digest_failed_response_handler, wait_to_close_event=close_server)129130 with server as (host, port):131 url = 'http://{0}:{1}/'.format(host, port)132 r = requests.get(url, auth=auth)133 # Verify server didn't authenticate us.134 assert r.status_code == 401135 assert r.history[0].status_code == 401136 close_server.set()137138139_schemes_by_var_prefix = [140 ('http', ['http']),141 ('https', ['https']),142 ('all', ['http', 'https']),
27 unmodified lines