Skip to content

Commit

Permalink
Fixes #1033 - Ensure that text that spans 3 invocations before newlin…
Browse files Browse the repository at this point in the history
…e is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)

Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines.
  • Loading branch information
scr authored Jul 21, 2020
1 parent 27b0dbc commit ad5e6eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion httpx/_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def decode(self, text: str) -> typing.List[str]:
text = text[idx + 1 :]
break
elif next_char is None:
self.buffer = text
self.buffer += text
text = ""
break

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ trustme
uvicorn
seed-isort-config

attrs>=19.2 # See: https://github.com/encode/httpx/pull/566#issuecomment-559862665
attrs>=19.3.0 # See: https://github.com/encode/httpx/pull/566#issuecomment-559862665
28 changes: 28 additions & 0 deletions tests/test_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ def test_line_decoder_nl():
assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]
assert decoder.flush() == []

# Issue #1033
decoder = LineDecoder()
assert decoder.decode("") == []
assert decoder.decode("12345\n") == ["12345\n"]
assert decoder.decode("foo ") == []
assert decoder.decode("bar ") == []
assert decoder.decode("baz\n") == ["foo bar baz\n"]
assert decoder.flush() == []


def test_line_decoder_cr():
decoder = LineDecoder()
Expand All @@ -237,6 +246,16 @@ def test_line_decoder_cr():
assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]
assert decoder.flush() == ["c\n"]

# Issue #1033
# TODO: This seems like another bug; fix expectations and results.
decoder = LineDecoder()
assert decoder.decode("") == []
assert decoder.decode("12345\r") == []
assert decoder.decode("foo ") == []
assert decoder.decode("bar ") == []
assert decoder.decode("baz\r") == []
assert decoder.flush() == ["12345\rfoo bar baz\n"]


def test_line_decoder_crnl():
decoder = LineDecoder()
Expand All @@ -255,6 +274,15 @@ def test_line_decoder_crnl():
assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]
assert decoder.flush() == ["c"]

# Issue #1033
decoder = LineDecoder()
assert decoder.decode("") == []
assert decoder.decode("12345\r\n") == ["12345\n"]
assert decoder.decode("foo ") == []
assert decoder.decode("bar ") == []
assert decoder.decode("baz\r\n") == ["foo bar baz\n"]
assert decoder.flush() == []


def test_invalid_content_encoding_header():
headers = [(b"Content-Encoding", b"invalid-header")]
Expand Down

0 comments on commit ad5e6eb

Please sign in to comment.