aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDennis Fink2026-11-13 20:03:49 +0100
committerDennis Fink2026-11-13 20:03:49 +0100
commit87117345ed6e99e5a070d919ac17139cba7b8fe4 (patch)
treed7df5d45008dd2959194e2614b87333598b85198 /tests
parent2fbe1d383650cb31f3ef8d58c04e4d4095935833 (diff)
downloadwebmentions-ssg-87117345ed6e99e5a070d919ac17139cba7b8fe4.tar.gz
webmentions-ssg-87117345ed6e99e5a070d919ac17139cba7b8fe4.zip
fix(core): make network task failures explicit
Introduce a dedicated response-size error so callers only translate expected limit violations instead of masking unrelated `ValueError` exceptions. Ensure failed sockets are always closed, remove dead discovery error handling, and log unexpected sender failures with tracebacks before recording them.
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_discovery.py26
-rw-r--r--tests/tasks/test_receiver.py19
-rw-r--r--tests/tasks/test_sender.py23
-rw-r--r--tests/test_net.py59
4 files changed, 123 insertions, 4 deletions
diff --git a/tests/tasks/test_discovery.py b/tests/tasks/test_discovery.py
index 2d6c37e..7c28108 100644
--- a/tests/tasks/test_discovery.py
+++ b/tests/tasks/test_discovery.py
@@ -569,7 +569,7 @@ def test_discovery_falls_back_to_get_after_head_request_error(
assert methods == ["HEAD", "GET"]
-def test_discovery_falls_back_to_get_after_head_dns_failure(
+def test_discovery_does_not_suppress_unexpected_head_error(
app: Flask, sender_module: ModuleType
) -> None:
methods: list[str] = []
@@ -588,11 +588,13 @@ def test_discovery_falls_back_to_get_after_head_dns_failure(
transport=httpx.MockTransport(handler),
event_hooks={"request": [request_hook]},
) as client,
+ pytest.raises(
+ sender_module.TemporarySenderError, match="HEAD DNS lookup failed"
+ ),
):
- endpoint = sender_module.discover_webmention_endpoint(client, TARGET_URL)
+ sender_module.discover_webmention_endpoint(client, TARGET_URL)
- assert endpoint == ENDPOINT_URL
- assert methods == ["GET"]
+ assert methods == []
def test_discovery_get_error_after_head_error_is_not_suppressed(
@@ -753,6 +755,22 @@ def test_read_target_body_rejects_declared_oversized_body(
sender_module.read_target_body(response)
+def test_read_target_body_does_not_mask_unexpected_value_error(
+ app: Flask, sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(
+ sender_module,
+ "read_limited_body",
+ Mock(side_effect=ValueError("unrelated processing error")),
+ )
+
+ with (
+ app.app_context(),
+ pytest.raises(ValueError, match="unrelated processing error"),
+ ):
+ sender_module.read_target_body(httpx.Response(200))
+
+
def test_read_target_body_ignores_invalid_content_length(
app: Flask, sender_module: ModuleType
) -> None:
diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py
index 133ab56..51d1bb6 100644
--- a/tests/tasks/test_receiver.py
+++ b/tests/tasks/test_receiver.py
@@ -198,6 +198,25 @@ def test_fetch_source_rejects_streamed_oversized_body(
receiver.fetch_source(SOURCE_URL)
+@patch("webmentions_ssg.tasks.receiver.httpx.Client")
+def test_fetch_source_does_not_mask_unexpected_value_error(
+ httpx_client: Mock,
+ app: Flask,
+ receiver: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ set_stream_response(
+ httpx_client,
+ httpx.Response(200, request=httpx.Request("GET", SOURCE_URL)),
+ )
+ monkeypatch.setattr(
+ receiver, "read_limited_body", Mock(side_effect=ValueError("unexpected bug"))
+ )
+
+ with app.app_context(), pytest.raises(ValueError, match="unexpected bug"):
+ receiver.fetch_source(SOURCE_URL)
+
+
@patch("webmentions_ssg.tasks.receiver.fetch_source")
@pytest.mark.parametrize(
("content_type", "body", "expected"),
diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py
index 94523e5..818092b 100644
--- a/tests/tasks/test_sender.py
+++ b/tests/tasks/test_sender.py
@@ -431,6 +431,29 @@ def test_sender_malformed_request_is_terminal(
assert post.call_count == (1 if stage == "post" else 0)
+def test_sender_logs_unexpected_value_error_traceback(
+ app: Flask,
+ sender_module: ModuleType,
+ monkeypatch: pytest.MonkeyPatch,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ identifier = create_sent_webmention(app)
+ _, _, discover, _ = mock_sender_requests(sender_module, monkeypatch)
+ discover.side_effect = ValueError("unexpected parsing bug")
+
+ with caplog.at_level(logging.ERROR):
+ sender_module.send_webmention.call_local(identifier)
+
+ assert f"Unexpected error sending SentWebmention {identifier}" in caplog.text
+ assert "ValueError: unexpected parsing bug" in caplog.text
+ with app.app_context():
+ webmention = db.session.get(SentWebmention, identifier)
+ assert webmention is not None
+ assert webmention.status == SentWebmentionStatus.FAILED
+ assert webmention.failure_reason == "unexpected parsing bug"
+ assert webmention.processed_revision == 1
+
+
@pytest.mark.parametrize(
("target", "reason"),
[
diff --git a/tests/test_net.py b/tests/test_net.py
index 05ae5a4..8e0744c 100644
--- a/tests/test_net.py
+++ b/tests/test_net.py
@@ -19,10 +19,12 @@ from webmentions_ssg.net import (
NetworkDeadline,
PublicHTTPTransport,
PublicNetworkBackend,
+ ResponseTooLargeError,
UnsafeAddressError,
is_http_url,
is_public_address,
public_addresses,
+ read_limited_body,
)
@@ -60,6 +62,63 @@ def test_rebinding_cannot_change_socket_peer(monkeypatch: pytest.MonkeyPatch) ->
create_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
+def test_backend_closes_failed_socket_and_tries_next_address(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ "webmentions_ssg.net.public_addresses",
+ Mock(return_value=("93.184.216.34", "8.8.8.8")),
+ )
+ first, second = Mock(), Mock()
+ first.connect.side_effect = TimeoutError("connection timed out")
+ monkeypatch.setattr(socket, "socket", Mock(side_effect=[first, second]))
+
+ stream = PublicNetworkBackend(NetworkDeadline(5)).connect_tcp(
+ "example.com", 443
+ )
+
+ assert isinstance(stream, DeadlineStream)
+ first.close.assert_called_once_with()
+ second.close.assert_not_called()
+ first.connect.assert_called_once_with(("93.184.216.34", 443))
+ second.connect.assert_called_once_with(("8.8.8.8", 443))
+ stream.close()
+ second.close.assert_called_once_with()
+
+
+@pytest.mark.parametrize(
+ ("failure", "expected"),
+ [
+ pytest.param(TimeoutError("timed out"), httpcore.ConnectTimeout, id="timeout"),
+ pytest.param(OSError("refused"), httpcore.ConnectError, id="os-error"),
+ pytest.param(ValueError("unexpected"), ValueError, id="unexpected"),
+ ],
+)
+def test_backend_closes_socket_on_failure(
+ monkeypatch: pytest.MonkeyPatch, failure: Exception, expected: type[Exception]
+) -> None:
+ sock = Mock()
+ sock.connect.side_effect = failure
+ monkeypatch.setattr(socket, "socket", Mock(return_value=sock))
+
+ with pytest.raises(expected, match=str(failure)):
+ PublicNetworkBackend(NetworkDeadline(5)).connect_tcp("8.8.8.8", 443)
+
+ sock.close.assert_called_once_with()
+
+
+@pytest.mark.parametrize("mode", ["declared", "streamed"])
+def test_read_limited_body_raises_specific_size_error(mode: str) -> None:
+ response = (
+ httpx.Response(200, headers={"Content-Length": "11"}, content=b"x" * 11)
+ if mode == "declared"
+ else httpx.Response(200, stream=httpx.ByteStream(b"x" * 11))
+ )
+
+ with pytest.raises(ResponseTooLargeError, match="Source document is too large"):
+ read_limited_body(response, 10, label="Source")
+
+
class FakeStream(httpcore.NetworkStream):
"""In-memory TCP/TLS stream so HTTP framing and SNI can be tested."""