test_utils.py
This file
Path
tests/test_utils.py
276 lines · 8.2 KiB · Python
Last changed by
Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
History of this fileTake it
The walk stopped after 200 commits: lines older than b019d6e point at it.
Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
line 1why, one step back1import loggingBump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 2–4why, one step back2import os3import random4Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
line 5why, one step back5import certifiBump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 6–13why, one step back6import pytest78import httpx9from httpx._utils import (10 URLPattern,11 get_ca_bundle_from_env,12 get_environment_proxies,13 guess_json_utf,Preserve Authorization header on HTTPS redirect (#1850) (#2074)waterfountain1996before adoption4401d55
line 14why, one step back14 is_https_redirect,Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 15–19why, one step back15 obfuscate_sensitive_headers,16 parse_header_links,17 same_origin,18)19Add `NetRCAuth()` class. (#2535)Tom Christiebefore adoption59914c7
line 20why, one step back20from .common import TESTS_DIRBump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 21–81why, one step back212223@pytest.mark.parametrize(24 "encoding",25 (26 "utf-32",27 "utf-8-sig",28 "utf-16",29 "utf-8",30 "utf-16-be",31 "utf-16-le",32 "utf-32-be",33 "utf-32-le",34 ),35)36def test_encoded(encoding):37 data = "{}".encode(encoding)38 assert guess_json_utf(data) == encoding394041def test_bad_utf_like_encoding():42 assert guess_json_utf(b"\x00\x00\x00\x00") is None434445@pytest.mark.parametrize(46 ("encoding", "expected"),47 (48 ("utf-16-be", "utf-16"),49 ("utf-16-le", "utf-16"),50 ("utf-32-be", "utf-32"),51 ("utf-32-le", "utf-32"),52 ),53)54def test_guess_by_bom(encoding, expected):55 data = "\ufeff{}".encode(encoding)56 assert guess_json_utf(data) == expected575859@pytest.mark.parametrize(60 "value, expected",61 (62 (63 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',64 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],65 ),66 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),67 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),68 (69 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',70 [71 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},72 {"url": "http://.../back.jpeg"},73 ],74 ),75 ("", []),76 ),77)78def test_parse_header_links(value, expected):79 assert parse_header_links(value) == expected8081Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 82–86why, one step back82def test_logging_request(server, caplog):83 caplog.set_level(logging.INFO)84 with httpx.Client() as client:85 response = client.get(server.url)86 assert response.status_code == 200Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
line 87why, one step back87Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 88–94why, one step back88 assert caplog.record_tuples == [89 (90 "httpx",91 logging.INFO,92 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',93 )94 ]Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 95–96why, one step back9596Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 97–101why, one step back97def test_logging_redirect_chain(server, caplog):98 caplog.set_level(logging.INFO)99 with httpx.Client(follow_redirects=True) as client:100 response = client.get(server.url.copy_with(path="/redirect_301"))101 assert response.status_code == 200Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
line 102why, one step back102Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 103–114why, one step back103 assert caplog.record_tuples == [104 (105 "httpx",106 logging.INFO,107 'HTTP Request: GET http://127.0.0.1:8000/redirect_301 "HTTP/1.1 301 Moved Permanently"',108 ),109 (110 "httpx",111 logging.INFO,112 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',113 ),114 ]Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
line 115why, one step back115Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 116–134why, one step back116117def test_logging_ssl(caplog):118 caplog.set_level(logging.DEBUG)119 with httpx.Client():120 pass121122 cafile = certifi.where()123 assert caplog.record_tuples == [124 (125 "httpx",126 logging.DEBUG,127 "load_ssl_context verify=True cert=None trust_env=True http2=False",128 ),129 (130 "httpx",131 logging.DEBUG,132 f"load_verify_locations cafile='{cafile}'",133 ),134 ]Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 135–220why, one step back135136137def test_get_ssl_cert_file():138 # Two environments is not set.139 assert get_ca_bundle_from_env() is None140141 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)142 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.143 ca_bundle = get_ca_bundle_from_env()144 assert ca_bundle is not None and ca_bundle.endswith("tests")145146 del os.environ["SSL_CERT_DIR"]147 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")148 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.149 ca_bundle = get_ca_bundle_from_env()150 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")151152 os.environ["SSL_CERT_FILE"] = "wrongfile"153 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.154 assert get_ca_bundle_from_env() is None155156 del os.environ["SSL_CERT_FILE"]157 os.environ["SSL_CERT_DIR"] = "wrongpath"158 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.159 assert get_ca_bundle_from_env() is None160161 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)162 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")163 # Two environments is correctly set.164 ca_bundle = get_ca_bundle_from_env()165 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")166167 os.environ["SSL_CERT_FILE"] = "wrongfile"168 # Two environments is set but SSL_CERT_FILE is not a file.169 ca_bundle = get_ca_bundle_from_env()170 assert ca_bundle is not None and ca_bundle.endswith("tests")171172 os.environ["SSL_CERT_DIR"] = "wrongpath"173 # Two environments is set but both are not correct.174 assert get_ca_bundle_from_env() is None175176177@pytest.mark.parametrize(178 ["environment", "proxies"],179 [180 ({}, {}),181 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),182 (183 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},184 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},185 ),186 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),187 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),188 ],189)190def test_get_environment_proxies(environment, proxies):191 os.environ.update(environment)192193 assert get_environment_proxies() == proxies194195196@pytest.mark.parametrize(197 "headers, output",198 [199 ([("content-type", "text/html")], [("content-type", "text/html")]),200 ([("authorization", "s3kr3t")], [("authorization", "[secure]")]),201 ([("proxy-authorization", "s3kr3t")], [("proxy-authorization", "[secure]")]),202 ],203)204def test_obfuscate_sensitive_headers(headers, output):205 bytes_headers = [(k.encode(), v.encode()) for k, v in headers]206 bytes_output = [(k.encode(), v.encode()) for k, v in output]207 assert list(obfuscate_sensitive_headers(headers)) == output208 assert list(obfuscate_sensitive_headers(bytes_headers)) == bytes_output209210211def test_same_origin():212 origin1 = httpx.URL("https://example.com")213 origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")214 assert same_origin(origin1, origin2)215216217def test_not_same_origin():218 origin1 = httpx.URL("https://example.com")219 origin2 = httpx.URL("HTTP://EXAMPLE.COM")220 assert not same_origin(origin1, origin2)Preserve Authorization header on HTTPS redirect (#1850) (#2074)waterfountain1996before adoption4401d55
lines 221–238why, one step back221222223def test_is_https_redirect():224 url = httpx.URL("http://example.com")225 location = httpx.URL("https://example.com")226 assert is_https_redirect(url, location)227228229def test_is_not_https_redirect():230 url = httpx.URL("http://example.com")231 location = httpx.URL("https://www.example.com")232 assert not is_https_redirect(url, location)233234235def test_is_not_https_redirect_if_not_default_ports():236 url = httpx.URL("http://example.com:9999")237 location = httpx.URL("https://example.com:1337")238 assert not is_https_redirect(url, location)Bump twine from 3.4.2 to 3.7.1 (#1998)dependabot[bot]before adoptionb019d6e
lines 239–276why, one step back239240241@pytest.mark.parametrize(242 ["pattern", "url", "expected"],243 [244 ("http://example.com", "http://example.com", True),245 ("http://example.com", "https://example.com", False),246 ("http://example.com", "http://other.com", False),247 ("http://example.com:123", "http://example.com:123", True),248 ("http://example.com:123", "http://example.com:456", False),249 ("http://example.com:123", "http://example.com", False),250 ("all://example.com", "http://example.com", True),251 ("all://example.com", "https://example.com", True),252 ("http://", "http://example.com", True),253 ("http://", "https://example.com", False),254 ("all://", "https://example.com:123", True),255 ("", "https://example.com:123", True),256 ],257)258def test_url_matches(pattern, url, expected):259 pattern = URLPattern(pattern)260 assert pattern.matches(httpx.URL(url)) == expected261262263def test_pattern_priority():264 matchers = [265 URLPattern("all://"),266 URLPattern("http://"),267 URLPattern("http://example.com"),268 URLPattern("http://example.com:123"),269 ]270 random.shuffle(matchers)271 assert sorted(matchers) == [272 URLPattern("http://example.com:123"),273 URLPattern("http://example.com"),274 URLPattern("http://"),275 URLPattern("all://"),276 ]