Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/feditest/protocols/webfinger/abstract.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Functionality that may be shared by several WebFinger Node implementations.
"""

from typing import cast

from feditest.protocols.web.diag import HttpRequest, HttpRequestResponsePair, WebDiagClient
Expand All @@ -18,10 +19,12 @@ def diag_perform_webfinger_query(
rels: list[str] | None = None,
server: WebFingerServer | None = None
) -> WebFingerQueryDiagResponse:

query_url = construct_webfinger_uri_for(resource_uri, rels, server.hostname() if server else None )
parsed_uri = ParsedUri.parse(query_url)
if not parsed_uri:
raise ValueError('Not a valid URI:', query_url) # can't avoid this

first_request = HttpRequest(parsed_uri)
current_request = first_request
pair : HttpRequestResponsePair | None = None
Expand Down
37 changes: 25 additions & 12 deletions src/feditest/protocols/webfinger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from urllib.parse import quote, urlparse
from typing import Any, Type
from typing import Any, Type, cast

from multidict import MultiDict
from hamcrest.core.base_matcher import BaseMatcher
Expand Down Expand Up @@ -155,7 +155,7 @@ def describe_to(self, description: Description) -> None:
description.append_text(f'MultiDict has key: { self._key }')


class NoneExceptMatcher(BaseMatcher):
class NoExceptionOtherThanMatcher(BaseMatcher):
"""
Custom matcher: decode whether an Exception (which may be an ExceptionGroup) contains
any Exception other than the provided allowed exceptions.
Expand All @@ -164,27 +164,36 @@ def __init__(self, allowed_excs: list[Type[Exception]]):
self._allowed_excs = allowed_excs


def _matches(self, candidate: Exception | None ) -> bool:
def _matches(self, candidate: Exception | list[Exception] | None) -> bool:
if candidate is None:
return True
if type(candidate) is list:
for cand in candidate:
if not self._matches_single(cand):
return False
return True

return self._matches_single(cast(Exception, candidate))


def _matches_single(self, candidate: Exception) -> bool:
if isinstance(candidate, ExceptionGroup):
for cand in candidate.exceptions:
found = False
for allowed in self._allowed_excs:
if isinstance(cand, allowed):
found = True
if not found:
if not self._matches_single(cand):
return False
return True

for allowed in self._allowed_excs:
if isinstance(candidate, allowed):
return True
return False


def describe_to(self, description: Description) -> None:
description.append_text(f'No exception other than: { ",".join( [ x.__name__ for x in self._allowed_excs ] ) }')
if self._allowed_excs:
description.append_text(f'No exception other than: { ",".join( [ x.__name__ for x in self._allowed_excs ] ) }')
else:
description.append_text('No exception')


def recursive_equal_to(arg: object) -> RecursiveEqualToMatcher :
Expand All @@ -199,8 +208,12 @@ def multi_dict_has_key(arg: str) -> MultiDictHasKeyMatcher :
return MultiDictHasKeyMatcher(arg)


def none_except(*allowed_excs : Type[Exception]) -> NoneExceptMatcher :
return NoneExceptMatcher(list(allowed_excs))
def no_exception_other_than(allowed_excs : list[type[Exception]]) -> NoExceptionOtherThanMatcher :
return NoExceptionOtherThanMatcher(allowed_excs)


def no_exception() -> NoExceptionOtherThanMatcher :
return NoExceptionOtherThanMatcher([])


def wf_error(response: WebFingerQueryDiagResponse) -> str:
Expand Down