Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

288 lines · 8.8 KiB · Python

Last changed by

Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18

History of this file

Take it

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

Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150

line 1why, one step back
1import json

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

line 2why, one step back
2import logging

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 3–5why, one step back
3import os
4import random5

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

line 6why, one step back
6import certifi

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 7–19why, one step back
7import pytest
89import httpx10from httpx._utils import (11 URLPattern,12 get_ca_bundle_from_env,13 get_environment_proxies,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

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 21–36why, 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):

Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150

lines 37–39why, one step back
37 content = '{"abc": 123}'.encode(encoding)
38 response = httpx.Response(200, content=content)39 assert response.json() == {"abc": 123}

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 40–42why, one step back
40
4142def test_bad_utf_like_encoding():

Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150

lines 43–46why, one step back
43 content = b"\x00\x00\x00\x00"
44 response = httpx.Response(200, content=content)45 with pytest.raises(json.decoder.JSONDecodeError):46 response.json()

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 47–58why, one step back
47
4849@pytest.mark.parametrize(50 ("encoding", "expected"),51 (52 ("utf-16-be", "utf-16"),53 ("utf-16-le", "utf-16"),54 ("utf-32-be", "utf-32"),55 ("utf-32-le", "utf-32"),56 ),57)58def test_guess_by_bom(encoding, expected):

Cleanup response.json() method (#2911)Tom Christiebefore adoption1d73150

lines 59–61why, one step back
59 content = '\ufeff{"abc": 123}'.encode(encoding)
60 response = httpx.Response(200, content=content)61 assert response.json() == {"abc": 123}

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 62–86why, one step back
62
6364@pytest.mark.parametrize(65 "value, expected",66 (67 (68 '<http:/.../front.jpeg>; rel=front; type="image/jpeg"',69 [{"url": "http:/.../front.jpeg", "rel": "front", "type": "image/jpeg"}],70 ),71 ("<http:/.../front.jpeg>", [{"url": "http:/.../front.jpeg"}]),72 ("<http:/.../front.jpeg>;", [{"url": "http:/.../front.jpeg"}]),73 (74 '<http:/.../front.jpeg>; type="image/jpeg",<http://.../back.jpeg>;',75 [76 {"url": "http:/.../front.jpeg", "type": "image/jpeg"},77 {"url": "http://.../back.jpeg"},78 ],79 ),80 ("", []),81 ),82)83def test_parse_header_links(value, expected):84 assert parse_header_links(value) == expected8586

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 87–91why, one step back
87def test_logging_request(server, caplog):
88 caplog.set_level(logging.INFO)89 with httpx.Client() as client:90 response = client.get(server.url)91 assert response.status_code == 200

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

line 92why, one step back
92

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 93–99why, one step back
93 assert caplog.record_tuples == [
94 (95 "httpx",96 logging.INFO,97 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',98 )99 ]

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 100–101why, one step back
100
101

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 102–106why, one step back
102def test_logging_redirect_chain(server, caplog):
103 caplog.set_level(logging.INFO)104 with httpx.Client(follow_redirects=True) as client:105 response = client.get(server.url.copy_with(path="/redirect_301"))106 assert response.status_code == 200

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

line 107why, one step back
107

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 108–111why, one step back
108 assert caplog.record_tuples == [
109 (110 "httpx",111 logging.INFO,

Ruff linter: Use the default line-length (#2922)T-256before adoptionfd60b18

lines 112–113why, one step back
112 "HTTP Request: GET http://127.0.0.1:8000/redirect_301"
113 ' "HTTP/1.1 301 Moved Permanently"',

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 114–120why, one step back
114 ),
115 (116 "httpx",117 logging.INFO,118 'HTTP Request: GET http://127.0.0.1:8000/ "HTTP/1.1 200 OK"',119 ),120 ]

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

line 121why, one step back
121

Use standard logging style (#2547)Tom Christiebefore adoptionf1157db

lines 122–140why, one step back
122
123def test_logging_ssl(caplog):124 caplog.set_level(logging.DEBUG)125 with httpx.Client():126 pass127128 cafile = certifi.where()129 assert caplog.record_tuples == [130 (131 "httpx",132 logging.DEBUG,133 "load_ssl_context verify=True cert=None trust_env=True http2=False",134 ),135 (136 "httpx",137 logging.DEBUG,138 f"load_verify_locations cafile='{cafile}'",139 ),140 ]

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 141–193why, one step back
141
142143def test_get_ssl_cert_file():144 # Two environments is not set.145 assert get_ca_bundle_from_env() is None146147 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)148 # SSL_CERT_DIR is correctly set, SSL_CERT_FILE is not set.149 ca_bundle = get_ca_bundle_from_env()150 assert ca_bundle is not None and ca_bundle.endswith("tests")151152 del os.environ["SSL_CERT_DIR"]153 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")154 # SSL_CERT_FILE is correctly set, SSL_CERT_DIR is not set.155 ca_bundle = get_ca_bundle_from_env()156 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")157158 os.environ["SSL_CERT_FILE"] = "wrongfile"159 # SSL_CERT_FILE is set with wrong file, SSL_CERT_DIR is not set.160 assert get_ca_bundle_from_env() is None161162 del os.environ["SSL_CERT_FILE"]163 os.environ["SSL_CERT_DIR"] = "wrongpath"164 # SSL_CERT_DIR is set with wrong path, SSL_CERT_FILE is not set.165 assert get_ca_bundle_from_env() is None166167 os.environ["SSL_CERT_DIR"] = str(TESTS_DIR)168 os.environ["SSL_CERT_FILE"] = str(TESTS_DIR / "test_utils.py")169 # Two environments is correctly set.170 ca_bundle = get_ca_bundle_from_env()171 assert ca_bundle is not None and ca_bundle.endswith("tests/test_utils.py")172173 os.environ["SSL_CERT_FILE"] = "wrongfile"174 # Two environments is set but SSL_CERT_FILE is not a file.175 ca_bundle = get_ca_bundle_from_env()176 assert ca_bundle is not None and ca_bundle.endswith("tests")177178 os.environ["SSL_CERT_DIR"] = "wrongpath"179 # Two environments is set but both are not correct.180 assert get_ca_bundle_from_env() is None181182183@pytest.mark.parametrize(184 ["environment", "proxies"],185 [186 ({}, {}),187 ({"HTTP_PROXY": "http://127.0.0.1"}, {"http://": "http://127.0.0.1"}),188 (189 {"https_proxy": "http://127.0.0.1", "HTTP_PROXY": "https://127.0.0.1"},190 {"https://": "http://127.0.0.1", "http://": "https://127.0.0.1"},191 ),192 ({"all_proxy": "http://127.0.0.1"}, {"all://": "http://127.0.0.1"}),193 ({"TRAVIS_APT_PROXY": "http://127.0.0.1"}, {}),

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

lines 194–199why, one step back
194 ({"no_proxy": "127.0.0.1"}, {"all://127.0.0.1": None}),
195 ({"no_proxy": "192.168.0.0/16"}, {"all://192.168.0.0/16": None}),196 ({"no_proxy": "::1"}, {"all://[::1]": None}),197 ({"no_proxy": "localhost"}, {"all://localhost": None}),198 ({"no_proxy": "github.com"}, {"all://*github.com": None}),199 ({"no_proxy": ".github.com"}, {"all://*.github.com": None}),

Bump uvicorn from 0.18.2 to 0.18.3 (#2357)dependabot[bot]before adoption3a8f886

lines 200–288why, one step back
200 ],
201)202def test_get_environment_proxies(environment, proxies):203 os.environ.update(environment)204205 assert get_environment_proxies() == proxies206207208@pytest.mark.parametrize(209 "headers, output",210 [211 ([("content-type", "text/html")], [("content-type", "text/html")]),212 ([("authorization", "s3kr3t")], [("authorization", "[secure]")]),213 ([("proxy-authorization", "s3kr3t")], [("proxy-authorization", "[secure]")]),214 ],215)216def test_obfuscate_sensitive_headers(headers, output):217 bytes_headers = [(k.encode(), v.encode()) for k, v in headers]218 bytes_output = [(k.encode(), v.encode()) for k, v in output]219 assert list(obfuscate_sensitive_headers(headers)) == output220 assert list(obfuscate_sensitive_headers(bytes_headers)) == bytes_output221222223def test_same_origin():224 origin1 = httpx.URL("https://example.com")225 origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")226 assert same_origin(origin1, origin2)227228229def test_not_same_origin():230 origin1 = httpx.URL("https://example.com")231 origin2 = httpx.URL("HTTP://EXAMPLE.COM")232 assert not same_origin(origin1, origin2)233234235def test_is_https_redirect():236 url = httpx.URL("http://example.com")237 location = httpx.URL("https://example.com")238 assert is_https_redirect(url, location)239240241def test_is_not_https_redirect():242 url = httpx.URL("http://example.com")243 location = httpx.URL("https://www.example.com")244 assert not is_https_redirect(url, location)245246247def test_is_not_https_redirect_if_not_default_ports():248 url = httpx.URL("http://example.com:9999")249 location = httpx.URL("https://example.com:1337")250 assert not is_https_redirect(url, location)251252253@pytest.mark.parametrize(254 ["pattern", "url", "expected"],255 [256 ("http://example.com", "http://example.com", True),257 ("http://example.com", "https://example.com", False),258 ("http://example.com", "http://other.com", False),259 ("http://example.com:123", "http://example.com:123", True),260 ("http://example.com:123", "http://example.com:456", False),261 ("http://example.com:123", "http://example.com", False),262 ("all://example.com", "http://example.com", True),263 ("all://example.com", "https://example.com", True),264 ("http://", "http://example.com", True),265 ("http://", "https://example.com", False),266 ("all://", "https://example.com:123", True),267 ("", "https://example.com:123", True),268 ],269)270def test_url_matches(pattern, url, expected):271 pattern = URLPattern(pattern)272 assert pattern.matches(httpx.URL(url)) == expected273274275def test_pattern_priority():276 matchers = [277 URLPattern("all://"),278 URLPattern("http://"),279 URLPattern("http://example.com"),280 URLPattern("http://example.com:123"),281 ]282 random.shuffle(matchers)283 assert sorted(matchers) == [284 URLPattern("http://example.com:123"),285 URLPattern("http://example.com"),286 URLPattern("http://"),287 URLPattern("all://"),288 ]