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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
import hpack
import hyperframe.frame
import pytest
import httpcore
def test_http2_connection():
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
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=["END_STREAM"]
).serialize(),
]
)
with httpcore.HTTP2Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
response = conn.request("GET", "https://example.com/")
assert response.status == 200
assert response.content == b"Hello, world!"
assert conn.is_idle()
assert conn.is_available()
assert not conn.is_closed()
assert not conn.has_expired()
assert (
conn.info() == "'https://example.com:443', HTTP/2, IDLE, Request Count: 1"
)
assert (
repr(conn)
== "<HTTP2Connection ['https://example.com:443', IDLE, Request Count: 1]>"
)
def test_http2_connection_closed():
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
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=["END_STREAM"]
).serialize(),
# Connection is closed after the first response
hyperframe.frame.GoAwayFrame(
stream_id=0, error_code=0, last_stream_id=1
).serialize(),
]
)
with httpcore.HTTP2Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
conn.request("GET", "https://example.com/")
with pytest.raises(httpcore.ConnectionNotAvailable):
conn.request("GET", "https://example.com/")
assert not conn.is_available()
def test_http2_connection_post_request():
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
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=["END_STREAM"]
).serialize(),
]
)
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
response = conn.request(
"POST",
"https://example.com/",
headers={b"content-length": b"17"},
content=b'{"data": "upload"}',
)
assert response.status == 200
assert response.content == b"Hello, world!"
def test_http2_connection_with_remote_protocol_error():
"""
If a remote protocol error occurs, then no response will be returned,
and the connection will not be reusable.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream([b"Wait, this isn't valid HTTP!", b""])
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(httpcore.RemoteProtocolError):
conn.request("GET", "https://example.com/")
def test_http2_connection_with_rst_stream():
"""
If a stream reset occurs, then no response will be returned,
but the connection will remain reusable for other requests.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
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(),
# Stream is closed midway through the first response...
hyperframe.frame.RstStreamFrame(stream_id=1, error_code=8).serialize(),
# ...Which doesn't prevent the second response.
hyperframe.frame.HeadersFrame(
stream_id=3,
data=hpack.Encoder().encode(
[
(b":status", b"200"),
(b"content-type", b"plain/text"),
]
),
flags=["END_HEADERS"],
).serialize(),
hyperframe.frame.DataFrame(
stream_id=3, data=b"Hello, world!", flags=["END_STREAM"]
).serialize(),
b"",
]
)
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(httpcore.RemoteProtocolError):
conn.request("GET", "https://example.com/")
response = conn.request("GET", "https://example.com/")
assert response.status == 200
def test_http2_connection_with_goaway():
"""
If a GoAway frame occurs, then no response will be returned,
and the connection will not be reusable for other requests.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
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(),
# Connection is closed midway through the first response...
hyperframe.frame.GoAwayFrame(stream_id=0, error_code=0).serialize(),
# ...We'll never get to this second response.
hyperframe.frame.HeadersFrame(
stream_id=3,
data=hpack.Encoder().encode(
[
(b":status", b"200"),
(b"content-type", b"plain/text"),
]
),
flags=["END_HEADERS"],
).serialize(),
hyperframe.frame.DataFrame(
stream_id=3, data=b"Hello, world!", flags=["END_STREAM"]
).serialize(),
b"",
]
)
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
# The initial request has been closed midway, with an unrecoverable error.
with pytest.raises(httpcore.RemoteProtocolError):
conn.request("GET", "https://example.com/")
# The second request can receive a graceful `ConnectionNotAvailable`,
# and may be retried on a new connection.
with pytest.raises(httpcore.ConnectionNotAvailable):
conn.request("GET", "https://example.com/")
def test_http2_connection_with_flow_control():
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
hyperframe.frame.SettingsFrame().serialize(),
# Available flow: 65,535
hyperframe.frame.WindowUpdateFrame(
stream_id=0, window_increment=10_000
).serialize(),
hyperframe.frame.WindowUpdateFrame(
stream_id=1, window_increment=10_000
).serialize(),
# Available flow: 75,535
hyperframe.frame.WindowUpdateFrame(
stream_id=0, window_increment=10_000
).serialize(),
hyperframe.frame.WindowUpdateFrame(
stream_id=1, window_increment=10_000
).serialize(),
# Available flow: 85,535
hyperframe.frame.WindowUpdateFrame(
stream_id=0, window_increment=10_000
).serialize(),
hyperframe.frame.WindowUpdateFrame(
stream_id=1, window_increment=10_000
).serialize(),
# Available flow: 95,535
hyperframe.frame.WindowUpdateFrame(
stream_id=0, window_increment=10_000
).serialize(),
hyperframe.frame.WindowUpdateFrame(
stream_id=1, window_increment=10_000
).serialize(),
# Available flow: 105,535
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"100,000 bytes received", flags=["END_STREAM"]
).serialize(),
]
)
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
response = conn.request(
"POST",
"https://example.com/",
content=b"x" * 100_000,
)
assert response.status == 200
assert response.content == b"100,000 bytes received"
def test_http2_connection_attempt_close():
"""
A connection can only be closed when it is idle.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
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=["END_STREAM"]
).serialize(),
]
)
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
with conn.stream("GET", "https://example.com/") as response:
response.read()
assert response.status == 200
assert response.content == b"Hello, world!"
conn.close()
with pytest.raises(httpcore.ConnectionNotAvailable):
conn.request("GET", "https://example.com/")
def test_http2_request_to_incorrect_origin():
"""
A connection can only send requests to whichever origin it is connected to.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream([])
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(RuntimeError):
conn.request("GET", "https://other.com/")
def test_http2_remote_max_streams_update():
"""
If the remote server updates the maximum concurrent streams value, we should
be adjusting how many streams we will allow.
"""
origin = httpcore.Origin(b"https", b"example.com", 443)
stream = httpcore.MockStream(
[
hyperframe.frame.SettingsFrame(
settings={hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 1000}
).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!").serialize(),
hyperframe.frame.SettingsFrame(
settings={hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 50}
).serialize(),
hyperframe.frame.DataFrame(
stream_id=1, data=b"Hello, world...again!", flags=["END_STREAM"]
).serialize(),
]
)
with httpcore.HTTP2Connection(origin=origin, stream=stream) as conn:
with conn.stream("GET", "https://example.com/") as response:
i = 0
for chunk in response.iter_stream():
if i == 0:
assert chunk == b"Hello, world!"
assert conn._h2_state.remote_settings.max_concurrent_streams == 1000
assert conn._max_streams == min(
conn._h2_state.remote_settings.max_concurrent_streams,
conn._h2_state.local_settings.max_concurrent_streams,
)
elif i == 1:
assert chunk == b"Hello, world...again!"
assert conn._h2_state.remote_settings.max_concurrent_streams == 50
assert conn._max_streams == min(
conn._h2_state.remote_settings.max_concurrent_streams,
conn._h2_state.local_settings.max_concurrent_streams,
)
i += 1
|