1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
import typing
import anyio
import hpack
import hyperframe
import pytest
import httpcore
class SlowWriteStream(httpcore.AsyncNetworkStream):
"""
A stream that we can use to test cancellations during
the request writing.
"""
async def write(
self, buffer: bytes, timeout: typing.Optional[float] = None
) -> None:
await anyio.sleep(999)
async def aclose(self) -> None:
await anyio.sleep(0)
class HandshakeThenSlowWriteStream(httpcore.AsyncNetworkStream):
"""
A stream that we can use to test cancellations during
the HTTP/2 request writing, after allowing the initial
handshake to complete.
"""
def __init__(self) -> None:
self._handshake_complete = False
async def write(
self, buffer: bytes, timeout: typing.Optional[float] = None
) -> None:
if not self._handshake_complete:
self._handshake_complete = True
else:
await anyio.sleep(999)
async def aclose(self) -> None:
await anyio.sleep(0)
class SlowReadStream(httpcore.AsyncNetworkStream):
"""
A stream that we can use to test cancellations during
the response reading.
"""
def __init__(self, buffer: typing.List[bytes]):
self._buffer = buffer
async def write(self, buffer, timeout=None):
pass
async def read(
self, max_bytes: int, timeout: typing.Optional[float] = None
) -> bytes:
if not self._buffer:
await anyio.sleep(999)
return self._buffer.pop(0)
async def aclose(self):
await anyio.sleep(0)
class SlowWriteBackend(httpcore.AsyncNetworkBackend):
async def connect_tcp(
self,
host: str,
port: int,
timeout: typing.Optional[float] = None,
local_address: typing.Optional[str] = None,
socket_options: typing.Optional[typing.Iterable[httpcore.SOCKET_OPTION]] = None,
) -> httpcore.AsyncNetworkStream:
return SlowWriteStream()
class SlowReadBackend(httpcore.AsyncNetworkBackend):
def __init__(self, buffer: typing.List[bytes]):
self._buffer = buffer
async def connect_tcp(
self,
host: str,
port: int,
timeout: typing.Optional[float] = None,
local_address: typing.Optional[str] = None,
socket_options: typing.Optional[typing.Iterable[httpcore.SOCKET_OPTION]] = None,
) -> httpcore.AsyncNetworkStream:
return SlowReadStream(self._buffer)
@pytest.mark.anyio
async def test_connection_pool_timeout_during_request():
"""
An async timeout when writing an HTTP/1.1 response on the connection pool
should leave the pool in a consistent state.
In this case, that means the connection will become closed, and no
longer remain in the pool.
"""
network_backend = SlowWriteBackend()
async with httpcore.AsyncConnectionPool(network_backend=network_backend) as pool:
with anyio.move_on_after(0.01):
await pool.request("GET", "http://example.com")
assert not pool.connections
@pytest.mark.anyio
async def test_connection_pool_timeout_during_response():
"""
An async timeout when reading an HTTP/1.1 response on the connection pool
should leave the pool in a consistent state.
In this case, that means the connection will become closed, and no
longer remain in the pool.
"""
network_backend = SlowReadBackend(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 1000\r\n",
b"\r\n",
b"Hello, world!...",
]
)
async with httpcore.AsyncConnectionPool(network_backend=network_backend) as pool:
with anyio.move_on_after(0.01):
await pool.request("GET", "http://example.com")
assert not pool.connections
@pytest.mark.anyio
async def test_h11_timeout_during_request():
"""
An async timeout on an HTTP/1.1 during the request writing
should leave the connection in a neatly closed state.
"""
origin = httpcore.Origin(b"http", b"example.com", 80)
stream = SlowWriteStream()
async with httpcore.AsyncHTTP11Connection(origin, stream) as conn:
with anyio.move_on_after(0.01):
await conn.request("GET", "http://example.com")
assert conn.is_closed()
@pytest.mark.anyio
async def test_h11_timeout_during_response():
"""
An async timeout on an HTTP/1.1 during the response reading
should leave the connection in a neatly closed state.
"""
origin = httpcore.Origin(b"http", b"example.com", 80)
stream = SlowReadStream(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 1000\r\n",
b"\r\n",
b"Hello, world!...",
]
)
async with httpcore.AsyncHTTP11Connection(origin, stream) as conn:
with anyio.move_on_after(0.01):
await conn.request("GET", "http://example.com")
assert conn.is_closed()
@pytest.mark.xfail
@pytest.mark.anyio
async def test_h2_timeout_during_handshake():
"""
An async timeout on an HTTP/2 during the initial handshake
should leave the connection in a neatly closed state.
"""
origin = httpcore.Origin(b"http", b"example.com", 80)
stream = SlowWriteStream()
async with httpcore.AsyncHTTP2Connection(origin, stream) as conn:
with anyio.move_on_after(0.01):
await conn.request("GET", "http://example.com")
assert conn.is_closed()
@pytest.mark.xfail
@pytest.mark.anyio
async def test_h2_timeout_during_request():
"""
An async timeout on an HTTP/2 during a request
should leave the connection in a neatly idle state.
The connection is not closed because it is multiplexed,
and a timeout on one request does not require the entire
connection be closed.
"""
origin = httpcore.Origin(b"http", b"example.com", 80)
stream = HandshakeThenSlowWriteStream()
async with httpcore.AsyncHTTP2Connection(origin, stream) as conn:
with anyio.move_on_after(0.01):
await conn.request("GET", "http://example.com")
assert not conn.is_closed()
assert conn.is_idle()
@pytest.mark.xfail
@pytest.mark.anyio
async def test_h2_timeout_during_response():
"""
An async timeout on an HTTP/2 during the response reading
should leave the connection in a neatly idle state.
The connection is not closed because it is multiplexed,
and a timeout on one request does not require the entire
connection be closed.
"""
origin = httpcore.Origin(b"http", b"example.com", 80)
stream = SlowReadStream(
[
hyperframe.frame.SettingsFrame().serialize(),
hyperframe.frame.HeadersFrame(
stream_id=1,
data=hpack.Encoder().encode(
[
(b":status", b"200"),
(b"content-type", b"plain/text"),
]
),
flags=["END_HEADERS"],
).serialize(),
hyperframe.frame.DataFrame(
stream_id=1, data=b"Hello, world!...", flags=[]
).serialize(),
]
)
async with httpcore.AsyncHTTP2Connection(origin, stream) as conn:
with anyio.move_on_after(0.01):
await conn.request("GET", "http://example.com")
assert not conn.is_closed()
assert conn.is_idle()
|