Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

282 lines · 8.5 KiB · Python

Last changed by

fix: NO_PROXY should support IPv4, IPv6 and localhost (#2659)Jiayun Shenbefore adoption15d09a3

History of this file

Take it

The walk stopped after 200 commits: lines older than 3a82176 point at it.

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

line 1why, one step back
1import logging

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

lines 2–4why, one step back
2import os
3import random4

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

line 5why, one step back
5import certifi

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

lines 6–19why, one step back
6import pytest
78import httpx9from httpx._utils import (10 URLPattern,11 get_ca_bundle_from_env,12 get_environment_proxies,13 guess_json_utf,14 is_https_redirect,15 obfuscate_sensitive_headers,16 parse_header_links,17 same_origin,18)19

Add `NetRCAuth()` class. (#2535)Tom Christiebefore adoption59914c7

line 20why, one step back
20from .common import TESTS_DIR

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

lines 21–81why, one step back
21
2223@pytest.mark.parametrize(24 "encoding",25 (26 "utf-32",27 "utf-8-sig",28 "utf-16",29 "utf-8",30 "utf-16-be",31 "utf-16-le",32 "utf-32-be",33 "utf-32-le",34 ),35)36def test_encoded(encoding):37 data = "{}".encode(encoding)38 assert guess_json_utf(data) == encoding394041def test_bad_utf_like_encoding():42 assert guess_json_utf(b"\x00\x00\x00\x00") is None434445@pytest.mark.parametrize(46 ("encoding", "expected"),47 (48 ("utf-16-be", "utf-16"),49 ("utf-16-le", "utf-16"),50 ("utf-32-be", "utf-32"),51 ("utf-32-le", "utf-32"),52 ),53)54def test_guess_by_bom(encoding, expected):55 data = "\ufeff{}".encode(encoding)56 assert guess_json_utf(data) == expected575859@pytest.mark.parametrize(60 "value, expected",61 (62 (63 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',64 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],65 ),66 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),67 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),68 (69 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',70 [71 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},72 {"url": "http://.../back.jpeg"},73 ],74 ),75 ("", []),76 ),77)78def test_parse_header_links(value, expected):79 assert parse_header_links(value) == expected8081

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 82–86why, one step back
82def test_logging_request(server, caplog):
83 caplog.set_level(logging.INFO)84 with httpx.Client() as client:85 response = client.get(server.url)86 assert response.status_code == 200

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

line 87why, one step back
87

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 88–94why, one step back
88 assert caplog.record_tuples == [
89 (90 "httpx",91 logging.INFO,92 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',93 )94 ]

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

lines 95–96why, one step back
95
96

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 97–101why, one step back
97def test_logging_redirect_chain(server, caplog):
98 caplog.set_level(logging.INFO)99 with httpx.Client(follow_redirects=True) as client:100 response = client.get(server.url.copy_with(path="/redirect_301"))101 assert response.status_code == 200

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

line 102why, one step back
102

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 103–114why, one step back
103 assert caplog.record_tuples == [
104 (105 "httpx",106 logging.INFO,107 'HTTP Request: GET http://127.0.0.1:8000/redirect_301 "HTTP/1.1 301 Moved Permanently"',108 ),109 (110 "httpx",111 logging.INFO,112 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',113 ),114 ]

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

line 115why, one step back
115

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 116–134why, one step back
116
117def test_logging_ssl(caplog):118 caplog.set_level(logging.DEBUG)119 with httpx.Client():120 pass121122 cafile = certifi.where()123 assert caplog.record_tuples == [124 (125 "httpx",126 logging.DEBUG,127 "load_ssl_context verify=True cert=None trust_env=True http2=False",128 ),129 (130 "httpx",131 logging.DEBUG,132 f"load_verify_locations cafile='{cafile}'",133 ),134 ]

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

lines 135–187why, one step back
135
136137def test_get_ssl_cert_file():138 # Two environments is not set.139 assert get_ca_bundle_from_env() is None140141 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)142 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.143 ca_bundle = get_ca_bundle_from_env()144 assert ca_bundle is not None and ca_bundle.endswith("tests")145146 del os.environ["SSL_CERT_DIR"]147 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")148 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.149 ca_bundle = get_ca_bundle_from_env()150 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")151152 os.environ["SSL_CERT_FILE"] = "wrongfile"153 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.154 assert get_ca_bundle_from_env() is None155156 del os.environ["SSL_CERT_FILE"]157 os.environ["SSL_CERT_DIR"] = "wrongpath"158 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.159 assert get_ca_bundle_from_env() is None160161 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)162 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")163 # Two environments is correctly set.164 ca_bundle = get_ca_bundle_from_env()165 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")166167 os.environ["SSL_CERT_FILE"] = "wrongfile"168 # Two environments is set but SSL_CERT_FILE is not a file.169 ca_bundle = get_ca_bundle_from_env()170 assert ca_bundle is not None and ca_bundle.endswith("tests")171172 os.environ["SSL_CERT_DIR"] = "wrongpath"173 # Two environments is set but both are not correct.174 assert get_ca_bundle_from_env() is None175176177@pytest.mark.parametrize(178 ["environment", "proxies"],179 [180 ({}, {}),181 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),182 (183 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},184 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},185 ),186 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),187 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),

fix: NO_PROXY should support IPv4, IPv6 and localhost (#2659)Jiayun Shenbefore adoption15d09a3

lines 188–193why, one step back
188 ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),
189 ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),190 ({"no_proxy": "::1"}, {"all://[::1]": None}),191 ({"no_proxy": "localhost"}, {"all://localhost": None}),192 ({"no_proxy": "github.com"}, {"all://*github.com": None}),193 ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),

Rework docs structure (#2308)Florimond Mancabefore adoption3a82176

lines 194–282why, one step back
194 ],
195)196def test_get_environment_proxies(environment, proxies):197 os.environ.update(environment)198199 assert get_environment_proxies() == proxies200201202@pytest.mark.parametrize(203 "headers, output",204 [205 ([("content-type", "text/html")], [("content-type", "text/html")]),206 ([("authorization", "s3kr3t")], [("authorization", "[secure]")]),207 ([("proxy-authorization", "s3kr3t")], [("proxy-authorization", "[secure]")]),208 ],209)210def test_obfuscate_sensitive_headers(headers, output):211 bytes_headers = [(k.encode(), v.encode()) for k, v in headers]212 bytes_output = [(k.encode(), v.encode()) for k, v in output]213 assert list(obfuscate_sensitive_headers(headers)) == output214 assert list(obfuscate_sensitive_headers(bytes_headers)) == bytes_output215216217def test_same_origin():218 origin1 = httpx.URL("https://example.com")219 origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")220 assert same_origin(origin1, origin2)221222223def test_not_same_origin():224 origin1 = httpx.URL("https://example.com")225 origin2 = httpx.URL("HTTP://EXAMPLE.COM")226 assert not same_origin(origin1, origin2)227228229def test_is_https_redirect():230 url = httpx.URL("http://example.com")231 location = httpx.URL("https://example.com")232 assert is_https_redirect(url, location)233234235def test_is_not_https_redirect():236 url = httpx.URL("http://example.com")237 location = httpx.URL("https://www.example.com")238 assert not is_https_redirect(url, location)239240241def test_is_not_https_redirect_if_not_default_ports():242 url = httpx.URL("http://example.com:9999")243 location = httpx.URL("https://example.com:1337")244 assert not is_https_redirect(url, location)245246247@pytest.mark.parametrize(248 ["pattern", "url", "expected"],249 [250 ("http://example.com", "http://example.com", True),251 ("http://example.com", "https://example.com", False),252 ("http://example.com", "http://other.com", False),253 ("http://example.com:123", "http://example.com:123", True),254 ("http://example.com:123", "http://example.com:456", False),255 ("http://example.com:123", "http://example.com", False),256 ("all://example.com", "http://example.com", True),257 ("all://example.com", "https://example.com", True),258 ("http://", "http://example.com", True),259 ("http://", "https://example.com", False),260 ("all://", "https://example.com:123", True),261 ("", "https://example.com:123", True),262 ],263)264def test_url_matches(pattern, url, expected):265 pattern = URLPattern(pattern)266 assert pattern.matches(httpx.URL(url)) == expected267268269def test_pattern_priority():270 matchers = [271 URLPattern("all://"),272 URLPattern("http://"),273 URLPattern("http://example.com"),274 URLPattern("http://example.com:123"),275 ]276 random.shuffle(matchers)277 assert sorted(matchers) == [278 URLPattern("http://example.com:123"),279 URLPattern("http://example.com"),280 URLPattern("http://"),281 URLPattern("all://"),282 ]