Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

150 lines · 4.5 KiB · Python

Last changed by

Move utility functions from _utils.py to _client.py (#3389)RafaelWObefore adoption7b19cd5

History of this file

Take it

1import json2import logging3import os4import random56import pytest78import httpx9from httpx._utils import URLPattern, get_environment_proxies101112@pytest.mark.parametrize(13    "encoding",14    (15        "utf-32",16        "utf-8-sig",17        "utf-16",18        "utf-8",19        "utf-16-be",20        "utf-16-le",21        "utf-32-be",22        "utf-32-le",23    ),24)25def test_encoded(encoding):26    content = '{"abc": 123}'.encode(encoding)27    response = httpx.Response(200, content=content)28    assert response.json() == {"abc": 123}293031def test_bad_utf_like_encoding():32    content = b"\x00\x00\x00\x00"33    response = httpx.Response(200, content=content)34    with pytest.raises(json.decoder.JSONDecodeError):35        response.json()363738@pytest.mark.parametrize(39    ("encoding", "expected"),40    (41        ("utf-16-be", "utf-16"),42        ("utf-16-le", "utf-16"),43        ("utf-32-be", "utf-32"),44        ("utf-32-le", "utf-32"),45    ),46)47def test_guess_by_bom(encoding, expected):48    content = '\ufeff{"abc": 123}'.encode(encoding)49    response = httpx.Response(200, content=content)50    assert response.json() == {"abc": 123}515253def test_logging_request(server, caplog):54    caplog.set_level(logging.INFO)55    with httpx.Client() as client:56        response = client.get(server.url)57        assert response.status_code == 2005859    assert caplog.record_tuples == [60        (61            "httpx",62            logging.INFO,63            'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',64        )65    ]666768def test_logging_redirect_chain(server, caplog):69    caplog.set_level(logging.INFO)70    with httpx.Client(follow_redirects=True) as client:71        response = client.get(server.url.copy_with(path="/redirect_301"))72        assert response.status_code == 2007374    assert caplog.record_tuples == [75        (76            "httpx",77            logging.INFO,78            "HTTP Request: GET http://127.0.0.1:8000/redirect_301"79            ' "HTTP/1.1 301 Moved Permanently"',80        ),81        (82            "httpx",83            logging.INFO,84            'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',85        ),86    ]878889@pytest.mark.parametrize(90    ["environment", "proxies"],91    [92        ({}, {}),93        ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),94        (95            {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},96            {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},97        ),98        ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),99        ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),100        ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),101        ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),102        ({"no_proxy": "::1"}, {"all://[::1]": None}),103        ({"no_proxy": "localhost"}, {"all://localhost": None}),104        ({"no_proxy": "github.com"}, {"all://*github.com": None}),105        ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),106        ({"no_proxy": "http://github.com"}, {"http://github.com": None}),107    ],108)109def test_get_environment_proxies(environment, proxies):110    os.environ.update(environment)111112    assert get_environment_proxies() == proxies113114115@pytest.mark.parametrize(116    ["pattern", "url", "expected"],117    [118        ("http://example.com", "http://example.com", True),119        ("http://example.com", "https://example.com", False),120        ("http://example.com", "http://other.com", False),121        ("http://example.com:123", "http://example.com:123", True),122        ("http://example.com:123", "http://example.com:456", False),123        ("http://example.com:123", "http://example.com", False),124        ("all://example.com", "http://example.com", True),125        ("all://example.com", "https://example.com", True),126        ("http://", "http://example.com", True),127        ("http://", "https://example.com", False),128        ("all://", "https://example.com:123", True),129        ("", "https://example.com:123", True),130    ],131)132def test_url_matches(pattern, url, expected):133    pattern = URLPattern(pattern)134    assert pattern.matches(httpx.URL(url)) == expected135136137def test_pattern_priority():138    matchers = [139        URLPattern("all://"),140        URLPattern("http://"),141        URLPattern("http://example.com"),142        URLPattern("http://example.com:123"),143    ]144    random.shuffle(matchers)145    assert sorted(matchers) == [146        URLPattern("http://example.com:123"),147        URLPattern("http://example.com"),148        URLPattern("http://"),149        URLPattern("all://"),150    ]