test_lowlevel.py
This file
Path
tests/test_lowlevel.py
206 lines · 7.6 KiB · Python
Last changed by
Merge branch 'master' of github.com:kennethreitz/requestsKenneth Reitzbefore adoption8c6e938
History of this fileTake it
The walk stopped after 200 commits: lines older than f803dc6 point at it.
Merge pull request #3429 from nateprewitt/docstring_cleanupKenneth Reitzbefore adoptionfb01456
lines 1–2why, one step back1# -*- coding: utf-8 -*-2Merge pull request #3185 from brettdh/3183-support-all-proxy-env-varIan Cordascobefore adoptioncd4e6b9
lines 3–4why, one step back3import os4import pytestupdated authorsKenneth Reitzbefore adoptionf803dc6
lines 5–7why, one step back5import threading6import requests7Merge pull request #3766 from nateprewitt/1979_test_401Cory Benfieldbefore adoption9726f53
line 8why, one step back8from tests.testserver.server import Server, consume_socket_contentupdated authorsKenneth Reitzbefore adoptionf803dc6
line 9why, one step back9Merge pull request #3185 from brettdh/3183-support-all-proxy-env-varIan Cordascobefore adoptioncd4e6b9
line 10why, one step back10from .utils import override_environupdated authorsKenneth Reitzbefore adoptionf803dc6
line 11why, one step back11Merge pull request #3185 from brettdh/3183-support-all-proxy-env-varIan Cordascobefore adoptioncd4e6b9
line 12why, one step back12updated authorsKenneth Reitzbefore adoptionf803dc6
lines 13–16why, one step back13def test_chunked_upload():14 """can safely send generators"""15 close_server = threading.Event()16 server = Server.basic_response_server(wait_to_close_event=close_server)Merge pull request #3185 from brettdh/3183-support-all-proxy-env-varIan Cordascobefore adoptioncd4e6b9
line 17why, one step back17 data = iter([b'a', b'b', b'c'])updated authorsKenneth Reitzbefore adoptionf803dc6
lines 18–21why, one step back1819 with server as (host, port):20 url = 'http://{0}:{1}/'.format(host, port)21 r = requests.post(url, data=data, stream=True)Merge pull request #3428 from nateprewitt/pep8_fixesKenneth Reitzbefore adoption4285bac
line 22why, one step back22 close_server.set() # release server blockupdated authorsKenneth Reitzbefore adoptionf803dc6
lines 23–25why, one step back2324 assert r.status_code == 20025 assert r.request.headers['Transfer-Encoding'] == 'chunked'Merge pull request #3766 from nateprewitt/1979_test_401Cory Benfieldbefore adoption9726f53
lines 26–136why, one step back262728def 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()Merge pull request #3185 from brettdh/3183-support-all-proxy-env-varIan Cordascobefore adoptioncd4e6b9
lines 137–138why, one step back137138Merge branch 'master' of github.com:kennethreitz/requestsKenneth Reitzbefore adoption8c6e938
lines 139–175why, one step back139def test_digestauth_only_on_4xx():140 """Ensure we only send digestauth on 4xx challenges.141142 See https://github.com/kennethreitz/requests/issues/3772.143 """144 text_200_chal = (b'HTTP/1.1 200 OK\r\n'145 b'Content-Length: 0\r\n'146 b'WWW-Authenticate: Digest nonce="6bf5d6e4da1ce66918800195d6b9130d"'147 b', opaque="372825293d1c26955496c80ed6426e9e", '148 b'realm="me@kennethreitz.com", qop=auth\r\n\r\n')149150 auth = requests.auth.HTTPDigestAuth('user', 'pass')151152 def digest_response_handler(sock):153 # Respond to GET with a 200 containing www-authenticate header.154 request_content = consume_socket_content(sock, timeout=0.5)155 assert request_content.startswith(b"GET / HTTP/1.1")156 sock.send(text_200_chal)157158 # Verify the client didn't respond with auth.159 request_content = consume_socket_content(sock, timeout=0.5)160 assert request_content == b''161162 return request_content163164 close_server = threading.Event()165 server = Server(digest_response_handler, wait_to_close_event=close_server)166167 with server as (host, port):168 url = 'http://{0}:{1}/'.format(host, port)169 r = requests.get(url, auth=auth)170 # Verify server didn't receive auth from us.171 assert r.status_code == 200172 assert len(r.history) == 0173 close_server.set()174175Merge pull request #3185 from brettdh/3183-support-all-proxy-env-varIan Cordascobefore adoptioncd4e6b9
lines 176–206why, one step back176_schemes_by_var_prefix = [177 ('http', ['http']),178 ('https', ['https']),179 ('all', ['http', 'https']),180]181182_proxy_combos = []183for prefix, schemes in _schemes_by_var_prefix:184 for scheme in schemes:185 _proxy_combos.append(("{0}_proxy".format(prefix), scheme))186187_proxy_combos += [(var.upper(), scheme) for var, scheme in _proxy_combos]188189190@pytest.mark.parametrize("var,scheme", _proxy_combos)191def test_use_proxy_from_environment(httpbin, var, scheme):192 url = "{0}://httpbin.org".format(scheme)193 fake_proxy = Server() # do nothing with the requests; just close the socket194 with fake_proxy as (host, port):195 proxy_url = "socks5://{0}:{1}".format(host, port)196 kwargs = {var: proxy_url}197 with override_environ(**kwargs):198 # fake proxy's lack of response will cause a ConnectionError199 with pytest.raises(requests.exceptions.ConnectionError):200 requests.get(url)201202 # the fake proxy received a request203 assert len(fake_proxy.handler_results) == 1204205 # it had actual content (not checking for SOCKS protocol for now)206 assert len(fake_proxy.handler_results[0]) > 0