test_utils.py
This file
Path
tests/test_utils.py
289 lines · 8.9 KiB · Python
Last changed by
Fix environment proxies (#2741)Kar Petrosyanbefore adoption3b9060e
History of this fileTake it
The walk stopped after 200 commits: lines older than 8e5e3b8 point at it.
Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
line 1why, one step back1import jsonUse standard logging style (#2547)Tom Christiebefore adoptionf1157db
line 2why, one step back2import loggingupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 3–5why, one step back3import os4import random5Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
line 6why, one step back6import certifiupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 7–19why, one step back7import pytest89import httpx10from httpx._utils import (11 URLPattern,12 get_ca_bundle_from_env,13 get_environment_proxies,14 is_https_redirect,15 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_DIRupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 21–36why, 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):Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
lines 37–39why, one step back37 content = '{"abc": 123}'.encode(encoding)38 response = httpx.Response(200, content=content)39 assert response.json() == {"abc": 123}update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 40–42why, one step back404142def test_bad_utf_like_encoding():Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
lines 43–46why, one step back43 content = b"\x00\x00\x00\x00"44 response = httpx.Response(200, content=content)45 with pytest.raises(json.decoder.JSONDecodeError):46 response.json()update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 47–58why, one step back474849@pytest.mark.parametrize(50 ("encoding", "expected"),51 (52 ("utf-16-be", "utf-16"),53 ("utf-16-le", "utf-16"),54 ("utf-32-be", "utf-32"),55 ("utf-32-le", "utf-32"),56 ),57)58def test_guess_by_bom(encoding, expected):Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150
lines 59–61why, one step back59 content = '\ufeff{"abc": 123}'.encode(encoding)60 response = httpx.Response(200, content=content)61 assert response.json() == {"abc": 123}update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 62–86why, one step back626364@pytest.mark.parametrize(65 "value, expected",66 (67 (68 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',69 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],70 ),71 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),72 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),73 (74 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',75 [76 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},77 {"url": "http://.../back.jpeg"},78 ],79 ),80 ("", []),81 ),82)83def test_parse_header_links(value, expected):84 assert parse_header_links(value) == expected8586Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 87–91why, one step back87def test_logging_request(server, caplog):88 caplog.set_level(logging.INFO)89 with httpx.Client() as client:90 response = client.get(server.url)91 assert response.status_code == 200update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 92why, one step back92Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 93–99why, one step back93 assert caplog.record_tuples == [94 (95 "httpx",96 logging.INFO,97 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',98 )99 ]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 100–101why, one step back100101Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 102–106why, one step back102def test_logging_redirect_chain(server, caplog):103 caplog.set_level(logging.INFO)104 with httpx.Client(follow_redirects=True) as client:105 response = client.get(server.url.copy_with(path="/redirect_301"))106 assert response.status_code == 200update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 107why, one step back107Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 108–111why, one step back108 assert caplog.record_tuples == [109 (110 "httpx",111 logging.INFO,Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18
lines 112–113why, one step back112 "HTTP Request: GET http://127.0.0.1:8000/redirect_301"113 ' "HTTP/1.1 301 Moved Permanently"',Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 114–120why, one step back114 ),115 (116 "httpx",117 logging.INFO,118 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',119 ),120 ]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 121why, one step back121Use standard logging style (#2547)Tom Christiebefore adoptionf1157db
lines 122–140why, one step back122123def test_logging_ssl(caplog):124 caplog.set_level(logging.DEBUG)125 with httpx.Client():126 pass127128 cafile = certifi.where()129 assert caplog.record_tuples == [130 (131 "httpx",132 logging.DEBUG,133 "load_ssl_context verify=True cert=None trust_env=True http2=False",134 ),135 (136 "httpx",137 logging.DEBUG,138 f"load_verify_locations cafile='{cafile}'",139 ),140 ]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 141–193why, one step back141142143def test_get_ssl_cert_file():144 # Two environments is not set.145 assert get_ca_bundle_from_env() is None146147 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)148 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.149 ca_bundle = get_ca_bundle_from_env()150 assert ca_bundle is not None and ca_bundle.endswith("tests")151152 del os.environ["SSL_CERT_DIR"]153 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")154 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.155 ca_bundle = get_ca_bundle_from_env()156 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")157158 os.environ["SSL_CERT_FILE"] = "wrongfile"159 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.160 assert get_ca_bundle_from_env() is None161162 del os.environ["SSL_CERT_FILE"]163 os.environ["SSL_CERT_DIR"] = "wrongpath"164 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.165 assert get_ca_bundle_from_env() is None166167 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)168 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")169 # Two environments is correctly set.170 ca_bundle = get_ca_bundle_from_env()171 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")172173 os.environ["SSL_CERT_FILE"] = "wrongfile"174 # Two environments is set but SSL_CERT_FILE is not a file.175 ca_bundle = get_ca_bundle_from_env()176 assert ca_bundle is not None and ca_bundle.endswith("tests")177178 os.environ["SSL_CERT_DIR"] = "wrongpath"179 # Two environments is set but both are not correct.180 assert get_ca_bundle_from_env() is None181182183@pytest.mark.parametrize(184 ["environment", "proxies"],185 [186 ({}, {}),187 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),188 (189 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},190 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},191 ),192 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),193 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),fix: NO_PROXY should support IPv4, IPv6 and localhost (#2659)Jiayun Shenbefore adoption15d09a3
lines 194–199why, one step back194 ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),195 ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),196 ({"no_proxy": "::1"}, {"all://[::1]": None}),197 ({"no_proxy": "localhost"}, {"all://localhost": None}),198 ({"no_proxy": "github.com"}, {"all://*github.com": None}),199 ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),Fix environment proxies (#2741)Kar Petrosyanbefore adoption3b9060e
line 200why, one step back200 ({"no_proxy": "http://github.com"}, {"http://github.com": None}),update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 201–289why, one step back201 ],202)203def test_get_environment_proxies(environment, proxies):204 os.environ.update(environment)205206 assert get_environment_proxies() == proxies207208209@pytest.mark.parametrize(210 "headers, output",211 [212 ([("content-type", "text/html")], [("content-type", "text/html")]),213 ([("authorization", "s3kr3t")], [("authorization", "[secure]")]),214 ([("proxy-authorization", "s3kr3t")], [("proxy-authorization", "[secure]")]),215 ],216)217def test_obfuscate_sensitive_headers(headers, output):218 bytes_headers = [(k.encode(), v.encode()) for k, v in headers]219 bytes_output = [(k.encode(), v.encode()) for k, v in output]220 assert list(obfuscate_sensitive_headers(headers)) == output221 assert list(obfuscate_sensitive_headers(bytes_headers)) == bytes_output222223224def test_same_origin():225 origin1 = httpx.URL("https://example.com")226 origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")227 assert same_origin(origin1, origin2)228229230def test_not_same_origin():231 origin1 = httpx.URL("https://example.com")232 origin2 = httpx.URL("HTTP://EXAMPLE.COM")233 assert not same_origin(origin1, origin2)234235236def test_is_https_redirect():237 url = httpx.URL("http://example.com")238 location = httpx.URL("https://example.com")239 assert is_https_redirect(url, location)240241242def test_is_not_https_redirect():243 url = httpx.URL("http://example.com")244 location = httpx.URL("https://www.example.com")245 assert not is_https_redirect(url, location)246247248def test_is_not_https_redirect_if_not_default_ports():249 url = httpx.URL("http://example.com:9999")250 location = httpx.URL("https://example.com:1337")251 assert not is_https_redirect(url, location)252253254@pytest.mark.parametrize(255 ["pattern", "url", "expected"],256 [257 ("http://example.com", "http://example.com", True),258 ("http://example.com", "https://example.com", False),259 ("http://example.com", "http://other.com", False),260 ("http://example.com:123", "http://example.com:123", True),261 ("http://example.com:123", "http://example.com:456", False),262 ("http://example.com:123", "http://example.com", False),263 ("all://example.com", "http://example.com", True),264 ("all://example.com", "https://example.com", True),265 ("http://", "http://example.com", True),266 ("http://", "https://example.com", False),267 ("all://", "https://example.com:123", True),268 ("", "https://example.com:123", True),269 ],270)271def test_url_matches(pattern, url, expected):272 pattern = URLPattern(pattern)273 assert pattern.matches(httpx.URL(url)) == expected274275276def test_pattern_priority():277 matchers = [278 URLPattern("all://"),279 URLPattern("http://"),280 URLPattern("http://example.com"),281 URLPattern("http://example.com:123"),282 ]283 random.shuffle(matchers)284 assert sorted(matchers) == [285 URLPattern("http://example.com:123"),286 URLPattern("http://example.com"),287 URLPattern("http://"),288 URLPattern("all://"),289 ]