Skip to content

Commit 0106c75

Browse files
authored
Merge pull request #45 from tannewt/fix_close
Fix crash when closing chunked response
2 parents 7d0e3ba + f9b1e68 commit 0106c75

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

tests/chunk_test.py

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

tests/protocol_test.py

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

0 commit comments

Comments
 (0)