test_utils.py
This file
Path
tests/test_utils.py
194 lines · 5.5 KiB · Python
Last changed by
Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
History of this fileTake it
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 1why, one step back1import asyncioAdd easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
line 2why, one step back2import loggingadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 3–4why, one step back3import os4Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 5–6why, one step back5import pytest6Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 7–8why, one step back7import httpx8from httpx import utilsAdd `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 9–10why, one step back9from httpx.utils import (10 ElapsedTimer,Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
line 11why, one step back11 get_ca_bundle_from_env,Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 12why, one step back12 get_environment_proxies,Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 13–16why, one step back13 get_netrc_login,14 guess_json_utf,15 parse_header_links,16)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 17–50why, one step back171819@pytest.mark.parametrize(20 "encoding",21 (22 "utf-32",23 "utf-8-sig",24 "utf-16",25 "utf-8",26 "utf-16-be",27 "utf-16-le",28 "utf-32-be",29 "utf-32-le",30 ),31)32def test_encoded(encoding):33 data = "{}".encode(encoding)34 assert guess_json_utf(data) == encoding353637def test_bad_utf_like_encoding():38 assert guess_json_utf(b"\x00\x00\x00\x00") is None394041@pytest.mark.parametrize(42 ("encoding", "expected"),43 (44 ("utf-16-be", "utf-16"),45 ("utf-16-le", "utf-16"),46 ("utf-32-be", "utf-32"),47 ("utf-32-le", "utf-32"),48 ),49)50def test_guess_by_bom(encoding, expected):Use nox for automation and CI (#128)Seth Michael Larsonbefore adoption80dc3e2
line 51why, one step back51 data = "\ufeff{}".encode(encoding)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
line 52why, one step back52 assert guess_json_utf(data) == expectedadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 53–63why, one step back535455def test_bad_get_netrc_login():56 assert get_netrc_login("url") is None5758 os.environ["NETRC"] = "tests/.netrc"59 assert get_netrc_login("url") is None6061 os.environ["NETRC"] = "wrongpath"62 assert get_netrc_login("url") is None63Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 64–69why, one step back64 from httpx import utils6566 utils.NETRC_STATIC_FILES = ()67 os.environ["NETRC"] = ""68 assert utils.get_netrc_login("url") is None69added netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 70–77why, one step back7071def test_get_netrc_login():72 os.environ["NETRC"] = "tests/.netrc"73 assert get_netrc_login("netrcexample.org") == (74 "example-username",75 None,76 "example-password",77 )Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 78–100why, one step back787980@pytest.mark.parametrize(81 "value, expected",82 (83 (84 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',85 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],86 ),87 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),88 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),89 (90 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',91 [92 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},93 {"url": "http://.../back.jpeg"},94 ],95 ),96 ("", []),97 ),98)99def test_parse_header_links(value, expected):100 assert parse_header_links(value) == expectedAdd easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 101–112why, one step back101102103@pytest.mark.asyncio104@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])105async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):106 os.environ["HTTPX_DEBUG"] = httpx_debug107108 # Force a reload on the logging handlers109 utils._LOGGER_INITIALIZED = False110 utils.get_logger("httpx")111112 async with httpx.AsyncClient() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 113why, one step back113 await client.get(server.url)Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 114–121why, one step back114115 if httpx_debug in ("1", "True"):116 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err117 else:118 assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err119120 # Reset the logger so we don't have verbose output in all unit tests121 logging.getLogger("httpx").handlers = []Add support for SSL_CERT_FILE and SSL_CERT_DIR (#307)Can Sarıgölbefore adoptionc9810a7
lines 122–157why, one step back122123124def test_get_ssl_cert_file():125 # Two environments is not set.126 assert get_ca_bundle_from_env() is None127128 os.environ["SSL_CERT_DIR"] = "tests/"129 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.130 assert get_ca_bundle_from_env() == "tests"131132 del os.environ["SSL_CERT_DIR"]133 os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"134 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.135 assert get_ca_bundle_from_env() == "tests/test_utils.py"136137 os.environ["SSL_CERT_FILE"] = "wrongfile"138 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.139 assert get_ca_bundle_from_env() is None140141 del os.environ["SSL_CERT_FILE"]142 os.environ["SSL_CERT_DIR"] = "wrongpath"143 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.144 assert get_ca_bundle_from_env() is None145146 os.environ["SSL_CERT_DIR"] = "tests/"147 os.environ["SSL_CERT_FILE"] = "tests/test_utils.py"148 # Two environments is correctly set.149 assert get_ca_bundle_from_env() == "tests/test_utils.py"150151 os.environ["SSL_CERT_FILE"] = "wrongfile"152 # Two environments is set but SSL_CERT_FILE is not a file.153 assert get_ca_bundle_from_env() == "tests"154155 os.environ["SSL_CERT_DIR"] = "wrongpath"156 # Two environments is set but both are not correct.157 assert get_ca_bundle_from_env() is NoneAdd `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 158–168why, one step back158159160@pytest.mark.asyncio161async def test_elapsed_timer():162 with ElapsedTimer() as timer:163 assert timer.elapsed.total_seconds() == pytest.approx(0, abs=0.05)164 await asyncio.sleep(0.1)165 await asyncio.sleep(166 0.1167 ) # test to ensure time spent after timer exits isn't accounted for.168 assert timer.elapsed.total_seconds() == pytest.approx(0.1, abs=0.05)Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 169–194why, one step back169170171@pytest.mark.parametrize(172 ["environment", "proxies"],173 [174 ({}, {}),175 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http": "http://127.0.0.1"}),176 (177 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},178 {"https": "http://127.0.0.1", "http": "https://127.0.0.1"},179 ),180 (181 {"all_proxy": "http://127.0.0.1", "ALL_PROXY": "https://1.1.1.1"},182 {"all": "http://127.0.0.1"},183 ),184 (185 {"https_proxy": "http://127.0.0.1", "HTTPS_PROXY": "https://1.1.1.1"},186 {"https": "http://127.0.0.1"},187 ),188 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),189 ],190)191def test_get_environment_proxies(environment, proxies):192 os.environ.update(environment)193194 assert get_environment_proxies() == proxies