Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

89 lines · 2.1 KiB · Python

Last changed by

Added links property to BaseResponse (#211)euri10before adoptioncad9c03

History of this file

Take it

1import os23import pytest45from httpx.utils import get_netrc_login, guess_json_utf, parse_header_links678@pytest.mark.parametrize(9    "encoding",10    (11        "utf-32",12        "utf-8-sig",13        "utf-16",14        "utf-8",15        "utf-16-be",16        "utf-16-le",17        "utf-32-be",18        "utf-32-le",19    ),20)21def test_encoded(encoding):22    data = "{}".encode(encoding)23    assert guess_json_utf(data) == encoding242526def test_bad_utf_like_encoding():27    assert guess_json_utf(b"\x00\x00\x00\x00") is None282930@pytest.mark.parametrize(31    ("encoding", "expected"),32    (33        ("utf-16-be", "utf-16"),34        ("utf-16-le", "utf-16"),35        ("utf-32-be", "utf-32"),36        ("utf-32-le", "utf-32"),37    ),38)39def test_guess_by_bom(encoding, expected):40    data = "\ufeff{}".encode(encoding)41    assert guess_json_utf(data) == expected424344def test_bad_get_netrc_login():45    assert get_netrc_login("url") is None4647    os.environ["NETRC"] = "tests/.netrc"48    assert get_netrc_login("url") is None4950    os.environ["NETRC"] = "wrongpath"51    assert get_netrc_login("url") is None5253    from httpx import utils5455    utils.NETRC_STATIC_FILES = ()56    os.environ["NETRC"] = ""57    assert utils.get_netrc_login("url") is None585960def test_get_netrc_login():61    os.environ["NETRC"] = "tests/.netrc"62    assert get_netrc_login("netrcexample.org") == (63        "example-username",64        None,65        "example-password",66    )676869@pytest.mark.parametrize(70    "value, expected",71    (72        (73            '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',74            [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],75        ),76        ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),77        ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),78        (79            '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',80            [81                {"url": "http:/.../front.jpeg", "type": "image/jpeg"},82                {"url": "http://.../back.jpeg"},83            ],84        ),85        ("", []),86    ),87)88def test_parse_header_links(value, expected):89    assert parse_header_links(value) == expected