test_utils.py
This file
Path
tests/test_utils.py
130 lines · 3.3 KiB · Python
Last changed by
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
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–14why, one step back9from httpx.utils import (10 ElapsedTimer,11 get_netrc_login,12 guess_json_utf,13 parse_header_links,14)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
lines 15–48why, one step back151617@pytest.mark.parametrize(18 "encoding",19 (20 "utf-32",21 "utf-8-sig",22 "utf-16",23 "utf-8",24 "utf-16-be",25 "utf-16-le",26 "utf-32-be",27 "utf-32-le",28 ),29)30def test_encoded(encoding):31 data = "{}".encode(encoding)32 assert guess_json_utf(data) == encoding333435def test_bad_utf_like_encoding():36 assert guess_json_utf(b"\x00\x00\x00\x00") is None373839@pytest.mark.parametrize(40 ("encoding", "expected"),41 (42 ("utf-16-be", "utf-16"),43 ("utf-16-le", "utf-16"),44 ("utf-32-be", "utf-32"),45 ("utf-32-le", "utf-32"),46 ),47)48def test_guess_by_bom(encoding, expected):Use nox for automation and CI (#128)Seth Michael Larsonbefore adoption80dc3e2
line 49why, one step back49 data = "\ufeff{}".encode(encoding)Encoding detection in Response.json (#116)Yeray Diaz Diazbefore adoption5442006
line 50why, one step back50 assert guess_json_utf(data) == expectedadded netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 51–61why, one step back515253def test_bad_get_netrc_login():54 assert get_netrc_login("url") is None5556 os.environ["NETRC"] = "tests/.netrc"57 assert get_netrc_login("url") is None5859 os.environ["NETRC"] = "wrongpath"60 assert get_netrc_login("url") is None61Fail unit tests without 100% coverage (#206)Can Sarıgölbefore adoption9536b76
lines 62–67why, one step back62 from httpx import utils6364 utils.NETRC_STATIC_FILES = ()65 os.environ["NETRC"] = ""66 assert utils.get_netrc_login("url") is None67added netrc support (#177)Can Sarıgölbefore adoption919e8d3
lines 68–75why, one step back6869def test_get_netrc_login():70 os.environ["NETRC"] = "tests/.netrc"71 assert get_netrc_login("netrcexample.org") == (72 "example-username",73 None,74 "example-password",75 )Added links property to BaseResponse (#211)euri10before adoptioncad9c03
lines 76–98why, one step back767778@pytest.mark.parametrize(79 "value, expected",80 (81 (82 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',83 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],84 ),85 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),86 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),87 (88 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',89 [90 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},91 {"url": "http://.../back.jpeg"},92 ],93 ),94 ("", []),95 ),96)97def test_parse_header_links(value, expected):98 assert parse_header_links(value) == expectedAdd easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 99–110why, one step back99100101@pytest.mark.asyncio102@pytest.mark.parametrize("httpx_debug", ["0", "1", "True", "False"])103async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):104 os.environ["HTTPX_DEBUG"] = httpx_debug105106 # Force a reload on the logging handlers107 utils._LOGGER_INITIALIZED = False108 utils.get_logger("httpx")109110 async with httpx.AsyncClient() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 111why, one step back111 await client.get(server.url)Add easier debug logging for users (#277)Seth Michael Larsonbefore adoption6f4f186
lines 112–119why, one step back112113 if httpx_debug in ("1", "True"):114 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err115 else:116 assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err117118 # Reset the logger so we don't have verbose output in all unit tests119 logging.getLogger("httpx").handlers = []Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 120–130why, one step back120121122@pytest.mark.asyncio123async def test_elapsed_timer():124 with ElapsedTimer() as timer:125 assert timer.elapsed.total_seconds() == pytest.approx(0, abs=0.05)126 await asyncio.sleep(0.1)127 await asyncio.sleep(128 0.1129 ) # test to ensure time spent after timer exits isn't accounted for.130 assert timer.elapsed.total_seconds() == pytest.approx(0.1, abs=0.05)