Skip to content
Myr

Refactor debug and trace log tests (#506)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by GitHub · parent 07586f9 · Browse files · 2 files+36 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+36 added−18 removed

  • tests:+36 added−18 removed

2 files+36 added−18 removed

modified:tests/test_utils.py+16 added-18 removed
1import asyncio2import logging2import os34import pytest56import httpx8from httpx import utils7from httpx.utils import (8 ElapsedTimer,9 get_ca_bundle_from_env,10 get_environment_proxies,
2 unmodified lines
13 obfuscate_sensitive_headers,14 parse_header_links,15 should_not_be_proxied,16)17from tests.utils import override_log_level181920@pytest.mark.parametrize(21 "encoding",
79 unmodified lines
101 assert parse_header_links(value) == expected102103104@pytest.mark.asyncio106@pytest.mark.parametrize("httpx_log_level", ["trace", "debug"])107async def test_httpx_log_level_enabled_stderr_logging(server, capsys, httpx_log_level):108 os.environ["HTTPX_LOG_LEVEL"] = httpx_log_level105async def test_logs_debug(server, capsys):106 with override_log_level("debug"):107 async with httpx.AsyncClient() as client:108 response = await client.get(server.url)109 assert response.status_code == 200110 stderr = capsys.readouterr().err111 assert "httpx.dispatch.connection_pool" not in stderr112110 # Force a reload on the logging handlers111 utils._LOGGER_INITIALIZED = False112 utils.get_logger("httpx")113114@pytest.mark.asyncio115async def test_logs_trace(server, capsys):116 with override_log_level("trace"):114 async with httpx.AsyncClient() as client:115 await client.get(server.url)117 async with httpx.AsyncClient() as client:118 response = await client.get(server.url)119 assert response.status_code == 200120 stderr = capsys.readouterr().err121 assert "httpx.dispatch.connection_pool" in stderr122117 if httpx_log_level == "trace":118 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err119 else:120 assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err123122 # Reset the logger so we don't have verbose output in all unit tests123 logging.getLogger("httpx").handlers = []124125124def test_get_ssl_cert_file():125 # Two environments is not set.126 assert get_ca_bundle_from_env() is None127
151 unmodified lines
added:tests/utils.py+20 added
1import contextlib2import logging3import os45from httpx import utils678@contextlib.contextmanager9def override_log_level(log_level: str):10 os.environ["HTTPX_LOG_LEVEL"] = log_level1112 # Force a reload on the logging handlers13 utils._LOGGER_INITIALIZED = False14 utils.get_logger("httpx")1516 try:17 yield18 finally:19 # Reset the logger so we don't have verbose output in all unit tests20 logging.getLogger("httpx").handlers = []