Merge pull request #6757 from nateprewitt/urllib-1.x-tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by GitHub · parents 15e1f17, 01353d3 · Browse files · 4 files·+60 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+60 added−18 removed
- tests:+24 added−17 removed
- other:+36 added−1 removed
Provenance
- Commit
79b74ef- Tree
7ef5951- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
15e1f17- Parent 2
01353d3
4 files changed+60 added−18 removed
modified:.github/workflows/run-tests.yml+20 added
55 unmodified lines56575859606162636465666768697071727374757677787955 unmodified lines python -m pip uninstall -y "charset_normalizer" "chardet" - name: Run tests run: | make ci
urllib3: name: 'urllib3 1.x' runs-on: 'ubuntu-latest' strategy: fail-fast: true
steps: - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - name: 'Set up Python 3.8' uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d with: python-version: '3.8' - name: Install dependencies run: | make python -m pip install "urllib3<2" - name: Run tests run: | make cimodified:src/requests/compat.py+12 added
8 unmodified lines9 10 import importlib11 import sys12 13+# -------14+# urllib315+# -------16+from urllib3 import __version__ as urllib3_version17+18+# Detect which major version of urllib3 is being used.19+try:20+ is_urllib3_1 = int(urllib3_version.split(".")[0]) == 121+except (TypeError, AttributeError):22+ # If we can't discern a version, prefer old functionality.23+ is_urllib3_1 = True24+25 # -------------------26 # Character Detection27 # -------------------28 78 unmodified linesmodified:src/requests/utils.py+4 added-1 removed
36 unmodified lines37 bytes,38 getproxies,39 getproxies_environment,40 integer_types,41+ is_urllib3_1,42 )43 from .compat import parse_http_list as _parse_list_header44 from .compat import (45 proxy_bypass,90 unmodified lines136 def super_len(o):137 total_length = None138 current_position = 0139 139- if isinstance(o, str):140+ if not is_urllib3_1 and isinstance(o, str):141+ # urllib3 2.x+ treats all strings as utf-8 instead142+ # of latin-1 (iso-8859-1) like http.client.143 o = o.encode("utf-8")144 145 if hasattr(o, "__len__"):146 total_length = len(o)953 unmodified linesmodified:tests/test_requests.py+24 added-17 removed
23 unmodified lines24 MutableMapping,25 builtin_str,26 cookielib,27 getproxies,28+ is_urllib3_1,29 urlparse,30 )31 from requests.cookies import cookiejar_from_dict, morsel_to_cookie32 from requests.exceptions import (1777 unmodified lines1810 p = req.prepare()1811 1812 assert p.headers["Content-Length"] == length1813 1813- def test_content_length_for_bytes_data(self, httpbin):1814- data = "This is a string containing multi-byte UTF-8 ☃️"1815- encoded_data = data.encode("utf-8")1816- length = str(len(encoded_data))1817- req = requests.Request("POST", httpbin("post"), data=encoded_data)1818- p = req.prepare()1819-1820- assert p.headers["Content-Length"] == length1821-1822- def test_content_length_for_string_data_counts_bytes(self, httpbin):1823- data = "This is a string containing multi-byte UTF-8 ☃️"1824- length = str(len(data.encode("utf-8")))1825- req = requests.Request("POST", httpbin("post"), data=data)1826- p = req.prepare()1827-1828- assert p.headers["Content-Length"] == length1829-1814 def test_nonhttp_schemes_dont_check_URLs(self):1815 test_urls = (1816 "data:image/gif;base64,R0lGODlhAQABAHAAACH5BAUAAAAALAAAAAABAAEAAAICRAEAOw==",1817 "file:///etc/passwd",1131 unmodified lines2949 2950 assert client_cert is not None2951 2952 2953+def test_content_length_for_bytes_data(httpbin):2954+ data = "This is a string containing multi-byte UTF-8 ☃️"2955+ encoded_data = data.encode("utf-8")2956+ length = str(len(encoded_data))2957+ req = requests.Request("POST", httpbin("post"), data=encoded_data)2958+ p = req.prepare()2959+2960+ assert p.headers["Content-Length"] == length2961+2962+2963+@pytest.mark.skipif(2964+ is_urllib3_1,2965+ reason="urllib3 2.x encodes all strings to utf-8, urllib3 1.x uses latin-1",2966+)2967+def test_content_length_for_string_data_counts_bytes(httpbin):2968+ data = "This is a string containing multi-byte UTF-8 ☃️"2969+ length = str(len(data.encode("utf-8")))2970+ req = requests.Request("POST", httpbin("post"), data=data)2971+ p = req.prepare()2972+2973+ assert p.headers["Content-Length"] == length2974+2975+2976 def test_json_decode_errors_are_serializable_deserializable():2977 json_decode_error = requests.exceptions.JSONDecodeError(2978 "Extra data",2979 '{"responseCode":["706"],"data":null}{"responseCode":["706"],"data":null}',4 unmodified lines