Skip to content

Commit 92bf8e1

Browse files
committed
Fix crash when closing chunked response
1 parent 7d0e3ba commit 92bf8e1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/chunk_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,31 @@ def test_get_text():
5252
[mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"),]
5353
)
5454
assert r.text == str(text, "utf-8")
55+
56+
def test_close_flush():
57+
"""Test that a chunked response can be closed even when the request contents were not accessed."""
58+
pool = mocket.MocketPool()
59+
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
60+
c = _chunk(text, 33)
61+
print(c)
62+
sock = mocket.Mocket(headers + c)
63+
pool.socket.return_value = sock
64+
65+
s = adafruit_requests.Session(pool)
66+
r = s.get("http://" + host + path)
67+
68+
sock.connect.assert_called_once_with((ip, 80))
69+
70+
sock.send.assert_has_calls(
71+
[
72+
mock.call(b"GET"),
73+
mock.call(b" /"),
74+
mock.call(b"testwifi/index.html"),
75+
mock.call(b" HTTP/1.1\r\n"),
76+
]
77+
)
78+
sock.send.assert_has_calls(
79+
[mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"),]
80+
)
81+
82+
r.close()

tests/protocol_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,28 @@ def test_get_http_text():
7373
[mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"),]
7474
)
7575
assert r.text == str(text, "utf-8")
76+
77+
def test_get_close():
78+
"""Test that a response can be closed without the contents being accessed."""
79+
pool = mocket.MocketPool()
80+
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
81+
sock = mocket.Mocket(response)
82+
pool.socket.return_value = sock
83+
84+
s = adafruit_requests.Session(pool)
85+
r = s.get("http://" + host + path)
86+
87+
sock.connect.assert_called_once_with((ip, 80))
88+
89+
sock.send.assert_has_calls(
90+
[
91+
mock.call(b"GET"),
92+
mock.call(b" /"),
93+
mock.call(b"testwifi/index.html"),
94+
mock.call(b" HTTP/1.1\r\n"),
95+
]
96+
)
97+
sock.send.assert_has_calls(
98+
[mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"),]
99+
)
100+
r.close()

0 commit comments

Comments
 (0)