Skip to content

Commit

Permalink
Add missing test for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 11, 2024
1 parent 85304cd commit 1077ef7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
39 changes: 38 additions & 1 deletion tests/_async/test_http11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List

import pytest

Expand Down Expand Up @@ -158,6 +158,43 @@ async def test_http11_connection_with_local_protocol_error():
)


@pytest.mark.anyio
async def test_http11_has_expired_checks_readable_status():
class AsyncMockStreamReadable(httpcore.AsyncMockStream):
def __init__(self, buffer: List[bytes]) -> None:
super().__init__(buffer)
self.is_readable = False
self.checks = 0

def get_extra_info(self, info: str) -> Any:
if info == "is_readable":
self.checks += 1
return self.is_readable
return super().get_extra_info(info) # pragma: nocover

origin = httpcore.Origin(b"https", b"example.com", 443)
stream = AsyncMockStreamReadable(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
async with httpcore.AsyncHTTP11Connection(
origin=origin, stream=stream, socket_poll_interval_between=(0, 0)
) as conn:
response = await conn.request("GET", "https://example.com/")
assert response.status == 200

assert stream.checks == 0
assert not conn.has_expired()
stream.is_readable = True
assert conn.has_expired()
assert stream.checks == 2


@pytest.mark.anyio
@pytest.mark.parametrize("should_check", [True, False])
async def test_http11_has_expired_checks_readable_status_by_interval(
Expand Down
39 changes: 38 additions & 1 deletion tests/_sync/test_http11.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, List

import pytest

Expand Down Expand Up @@ -159,6 +159,43 @@ def test_http11_connection_with_local_protocol_error():



def test_http11_has_expired_checks_readable_status():
class MockStreamReadable(httpcore.MockStream):
def __init__(self, buffer: List[bytes]) -> None:
super().__init__(buffer)
self.is_readable = False
self.checks = 0

def get_extra_info(self, info: str) -> Any:
if info == "is_readable":
self.checks += 1
return self.is_readable
return super().get_extra_info(info) # pragma: nocover

origin = httpcore.Origin(b"https", b"example.com", 443)
stream = MockStreamReadable(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
)
with httpcore.HTTP11Connection(
origin=origin, stream=stream, socket_poll_interval_between=(0, 0)
) as conn:
response = conn.request("GET", "https://example.com/")
assert response.status == 200

assert stream.checks == 0
assert not conn.has_expired()
stream.is_readable = True
assert conn.has_expired()
assert stream.checks == 2



@pytest.mark.parametrize("should_check", [True, False])
def test_http11_has_expired_checks_readable_status_by_interval(
monkeypatch, should_check
Expand Down

0 comments on commit 1077ef7

Please sign in to comment.