Skip to content

Add, test support for chunk extensions #46

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

Merged
merged 3 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _readinto(self, buf):
# Consume trailing \r\n for chunks 2+
if self._remaining == 0:
self._throw_away(2)
chunk_header = self._readto(b";", b"\r\n")
chunk_header = self._readto(b"\r\n").split(b";", 1)[0]
http_chunk_size = int(bytes(chunk_header), 16)
if http_chunk_size == 0:
self._chunked = False
Expand Down Expand Up @@ -253,7 +253,7 @@ def close(self):
self._throw_away(self._remaining)
elif self._chunked:
while True:
chunk_header = self._readto(b";", b"\r\n")
chunk_header = self._readto(b"\r\n").split(b";", 1)[0]
chunk_size = int(bytes(chunk_header), 16)
if chunk_size == 0:
break
Expand Down
32 changes: 26 additions & 6 deletions tests/chunk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
headers = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"


def _chunk(response, split):
def _chunk(response, split, extra=b""):
i = 0
chunked = b""
while i < len(response):
Expand All @@ -19,18 +19,22 @@ def _chunk(response, split):
chunk_size = remaining
new_i = i + chunk_size
chunked += (
hex(chunk_size)[2:].encode("ascii") + b"\r\n" + response[i:new_i] + b"\r\n"
hex(chunk_size)[2:].encode("ascii")
+ extra
+ b"\r\n"
+ response[i:new_i]
+ b"\r\n"
)
i = new_i
# The final chunk is zero length.
chunked += b"0\r\n\r\n"
return chunked


def test_get_text():
def do_test_get_text(extra=b""):
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
c = _chunk(text, 33)
c = _chunk(text, 33, extra)
print(c)
sock = mocket.Mocket(headers + c)
pool.socket.return_value = sock
Expand All @@ -54,11 +58,19 @@ def test_get_text():
assert r.text == str(text, "utf-8")


def test_close_flush():
def test_get_text():
do_test_get_text()


def test_get_text_extra():
do_test_get_text(b";blahblah; blah")


def do_test_close_flush(extra=b""):
"""Test that a chunked response can be closed even when the request contents were not accessed."""
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
c = _chunk(text, 33)
c = _chunk(text, 33, extra)
print(c)
sock = mocket.Mocket(headers + c)
pool.socket.return_value = sock
Expand All @@ -81,3 +93,11 @@ def test_close_flush():
)

r.close()


def test_close_flush():
do_test_close_flush()


def test_close_flush_extra():
do_test_close_flush(b";blahblah; blah")