Skip to content

Commit f658b39

Browse files
authored
Merge pull request #65 from tannewt/better_chunked
Fix chunked request detection
2 parents 91c4c83 + c0c41c2 commit f658b39

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

adafruit_requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def _parse_headers(self):
293293
len(title) == len("transfer-encoding")
294294
and title.lower() == "transfer-encoding"
295295
):
296-
self._chunked = content.lower() == "chunked"
296+
self._chunked = content.strip().lower() == "chunked"
297297
self._headers[title] = content
298298

299299
@property

tests/chunk_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
path = "/testwifi/index.html"
1212
text = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
1313
headers = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
14+
headers_extra_space = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
1415

1516

1617
def _chunk(response, split, extra=b""):
@@ -111,3 +112,33 @@ def test_close_flush():
111112

112113
def test_close_flush_extra():
113114
do_test_close_flush(b";blahblah; blah")
115+
116+
117+
def do_test_get_text_extra_space(extra=b""):
118+
pool = mocket.MocketPool()
119+
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
120+
c = _chunk(text, 33, extra)
121+
print(c)
122+
sock = mocket.Mocket(headers_extra_space + c)
123+
pool.socket.return_value = sock
124+
125+
s = adafruit_requests.Session(pool)
126+
r = s.get("http://" + host + path)
127+
128+
sock.connect.assert_called_once_with((ip, 80))
129+
130+
sock.send.assert_has_calls(
131+
[
132+
mock.call(b"GET"),
133+
mock.call(b" /"),
134+
mock.call(b"testwifi/index.html"),
135+
mock.call(b" HTTP/1.1\r\n"),
136+
]
137+
)
138+
sock.send.assert_has_calls(
139+
[
140+
mock.call(b"Host: "),
141+
mock.call(b"wifitest.adafruit.com"),
142+
]
143+
)
144+
assert r.text == str(text, "utf-8")

0 commit comments

Comments
 (0)