Skip to content

Commit

Permalink
Tell urllib3 to ignore content length mismatch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jamielennox committed Jun 8, 2023
1 parent dc806b3 commit dcbef4f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions requests_mock/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,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)

0 comments on commit dcbef4f

Please sign in to comment.