# Path: federation/protocols/activitypub/protocol.py
# def identify_request(request: RequestType) -> bool:
#     """
#     Try to identify whether this is an ActivityPub request.
#     """
#     # noinspection PyBroadException
#     try:
#         data = json.loads(decode_if_bytes(request.body))
#         if "@context" in data:
#             return True
#     except Exception:
#         pass
#     return False
# 
# def identify_id(id: str) -> bool:
#     """
#     Try to identify whether this is an ActivityPub ID.
#     """
#     return re.match(r'^https?://', id, flags=re.IGNORECASE) is not None
# 
# Path: federation/types.py
# class RequestType:
#     """
#     Emulates structure of a Django HttpRequest for compatibility.
#     """
#     body: Union[str, bytes] = attr.ib()
# 
#     # Required when dealing with incoming AP payloads
#     headers: Dict = attr.ib(default=None)
#     method: str = attr.ib(default=None)
#     url: str = attr.ib(default=None)


import json
from federation.protocols.activitypub.protocol import identify_request, identify_id
from federation.types import RequestType



def test_identify_id():
    assert identify_id('foobar') is False
    assert identify_id('foobar@example.com') is False
    assert identify_id('foobar@example.com:8000') is False
    assert identify_id('http://foobar@example.com') is True
    assert identify_id('https://foobar@example.com') is True


class TestIdentifyRequest:
    def test_identifies_activitypub_request(self):
        assert identify_request(RequestType(body=json.dumps('{"@context": "foo"}')))
        assert identify_request(RequestType(body=json.dumps('{"@context": "foo"}').encode('utf-8')))

    def test_passes_gracefully_for_non_activitypub_request(self):
        assert not identify_request(RequestType(body='foo'))
        assert not identify_request(RequestType(body='<xml></<xml>'))

# Path: federation/protocols/activitypub/protocol.py
# def identify_request(request: RequestType) -> bool:
#     """
#     Try to identify whether this is an ActivityPub request.
#     """
#     # noinspection PyBroadException
#     try:
#         data = json.loads(decode_if_bytes(request.body))
#         if "@context" in data:
#             return True
#     except Exception:
#         pass
#     return False
# 
# def identify_id(id: str) -> bool:
#     """
#     Try to identify whether this is an ActivityPub ID.
#     """
#     return re.match(r'^https?://', id, flags=re.IGNORECASE) is not None
# 
# Path: federation/types.py
# class RequestType:
#     """
#     Emulates structure of a Django HttpRequest for compatibility.
#     """
#     body: Union[str, bytes] = attr.ib()
# 
#     # Required when dealing with incoming AP payloads
#     headers: Dict = attr.ib(default=None)
#     method: str = attr.ib(default=None)
#     url: str = attr.ib(default=None)


# Path: federation/tests/protocols/activitypub/test_protocol.py
import json
from federation.protocols.activitypub.protocol import identify_request, identify_id
from federation.types import RequestType




def test_identify_id():
    assert identify_id('foobar') is False
    assert identify_id('foobar@example.com') is False
    assert identify_id('foobar@example.com:8000') is False
    assert identify_id('http://foobar@example.com') is True
    assert identify_id('https://foobar@example.com') is True


class TestIdentifyRequest:
    def test_identifies_activitypub_request(self):
        assert identify_request(RequestType(body=json.dumps('{"@context": "foo"}')))
        assert identify_request(RequestType(body=json.dumps('{"@context": "foo"}').encode('utf-8')))

    def test_passes_gracefully_for_non_activitypub_request(self):
        assert not identify_request(RequestType(body='foo'))
        assert not identify_request(RequestType(body='<xml></<xml>'))

        assert not identify_request(RequestType(body=b'<xml></<xml>'))