test_utils.py
This file
Path
tests/test_utils.py
272 lines · 8.6 KiB · Python
Last changed by
Fixed warnings in unit tests suite, caused by #1127 PR (#1137)cdelerbefore adoption0a38695
History of this fileTake it
The walk stopped after 200 commits: lines older than f5eaec7 point at it.
More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 1–2why, one step back1import asyncio2import osURL matching (#1098)Tom Christiebefore adoptiondf54890
line 3why, one step back3import randomMore coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 4–7why, one step back45import pytest67import httpxSwitch to private module names (#785)Florimond Mancabefore adoption82dc6f3
line 8why, one step back8from httpx._utils import (More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 9–10why, one step back9 ElapsedTimer,10 NetRCInfo,Rename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
line 11why, one step back11 URLPattern,More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 12–16why, one step back12 get_ca_bundle_from_env,13 get_environment_proxies,14 guess_json_utf,15 obfuscate_sensitive_headers,16 parse_header_links,Drop private Origin model (#1070)Tom Christiebefore adoption247ee0d
line 17why, one step back17 same_origin,More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 18–20why, one step back18)19from tests.utils import override_log_level20Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 21why, one step back21from .common import FIXTURES_DIR, TESTS_DIRMore coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 22why, one step back22Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 23why, one step back23More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 24–60why, one step back24@pytest.mark.parametrize(25 "encoding",26 (27 "utf-32",28 "utf-8-sig",29 "utf-16",30 "utf-8",31 "utf-16-be",32 "utf-16-le",33 "utf-32-be",34 "utf-32-le",35 ),36)37def test_encoded(encoding):38 data = "{}".encode(encoding)39 assert guess_json_utf(data) == encoding404142def test_bad_utf_like_encoding():43 assert guess_json_utf(b"\x00\x00\x00\x00") is None444546@pytest.mark.parametrize(47 ("encoding", "expected"),48 (49 ("utf-16-be", "utf-16"),50 ("utf-16-le", "utf-16"),51 ("utf-32-be", "utf-32"),52 ("utf-32-le", "utf-32"),53 ),54)55def test_guess_by_bom(encoding, expected):56 data = "\ufeff{}".encode(encoding)57 assert guess_json_utf(data) == expected585960def test_bad_get_netrc_login():Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 61why, one step back61 netrc_info = NetRCInfo([str(FIXTURES_DIR / "does-not-exist")])More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 62–65why, one step back62 assert netrc_info.get_credentials("netrcexample.org") is None636465def test_get_netrc_login():Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 66why, one step back66 netrc_info = NetRCInfo([str(FIXTURES_DIR / ".netrc")])More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 67–73why, one step back67 expected_credentials = (68 "example-username",69 "example-password",70 )71 assert netrc_info.get_credentials("netrcexample.org") == expected_credentials7273Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 74why, one step back74def test_get_netrc_unknown():Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 75why, one step back75 netrc_info = NetRCInfo([str(FIXTURES_DIR / ".netrc")])Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 76–78why, one step back76 assert netrc_info.get_credentials("nonexistant.org") is None7778More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 79–143why, one step back79@pytest.mark.parametrize(80 "value, expected",81 (82 (83 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',84 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],85 ),86 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),87 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),88 (89 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',90 [91 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},92 {"url": "http://.../back.jpeg"},93 ],94 ),95 ("", []),96 ),97)98def test_parse_header_links(value, expected):99 assert parse_header_links(value) == expected100101102@pytest.mark.asyncio103async def test_logs_debug(server, capsys):104 with override_log_level("debug"):105 async with httpx.AsyncClient() as client:106 response = await client.get(server.url)107 assert response.status_code == 200108 stderr = capsys.readouterr().err109 assert 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"' in stderr110111112@pytest.mark.asyncio113async def test_logs_trace(server, capsys):114 with override_log_level("trace"):115 async with httpx.AsyncClient() as client:116 response = await client.get(server.url)117 assert response.status_code == 200118 stderr = capsys.readouterr().err119 assert 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"' in stderr120121122@pytest.mark.asyncio123async def test_logs_redirect_chain(server, capsys):124 with override_log_level("debug"):125 async with httpx.AsyncClient() as client:126 response = await client.get(server.url.copy_with(path="/redirect_301"))127 assert response.status_code == 200128129 stderr = capsys.readouterr().err.strip()130 redirected_request_line, ok_request_line = stderr.split("\n")131 assert redirected_request_line.endswith(132 "HTTP Request: GET http://127.0.0.1:8000/redirect_301 "133 '"HTTP/1.1 301 Moved Permanently"'134 )135 assert ok_request_line.endswith(136 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"'137 )138139140def test_get_ssl_cert_file():141 # Two environments is not set.142 assert get_ca_bundle_from_env() is None143Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 144why, one step back144 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 145why, one step back145 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
lines 146–147why, one step back146 ca_bundle = get_ca_bundle_from_env()147 assert ca_bundle is not None and ca_bundle.endswith("tests")More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 148–149why, one step back148149 del os.environ["SSL_CERT_DIR"]Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
line 150why, one step back150 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 151why, one step back151 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
lines 152–153why, one step back152 ca_bundle = get_ca_bundle_from_env()153 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 154–163why, one step back154155 os.environ["SSL_CERT_FILE"] = "wrongfile"156 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.157 assert get_ca_bundle_from_env() is None158159 del os.environ["SSL_CERT_FILE"]160 os.environ["SSL_CERT_DIR"] = "wrongpath"161 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.162 assert get_ca_bundle_from_env() is None163Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
lines 164–165why, one step back164 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)165 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 166why, one step back166 # Two environments is correctly set.Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
lines 167–168why, one step back167 ca_bundle = get_ca_bundle_from_env()168 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 169–171why, one step back169170 os.environ["SSL_CERT_FILE"] = "wrongfile"171 # Two environments is set but SSL_CERT_FILE is not a file.Use relative tests directory references (#1052)Florimond Mancabefore adoption09672a9
lines 172–173why, one step back172 ca_bundle = get_ca_bundle_from_env()173 assert ca_bundle is not None and ca_bundle.endswith("tests")More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 174–194why, one step back174175 os.environ["SSL_CERT_DIR"] = "wrongpath"176 # Two environments is set but both are not correct.177 assert get_ca_bundle_from_env() is None178179180@pytest.mark.asyncio181async def test_elapsed_timer():182 with ElapsedTimer() as timer:183 assert timer.elapsed.total_seconds() == pytest.approx(0, abs=0.05)184 await asyncio.sleep(0.1)185 await asyncio.sleep(186 0.1187 ) # test to ensure time spent after timer exits isn't accounted for.188 assert timer.elapsed.total_seconds() == pytest.approx(0.1, abs=0.05)189190191@pytest.mark.parametrize(192 ["environment", "proxies"],193 [194 ({}, {}),Fixed warnings in unit tests suite, caused by #1127 PR (#1137)cdelerbefore adoption0a38695
line 195why, one step back195 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 196–197why, one step back196 (197 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},Fixed warnings in unit tests suite, caused by #1127 PR (#1137)cdelerbefore adoption0a38695
line 198why, one step back198 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 199why, one step back199 ),Fixed warnings in unit tests suite, caused by #1127 PR (#1137)cdelerbefore adoption0a38695
line 200why, one step back200 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
lines 201–222why, one step back201 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),202 ],203)204def test_get_environment_proxies(environment, proxies):205 os.environ.update(environment)206207 assert get_environment_proxies() == proxies208209210@pytest.mark.parametrize(211 "headers, output",212 [213 ([("content-type", "text/html")], [("content-type", "text/html")]),214 ([("authorization", "s3kr3t")], [("authorization", "[secure]")]),215 ([("proxy-authorization", "s3kr3t")], [("proxy-authorization", "[secure]")]),216 ],217)218def test_obfuscate_sensitive_headers(headers, output):219 bytes_headers = [(k.encode(), v.encode()) for k, v in headers]220 bytes_output = [(k.encode(), v.encode()) for k, v in output]221 assert list(obfuscate_sensitive_headers(headers)) == output222 assert list(obfuscate_sensitive_headers(bytes_headers)) == bytes_outputDrop private Origin model (#1070)Tom Christiebefore adoption247ee0d
lines 223–234why, one step back223224225def test_same_origin():226 origin1 = httpx.URL("https://example.com")227 origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")228 assert same_origin(origin1, origin2)229230231def test_not_same_origin():232 origin1 = httpx.URL("https://example.com")233 origin2 = httpx.URL("HTTP://EXAMPLE.COM")234 assert not same_origin(origin1, origin2)URL matching (#1098)Tom Christiebefore adoptiondf54890
lines 235–249why, one step back235236237@pytest.mark.parametrize(238 ["pattern", "url", "expected"],239 [240 ("http://example.com", "http://example.com", True,),241 ("http://example.com", "https://example.com", False,),242 ("http://example.com", "http://other.com", False,),243 ("http://example.com:123", "http://example.com:123", True,),244 ("http://example.com:123", "http://example.com:456", False,),245 ("http://example.com:123", "http://example.com", False,),246 ("all://example.com", "http://example.com", True,),247 ("all://example.com", "https://example.com", True,),248 ("http://", "http://example.com", True,),249 ("http://", "https://example.com", False,),Fixed warnings in unit tests suite, caused by #1127 PR (#1137)cdelerbefore adoption0a38695
line 250why, one step back250 ("all://", "https://example.com:123", True,),URL matching (#1098)Tom Christiebefore adoptiondf54890
lines 251–254why, one step back251 ("", "https://example.com:123", True,),252 ],253)254def test_url_matches(pattern, url, expected):Rename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
lines 255–256why, one step back255 pattern = URLPattern(pattern)256 assert pattern.matches(httpx.URL(url)) == expectedURL matching (#1098)Tom Christiebefore adoptiondf54890
lines 257–258why, one step back257258Rename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
line 259why, one step back259def test_pattern_priority():URL matching (#1098)Tom Christiebefore adoptiondf54890
line 260why, one step back260 matchers = [Rename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
lines 261–264why, one step back261 URLPattern("all://"),262 URLPattern("http://"),263 URLPattern("http://example.com"),264 URLPattern("http://example.com:123"),URL matching (#1098)Tom Christiebefore adoptiondf54890
lines 265–267why, one step back265 ]266 random.shuffle(matchers)267 assert sorted(matchers) == [Rename URLMatcher -> URLPattern (#1109)Florimond Mancabefore adoption71d5234
lines 268–271why, one step back268 URLPattern("http://example.com:123"),269 URLPattern("http://example.com"),270 URLPattern("http://"),271 URLPattern("all://"),URL matching (#1098)Tom Christiebefore adoptiondf54890
line 272why, one step back272 ]