9
9
headers = b"HTTP/1.0 200 OK\r \n Transfer-Encoding: chunked\r \n \r \n "
10
10
11
11
12
- def _chunk (response , split ):
12
+ def _chunk (response , split , extra = b'' ):
13
13
i = 0
14
14
chunked = b""
15
15
while i < len (response ):
@@ -19,7 +19,7 @@ def _chunk(response, split):
19
19
chunk_size = remaining
20
20
new_i = i + chunk_size
21
21
chunked += (
22
- hex (chunk_size )[2 :].encode ("ascii" ) + b"\r \n " + response [i :new_i ] + b"\r \n "
22
+ hex (chunk_size )[2 :].encode ("ascii" ) + extra + b"\r \n " + response [i :new_i ] + b"\r \n "
23
23
)
24
24
i = new_i
25
25
# The final chunk is zero length.
@@ -28,56 +28,58 @@ def _chunk(response, split):
28
28
29
29
30
30
def test_get_text ():
31
- pool = mocket .MocketPool ()
32
- pool .getaddrinfo .return_value = ((None , None , None , None , (ip , 80 )),)
33
- c = _chunk (text , 33 )
34
- print (c )
35
- sock = mocket .Mocket (headers + c )
36
- pool .socket .return_value = sock
31
+ for extra in (b'' , b';blahblah; blah' ):
32
+ pool = mocket .MocketPool ()
33
+ pool .getaddrinfo .return_value = ((None , None , None , None , (ip , 80 )),)
34
+ c = _chunk (text , 33 , extra )
35
+ print (c )
36
+ sock = mocket .Mocket (headers + c )
37
+ pool .socket .return_value = sock
37
38
38
- s = adafruit_requests .Session (pool )
39
- r = s .get ("http://" + host + path )
39
+ s = adafruit_requests .Session (pool )
40
+ r = s .get ("http://" + host + path )
40
41
41
- sock .connect .assert_called_once_with ((ip , 80 ))
42
+ sock .connect .assert_called_once_with ((ip , 80 ))
42
43
43
- sock .send .assert_has_calls (
44
- [
45
- mock .call (b"GET" ),
46
- mock .call (b" /" ),
47
- mock .call (b"testwifi/index.html" ),
48
- mock .call (b" HTTP/1.1\r \n " ),
49
- ]
50
- )
51
- sock .send .assert_has_calls (
52
- [mock .call (b"Host: " ), mock .call (b"wifitest.adafruit.com" ),]
53
- )
54
- assert r .text == str (text , "utf-8" )
44
+ sock .send .assert_has_calls (
45
+ [
46
+ mock .call (b"GET" ),
47
+ mock .call (b" /" ),
48
+ mock .call (b"testwifi/index.html" ),
49
+ mock .call (b" HTTP/1.1\r \n " ),
50
+ ]
51
+ )
52
+ sock .send .assert_has_calls (
53
+ [mock .call (b"Host: " ), mock .call (b"wifitest.adafruit.com" ),]
54
+ )
55
+ assert r .text == str (text , "utf-8" )
55
56
56
57
57
58
def test_close_flush ():
58
59
"""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
60
+ for extra in (b'' , b';blahblah; blah' ):
61
+ pool = mocket .MocketPool ()
62
+ pool .getaddrinfo .return_value = ((None , None , None , None , (ip , 80 )),)
63
+ c = _chunk (text , 33 , extra )
64
+ print (c )
65
+ sock = mocket .Mocket (headers + c )
66
+ pool .socket .return_value = sock
65
67
66
- s = adafruit_requests .Session (pool )
67
- r = s .get ("http://" + host + path )
68
+ s = adafruit_requests .Session (pool )
69
+ r = s .get ("http://" + host + path )
68
70
69
- sock .connect .assert_called_once_with ((ip , 80 ))
71
+ sock .connect .assert_called_once_with ((ip , 80 ))
70
72
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
- )
73
+ sock .send .assert_has_calls (
74
+ [
75
+ mock .call (b"GET" ),
76
+ mock .call (b" /" ),
77
+ mock .call (b"testwifi/index.html" ),
78
+ mock .call (b" HTTP/1.1\r \n " ),
79
+ ]
80
+ )
81
+ sock .send .assert_has_calls (
82
+ [mock .call (b"Host: " ), mock .call (b"wifitest.adafruit.com" ),]
83
+ )
82
84
83
- r .close ()
85
+ r .close ()
0 commit comments