add socket tests for 401 redirect and 401 failure
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by Nate Prewitt · parent 576b447 · 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
Provenance
- Commit
1da1213- Tree
db350e3- Author
Nate Prewitt <Nate.Prewitt@gmail.com>- Committer
Nate Prewitt <Nate.Prewitt@gmail.com>- Parent
576b447
1 file changed+112 added−1 removed
modified:tests/test_lowlevel.py+112 added-1 removed
3 unmodified lines4 import pytest5 import threading6 import requests7 8-from tests.testserver.server import Server8+from tests.testserver.server import Server, consume_socket_content9 10 from .utils import override_environ11 12 11 unmodified lines24 assert r.status_code == 20025 assert r.request.headers['Transfer-Encoding'] == 'chunked'26 27 28+def 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.31+32+ 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')39+40+ text_302 = (b'HTTP/1.1 302 FOUND\r\n'41+ b'Content-Length: 0\r\n'42+ b'Location: /\r\n\r\n')43+44+ text_200 = (b'HTTP/1.1 200 OK\r\n'45+ b'Content-Length: 0\r\n\r\n')46+47+ expected_digest = (b'Authorization: Digest username="user", '48+ b'realm="me@kennethreitz.com", '49+ b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')50+51+ auth = requests.auth.HTTPDigestAuth('user', 'pass')52+53+ 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)58+59+ # 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)63+64+ # 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)69+70+ # 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)74+75+ return request_content76+77+ close_server = threading.Event()78+ server = Server(digest_response_handler, wait_to_close_event=close_server)79+80+ 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()91+92+93+def 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')102+103+ expected_digest = (b'Authorization: Digest username="user", '104+ b'realm="me@kennethreitz.com", '105+ b'nonce="6bf5d6e4da1ce66918800195d6b9130d", uri="/"')106+107+ auth = requests.auth.HTTPDigestAuth('user', 'pass')108+109+ 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)114+115+ # 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)120+121+ # Verify the client didn't respond to second challenge.122+ request_content = consume_socket_content(sock, timeout=0.5)123+ assert request_content == b''124+125+ return request_content126+127+ close_server = threading.Event()128+ server = Server(digest_failed_response_handler, wait_to_close_event=close_server)129+130+ 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()137+138+139 _schemes_by_var_prefix = [140 ('http', ['http']),141 ('https', ['https']),142 ('all', ['http', 'https']),27 unmodified lines