Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With urllib3>=2, mocking HEAD response with content-length header set requires body #708

Closed
KennyChenBasis opened this issue Mar 14, 2024 · 0 comments
Labels

Comments

@KennyChenBasis
Copy link

KennyChenBasis commented Mar 14, 2024

Describe the bug

With urllib3>=2, mocking HEAD responses with content-length header set requires a body with the right length, otherwise an IncompleteRead is raised. This should not be the case since HEAD responses do not have bodies.

Additional context

The fix is probably to pass the request method to HTTPResponse when forming the response. See the code around https://github.com/urllib3/urllib3/blob/733f638a2faa02b4ff8a9f3b5668949d39396b8b/src/urllib3/response.py#L624.

Version of responses

0.25.0

Steps to Reproduce

Run pytest on

import requests
import responses


def test_download():
    url = 'http://test'
    content = b'download me'
    headers = {'Content-length': str(len(content))}
    with responses.RequestsMock() as rsps:
        rsps.head(url, headers=headers)

        res = requests.head(url)
        res.raise_for_status()
        assert int(res.headers['content-length']) == 11
        assert res.text == ''

Expected Result

Test passes, like with urllib3<2.

Actual Result

Test fails with requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read, 11 more expected)', IncompleteRead(0 bytes read, 11 more expected)).

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Mar 14, 2024
@KennyChenBasis KennyChenBasis changed the title With urllib3>=2, mocking HEAD request with content-length header set requires body With urllib3>=2, mocking HEAD response with content-length header set requires body Mar 15, 2024
@markstory markstory added the bug label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

3 participants