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
1import asyncio2import logging3import os45import pytest67import httpx8from httpx import utils9from httpx.utils import (10 ElapsedTimer,11 get_netrc_login,12 guess_json_utf,13 parse_header_links,14)151617@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):49 data = "\ufeff{}".encode(encoding)50 assert guess_json_utf(data) == expected515253def 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 None6162 from httpx import utils6364 utils.NETRC_STATIC_FILES = ()65 os.environ["NETRC"] = ""66 assert utils.get_netrc_login("url") is None676869def 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 )767778@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) == expected99100101@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:111 await client.get(server.url)112113 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 = []120121122@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)