Skip to content

Commit

Permalink
Add test for closed connection
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed May 10, 2023
1 parent ab0e63a commit 00f98c5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
33 changes: 33 additions & 0 deletions tests/_async/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,39 @@ async def test_http2_connection():
)


async def test_http2_connection_closed():
origin = Origin(b"https", b"example.com", 443)
stream = AsyncMockStream(
[
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).serialize(),
]
)
with AsyncHTTP2Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
await conn.request("GET", "https://example.com/")

with pytest.raises(RemoteProtocolError):
await conn.request("GET", "https://example.com/")

assert not conn.is_available()


@pytest.mark.anyio
async def test_http2_connection_post_request():
origin = Origin(b"https", b"example.com", 443)
Expand Down
42 changes: 31 additions & 11 deletions tests/_sync/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from httpcore.backends.mock import MockStream



def test_http2_connection():
origin = Origin(b"https", b"example.com", 443)
stream = MockStream(
Expand All @@ -32,9 +31,7 @@ def test_http2_connection():
).serialize(),
]
)
with HTTP2Connection(
origin=origin, stream=stream, keepalive_expiry=5.0
) as conn:
with 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!"
Expand All @@ -52,6 +49,36 @@ def test_http2_connection():
)


def test_http2_connection_closed():
origin = Origin(b"https", b"example.com", 443)
stream = 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).serialize(),
]
)
with HTTP2Connection(origin=origin, stream=stream, keepalive_expiry=5.0) as conn:
conn.request("GET", "https://example.com/")

with pytest.raises(RemoteProtocolError):
conn.request("GET", "https://example.com/")

assert not conn.is_available()


def test_http2_connection_post_request():
origin = Origin(b"https", b"example.com", 443)
Expand Down Expand Up @@ -84,7 +111,6 @@ def test_http2_connection_post_request():
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,
Expand All @@ -97,7 +123,6 @@ def test_http2_connection_with_remote_protocol_error():
conn.request("GET", "https://example.com/")



def test_http2_connection_with_rst_stream():
"""
If a stream reset occurs, then no response will be returned,
Expand Down Expand Up @@ -143,7 +168,6 @@ def test_http2_connection_with_rst_stream():
assert response.status == 200



def test_http2_connection_with_goaway():
"""
If a stream reset occurs, then no response will be returned,
Expand Down Expand Up @@ -189,7 +213,6 @@ def test_http2_connection_with_goaway():
conn.request("GET", "https://example.com/")



def test_http2_connection_with_flow_control():
origin = Origin(b"https", b"example.com", 443)
stream = MockStream(
Expand Down Expand Up @@ -249,7 +272,6 @@ def test_http2_connection_with_flow_control():
assert response.content == b"100,000 bytes received"



def test_http2_connection_attempt_close():
"""
A connection can only be closed when it is idle.
Expand Down Expand Up @@ -284,7 +306,6 @@ def test_http2_connection_attempt_close():
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.
Expand All @@ -296,7 +317,6 @@ def test_http2_request_to_incorrect_origin():
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
Expand Down

0 comments on commit 00f98c5

Please sign in to comment.