diff options
| author | Dennis Fink | 2026-11-12 23:10:55 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-12 23:10:55 +0100 |
| commit | b439de2005b70ec3867d589d1f4c63b83441e620 (patch) | |
| tree | 8946413d4ab5ab92c0f81012ea9dce79ebb046ae | |
| parent | 8f4f9041c60991457ccb1b3f145676d90e28aa8c (diff) | |
| download | webmentions-ssg-b439de2005b70ec3867d589d1f4c63b83441e620.tar.gz webmentions-ssg-b439de2005b70ec3867d589d1f4c63b83441e620.zip | |
refactor(sender): simplify HTML endpoint discovery
Use a case-insensitive CSS selector to find Webmention links directly,
removing manual relation parsing while preserving endpoint validation
and document order.
| -rw-r--r-- | tests/tasks/test_discovery.py | 42 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/discovery.py | 25 |
2 files changed, 15 insertions, 52 deletions
diff --git a/tests/tasks/test_discovery.py b/tests/tasks/test_discovery.py index 2fc30b1..2d6c37e 100644 --- a/tests/tasks/test_discovery.py +++ b/tests/tasks/test_discovery.py @@ -714,47 +714,27 @@ def test_endpoint_from_headers_ignores_malformed_link( assert sender_module.endpoint_from_headers(response) == ENDPOINT_URL -def test_endpoint_from_html_accepts_string_rel( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - element = Mock() - element.get.side_effect = lambda name: { - "rel": "webmention alternate", - "href": "/webmention", - }[name] +def test_endpoint_from_html_accepts_string_rel(sender_module: ModuleType) -> None: + response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL)) + body = b'<link rel="webmention alternate" href="/webmention">' - document = Mock() - document.find_all.return_value = [element] + assert sender_module.endpoint_from_html(response, body) == ENDPOINT_URL - beautiful_soup = Mock(return_value=document) - monkeypatch.setattr(discovery, "BeautifulSoup", beautiful_soup) +def test_endpoint_from_html_accepts_uppercase_rel(sender_module: ModuleType) -> None: response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL)) + body = b'<LINK REL="ALTERNATE WEBMENTION" HREF="/webmention">' - assert sender_module.endpoint_from_html(response, b"") == ENDPOINT_URL + assert sender_module.endpoint_from_html(response, body) == ENDPOINT_URL -def test_endpoint_from_html_ignores_elements_without_rel_and_non_string_href( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch +def test_endpoint_from_html_ignores_elements_without_matching_rel_or_href( + sender_module: ModuleType, ) -> None: - missing_rel = Mock() - missing_rel.get.return_value = None - - invalid_href = Mock() - invalid_href.get.side_effect = lambda name: { - "rel": ["webmention"], - "href": ["invalid"], - }[name] - - document = Mock() - document.find_all.return_value = [missing_rel, invalid_href] - - beautiful_soup = Mock(return_value=document) - monkeypatch.setattr(discovery, "BeautifulSoup", beautiful_soup) - response = httpx.Response(200, request=httpx.Request("GET", TARGET_URL)) + body = b'<link href="/ignored"><a rel="webmention">No endpoint</a>' - assert sender_module.endpoint_from_html(response, b"") is None + assert sender_module.endpoint_from_html(response, body) is None def test_read_target_body_rejects_declared_oversized_body( diff --git a/webmentions_ssg/tasks/discovery.py b/webmentions_ssg/tasks/discovery.py index ddf5817..29c3e81 100644 --- a/webmentions_ssg/tasks/discovery.py +++ b/webmentions_ssg/tasks/discovery.py @@ -113,28 +113,11 @@ def endpoint_from_html(response: httpx.Response, body: bytes) -> str | None: """ document = BeautifulSoup(body, "html5lib") - for element in document.find_all(["link", "a"], href=True): - relations = element.get("rel") - - if isinstance(relations, str): - relations = relations.split() - - if not relations: - continue - - if not any( - isinstance(relation, str) and relation.lower() == "webmention" - for relation in relations - ): - continue - - href = element.get("href") - - if not isinstance(href, str): - continue - + for element in document.select( + 'link[rel~="webmention" i][href], a[rel~="webmention" i][href]' + ): try: - return resolve_endpoint(response, href) + return resolve_endpoint(response, str(element["href"])) except PermanentSenderError: continue |
