test_utils.py
This file
Path
tests/test_utils.py
113 lines · 2.9 KiB · Python
Last changed by
Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
History of this fileTake it
Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
line 1why, one step back1import loggingadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 2–3why, one step back2import os3Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 4–5why, one step back4import pytest5Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 6–7why, one step back6import httpx7from httpx import utilsAdded links property to BaseResponse (#211)euri10before adoptioncad9c03
line 8why, one step back8from httpx.utils import get_netrc_login, guess_json_utf, parse_header_linksEncoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 9–42why, one step back91011@pytest.mark.parametrize(12 "encoding",13 (14 "utf-32",15 "utf-8-sig",16 "utf-16",17 "utf-8",18 "utf-16-be",19 "utf-16-le",20 "utf-32-be",21 "utf-32-le",22 ),23)24def test_encoded(encoding):25 data = "{}".encode(encoding)26 assert guess_json_utf(data) == encoding272829def test_bad_utf_like_encoding():30 assert guess_json_utf(b"\x00\x00\x00\x00") is None313233@pytest.mark.parametrize(34 ("encoding", "expected"),35 (36 ("utf-16-be", "utf-16"),37 ("utf-16-le", "utf-16"),38 ("utf-32-be", "utf-32"),39 ("utf-32-le", "utf-32"),40 ),41)42def test_guess_by_bom(encoding, expected):Use nox for automation and CI (#128)Seth Michael Larsonbefore adoption80dc3e2
line 43why, one step back43 data = "\ufeff{}".encode(encoding)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
line 44why, one step back44 assert guess_json_utf(data) == expectedadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 45–55why, one step back454647def test_bad_get_netrc_login():48 assert get_netrc_login("url") is None4950 os.environ["NETRC"] = "tests/.netrc"51 assert get_netrc_login("url") is None5253 os.environ["NETRC"] = "wrongpath"54 assert get_netrc_login("url") is None55Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 56–61why, one step back56 from httpx import utils5758 utils.NETRC_STATIC_FILES = ()59 os.environ["NETRC"] = ""60 assert utils.get_netrc_login("url") is None61added netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 62–69why, one step back6263def test_get_netrc_login():64 os.environ["NETRC"] = "tests/.netrc"65 assert get_netrc_login("netrcexample.org") == (66 "example-username",67 None,68 "example-password",69 )Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 70–92why, one step back707172@pytest.mark.parametrize(73 "value, expected",74 (75 (76 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',77 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],78 ),79 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),80 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),81 (82 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',83 [84 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},85 {"url": "http://.../back.jpeg"},86 ],87 ),88 ("", []),89 ),90)91def test_parse_header_links(value, expected):92 assert parse_header_links(value) == expectedAdd easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 93–104why, one step back939495@pytest.mark.asyncio96@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])97async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):98 os.environ["HTTPX_DEBUG"] = httpx_debug99100 # Force a reload on the logging handlers101 utils._LOGGER_INITIALIZED = False102 utils.get_logger("httpx")103104 async with httpx.AsyncClient() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 105why, one step back105 await client.get(server.url)Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 106–113why, one step back106107 if httpx_debug in ("1", "True"):108 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err109 else:110 assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err111112 # Reset the logger so we don't have verbose output in all unit tests113 logging.getLogger("httpx").handlers = []