File fix_issues_with_urllib3_v2.patch of Package python-requests-mock.37845
From 455f7d95a0ff6892d6393d7190c901f10db4ff93 Mon Sep 17 00:00:00 2001
From: Jamie Lennox <jamie.lennox@agoda.com>
Date: Wed, 31 May 2023 10:36:15 +0700
Subject: [PATCH] Tell urllib3 to ignore content length mismatch
In urllib3 v2 they default enforce_content_length to true. This fails
now when you mock a request that doesn't have any data that can be
partially read.
Fixes: #228
---
...content-length-mismatch-d98e782a25c41266.yaml | 7 +++++++
requests_mock/response.py | 1 +
tests/test_mocker.py | 16 ++++++++++++++++
3 files changed, 24 insertions(+)
create mode 100644 releasenotes/notes/urllib3-2-content-length-mismatch-d98e782a25c41266.yaml
diff --git a/releasenotes/notes/urllib3-2-content-length-mismatch-d98e782a25c41266.yaml b/releasenotes/notes/urllib3-2-content-length-mismatch-d98e782a25c41266.yaml
new file mode 100644
index 0000000..7cbfd8a
--- /dev/null
+++ b/releasenotes/notes/urllib3-2-content-length-mismatch-d98e782a25c41266.yaml
@@ -0,0 +1,7 @@
+---
+fixes:
+ - |
+ Fix incompatibility with urllib3 >2.0.0. In 2.0.0 they default to enforcing
+ content length checking on returned bodies in responses from the previous
+ default of false. However the flag is still available so for compatibility
+ we can just default the other way.
diff --git a/requests_mock/response.py b/requests_mock/response.py
index 3643625..5855539 100644
--- a/requests_mock/response.py
+++ b/requests_mock/response.py
@@ -195,6 +195,7 @@ def create_response(request, **kwargs):
headers=headers,
body=body or _IOReader(six.b('')),
decode_content=False,
+ enforce_content_length=False,
preload_content=False,
original_response=None)
diff --git a/tests/test_mocker.py b/tests/test_mocker.py
index 8d015cd..e99f7d3 100644
--- a/tests/test_mocker.py
+++ b/tests/test_mocker.py
@@ -625,3 +625,19 @@ def encode(s, o):
m.get("http://test", json={"a": "b"}, json_encoder=MyJsonEncoder)
res = requests.get("http://test")
self.assertEqual(test_val, res.text)
+
+ @requests_mock.mock()
+ def test_mismatch_content_length_streaming(self, m):
+ url = "https://test/package.tar.gz"
+
+ def f(request, context):
+ context.headers["Content-Length"] = "300810"
+ return None
+
+ m.head(
+ url=url,
+ status_code=200,
+ text=f,
+ )
+
+ requests.head(url)