From 714608c17d3aad68f5dc1300b30cda6a990b1bdc Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 27 Jan 2021 19:19:10 -0800 Subject: [PATCH 1/2] Fix chunked request detection The response to http://site.api.espn.com/apis/site/v2/sports/basketball/mens-college-basketball/scoreboard has an extra space between Transfer-Encoding: and chunked so the comparison fails. Fixes #64 --- adafruit_requests.py | 2 +- tests/chunk_test.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/adafruit_requests.py b/adafruit_requests.py index 5aea7e8..8abd58b 100644 --- a/adafruit_requests.py +++ b/adafruit_requests.py @@ -293,7 +293,7 @@ def _parse_headers(self): len(title) == len("transfer-encoding") and title.lower() == "transfer-encoding" ): - self._chunked = content.lower() == "chunked" + self._chunked = content.strip().lower() == "chunked" self._headers[title] = content @property diff --git a/tests/chunk_test.py b/tests/chunk_test.py index e55a922..d4feebf 100644 --- a/tests/chunk_test.py +++ b/tests/chunk_test.py @@ -11,6 +11,7 @@ path = "/testwifi/index.html" text = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)" headers = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" +headers_extra_space = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" def _chunk(response, split, extra=b""): @@ -111,3 +112,33 @@ def test_close_flush(): def test_close_flush_extra(): do_test_close_flush(b";blahblah; blah") + + +def do_test_get_text_extra_space(extra=b""): + pool = mocket.MocketPool() + pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),) + c = _chunk(text, 33, extra) + print(c) + sock = mocket.Mocket(headers_extra_space + c) + pool.socket.return_value = sock + + s = adafruit_requests.Session(pool) + r = s.get("http://" + host + path) + + sock.connect.assert_called_once_with((ip, 80)) + + sock.send.assert_has_calls( + [ + mock.call(b"GET"), + mock.call(b" /"), + mock.call(b"testwifi/index.html"), + mock.call(b" HTTP/1.1\r\n"), + ] + ) + sock.send.assert_has_calls( + [ + mock.call(b"Host: "), + mock.call(b"wifitest.adafruit.com"), + ] + ) + assert r.text == str(text, "utf-8") \ No newline at end of file From c0c41c2ae6bdc00c934169f820643db27616c846 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jan 2021 10:16:17 -0800 Subject: [PATCH 2/2] Black --- tests/chunk_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/chunk_test.py b/tests/chunk_test.py index d4feebf..166f67c 100644 --- a/tests/chunk_test.py +++ b/tests/chunk_test.py @@ -141,4 +141,4 @@ def do_test_get_text_extra_space(extra=b""): mock.call(b"wifitest.adafruit.com"), ] ) - assert r.text == str(text, "utf-8") \ No newline at end of file + assert r.text == str(text, "utf-8")