test_utils.py
This file
Path
tests/test_utils.py
209 lines · 6.0 KiB · Python
Last changed by
Move remaining utility functions from _utils.py to _models.py (#3387)RafaelWObefore adoption41597ad
History of this fileTake it
1import json2import logging3import os4import random56import pytest78import httpx9from httpx._utils import (10 URLPattern,11 get_environment_proxies,12)131415@pytest.mark.parametrize(16 "encoding",17 (18 "utf-32",19 "utf-8-sig",20 "utf-16",21 "utf-8",22 "utf-16-be",23 "utf-16-le",24 "utf-32-be",25 "utf-32-le",26 ),27)28def test_encoded(encoding):29 content = '{"abc": 123}'.encode(encoding)30 response = httpx.Response(200, content=content)31 assert response.json() == {"abc": 123}323334def test_bad_utf_like_encoding():35 content = b"\x00\x00\x00\x00"36 response = httpx.Response(200, content=content)37 with pytest.raises(json.decoder.JSONDecodeError):38 response.json()394041@pytest.mark.parametrize(42 ("encoding", "expected"),43 (44 ("utf-16-be", "utf-16"),45 ("utf-16-le", "utf-16"),46 ("utf-32-be", "utf-32"),47 ("utf-32-le", "utf-32"),48 ),49)50def test_guess_by_bom(encoding, expected):51 content = '\ufeff{"abc": 123}'.encode(encoding)52 response = httpx.Response(200, content=content)53 assert response.json() == {"abc": 123}545556def test_logging_request(server, caplog):57 caplog.set_level(logging.INFO)58 with httpx.Client() as client:59 response = client.get(server.url)60 assert response.status_code == 2006162 assert caplog.record_tuples == [63 (64 "httpx",65 logging.INFO,66 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',67 )68 ]697071def test_logging_redirect_chain(server, caplog):72 caplog.set_level(logging.INFO)73 with httpx.Client(follow_redirects=True) as client:74 response = client.get(server.url.copy_with(path="/redirect_301"))75 assert response.status_code == 2007677 assert caplog.record_tuples == [78 (79 "httpx",80 logging.INFO,81 "HTTP Request: GET http://127.0.0.1:8000/redirect_301"82 ' "HTTP/1.1 301 Moved Permanently"',83 ),84 (85 "httpx",86 logging.INFO,87 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',88 ),89 ]909192@pytest.mark.parametrize(93 ["environment", "proxies"],94 [95 ({}, {}),96 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),97 (98 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},99 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},100 ),101 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),102 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),103 ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),104 ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),105 ({"no_proxy": "::1"}, {"all://[::1]": None}),106 ({"no_proxy": "localhost"}, {"all://localhost": None}),107 ({"no_proxy": "github.com"}, {"all://*github.com": None}),108 ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),109 ({"no_proxy": "http://github.com"}, {"http://github.com": None}),110 ],111)112def test_get_environment_proxies(environment, proxies):113 os.environ.update(environment)114115 assert get_environment_proxies() == proxies116117118def test_same_origin():119 origin = httpx.URL("https://example.com")120 request = httpx.Request("GET", "HTTPS://EXAMPLE.COM:443")121122 client = httpx.Client()123 headers = client._redirect_headers(request, origin, "GET")124125 assert headers["Host"] == request.url.netloc.decode("ascii")126127128def test_not_same_origin():129 origin = httpx.URL("https://example.com")130 request = httpx.Request("GET", "HTTP://EXAMPLE.COM:80")131132 client = httpx.Client()133 headers = client._redirect_headers(request, origin, "GET")134135 assert headers["Host"] == origin.netloc.decode("ascii")136137138def test_is_https_redirect():139 url = httpx.URL("https://example.com")140 request = httpx.Request(141 "GET", "http://example.com", headers={"Authorization": "empty"}142 )143144 client = httpx.Client()145 headers = client._redirect_headers(request, url, "GET")146147 assert "Authorization" in headers148149150def test_is_not_https_redirect():151 url = httpx.URL("https://www.example.com")152 request = httpx.Request(153 "GET", "http://example.com", headers={"Authorization": "empty"}154 )155156 client = httpx.Client()157 headers = client._redirect_headers(request, url, "GET")158159 assert "Authorization" not in headers160161162def test_is_not_https_redirect_if_not_default_ports():163 url = httpx.URL("https://example.com:1337")164 request = httpx.Request(165 "GET", "http://example.com:9999", headers={"Authorization": "empty"}166 )167168 client = httpx.Client()169 headers = client._redirect_headers(request, url, "GET")170171 assert "Authorization" not in headers172173174@pytest.mark.parametrize(175 ["pattern", "url", "expected"],176 [177 ("http://example.com", "http://example.com", True),178 ("http://example.com", "https://example.com", False),179 ("http://example.com", "http://other.com", False),180 ("http://example.com:123", "http://example.com:123", True),181 ("http://example.com:123", "http://example.com:456", False),182 ("http://example.com:123", "http://example.com", False),183 ("all://example.com", "http://example.com", True),184 ("all://example.com", "https://example.com", True),185 ("http://", "http://example.com", True),186 ("http://", "https://example.com", False),187 ("all://", "https://example.com:123", True),188 ("", "https://example.com:123", True),189 ],190)191def test_url_matches(pattern, url, expected):192 pattern = URLPattern(pattern)193 assert pattern.matches(httpx.URL(url)) == expected194195196def test_pattern_priority():197 matchers = [198 URLPattern("all://"),199 URLPattern("http://"),200 URLPattern("http://example.com"),201 URLPattern("http://example.com:123"),202 ]203 random.shuffle(matchers)204 assert sorted(matchers) == [205 URLPattern("http://example.com:123"),206 URLPattern("http://example.com"),207 URLPattern("http://"),208 URLPattern("all://"),209 ]