Skip to content
Myr

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 file

Take it

1import logging2import os34import pytest56import httpx7from httpx import utils8from httpx.utils import get_netrc_login, guess_json_utf, parse_header_links91011@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):43    data = "\ufeff{}".encode(encoding)44    assert guess_json_utf(data) == expected454647def 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 None5556    from httpx import utils5758    utils.NETRC_STATIC_FILES = ()59    os.environ["NETRC"] = ""60    assert utils.get_netrc_login("url") is None616263def 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    )707172@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) == expected939495@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:105        await client.get(server.url)106107    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 = []