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

Prefer Content-Length over Transfer-Encoding: chunked for content=<file-like> cases. #1619

Merged
merged 3 commits into from
Apr 30, 2021

Conversation

tomchristie
Copy link
Member

@tomchristie tomchristie commented Apr 30, 2021

Closes #1490

Step 1: Add failing test case for content=io.BytesIO(...):

def test_bytesio_content():
    headers, stream = encode_request(content=io.BytesIO(b"Hello, world!"))
    assert isinstance(stream, typing.Iterable)
    assert not isinstance(stream, typing.AsyncIterable)

    content = b"".join([part for part in stream])

    assert headers == {"Content-Length": "13"}  # Currently returns {'Transfer-Encoding': 'chunked'}
    assert content == b"Hello, world!"

Step 2: Refactor peek_filelike_length to return Optional[int], rather than raising an exception when the length cannot be determined.

Step 3: Use peek_filelike_length in the elif isinstance(content, Iterable): case of encode_content(...) -> (headers, stream), switching between either Content-Length: <length> or Transfer-Encoding: chunked

@tomchristie tomchristie added the enhancement New feature or request label Apr 30, 2021
@tomchristie tomchristie merged commit 2129a97 into master Apr 30, 2021
@tomchristie tomchristie deleted the use-content-length-for-filelike-content branch April 30, 2021 09:40
@tomchristie tomchristie mentioned this pull request Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

detect length of file-like object?
2 participants