2021-09-16 10:01:14 -06:00
|
|
|
|
# Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-01-29 05:07:00 -07:00
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Utilities for running the unit tests
|
|
|
|
|
"""
|
2022-10-25 08:25:02 -06:00
|
|
|
|
import json
|
2020-10-30 05:15:07 -06:00
|
|
|
|
import sys
|
|
|
|
|
import warnings
|
2021-09-16 10:01:14 -06:00
|
|
|
|
from binascii import unhexlify
|
2023-08-25 07:27:21 -06:00
|
|
|
|
from typing import TYPE_CHECKING, Awaitable, Callable, Tuple, TypeVar
|
2020-12-15 13:56:10 -07:00
|
|
|
|
|
2020-12-02 11:22:01 -07:00
|
|
|
|
import attr
|
2022-10-25 08:25:02 -06:00
|
|
|
|
import zope.interface
|
2020-12-02 11:22:01 -07:00
|
|
|
|
|
2023-02-08 14:29:49 -07:00
|
|
|
|
from twisted.internet.interfaces import IProtocol
|
2020-12-02 11:22:01 -07:00
|
|
|
|
from twisted.python.failure import Failure
|
|
|
|
|
from twisted.web.client import ResponseDone
|
2022-10-25 08:25:02 -06:00
|
|
|
|
from twisted.web.http import RESPONSES
|
|
|
|
|
from twisted.web.http_headers import Headers
|
|
|
|
|
from twisted.web.iweb import IResponse
|
|
|
|
|
|
2023-05-22 07:48:57 -06:00
|
|
|
|
from synapse.types import JsonSerializable
|
2020-12-02 11:22:01 -07:00
|
|
|
|
|
2023-02-08 14:29:49 -07:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from sys import UnraisableHookArgs
|
|
|
|
|
|
2020-04-29 05:30:36 -06:00
|
|
|
|
TV = TypeVar("TV")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_awaitable_result(awaitable: Awaitable[TV]) -> TV:
|
|
|
|
|
"""Get the result from an Awaitable which should have completed
|
|
|
|
|
|
|
|
|
|
Asserts that the given awaitable has a result ready, and returns its value
|
|
|
|
|
"""
|
|
|
|
|
i = awaitable.__await__()
|
|
|
|
|
try:
|
|
|
|
|
next(i)
|
|
|
|
|
except StopIteration as e:
|
|
|
|
|
# awaitable returned a result
|
|
|
|
|
return e.value
|
|
|
|
|
|
|
|
|
|
# if next didn't raise, the awaitable hasn't completed.
|
|
|
|
|
raise Exception("awaitable has not yet completed")
|
2020-07-24 08:59:51 -06:00
|
|
|
|
|
|
|
|
|
|
2020-10-30 05:15:07 -06:00
|
|
|
|
def setup_awaitable_errors() -> Callable[[], None]:
|
|
|
|
|
"""
|
|
|
|
|
Convert warnings from a non-awaited coroutines into errors.
|
|
|
|
|
"""
|
|
|
|
|
warnings.simplefilter("error", RuntimeWarning)
|
|
|
|
|
|
|
|
|
|
# State shared between unraisablehook and check_for_unraisable_exceptions.
|
|
|
|
|
unraisable_exceptions = []
|
2022-04-27 07:03:44 -06:00
|
|
|
|
orig_unraisablehook = sys.unraisablehook
|
2020-10-30 05:15:07 -06:00
|
|
|
|
|
2023-02-08 14:29:49 -07:00
|
|
|
|
def unraisablehook(unraisable: "UnraisableHookArgs") -> None:
|
2020-10-30 05:15:07 -06:00
|
|
|
|
unraisable_exceptions.append(unraisable.exc_value)
|
|
|
|
|
|
2023-02-08 14:29:49 -07:00
|
|
|
|
def cleanup() -> None:
|
2020-10-30 05:15:07 -06:00
|
|
|
|
"""
|
|
|
|
|
A method to be used as a clean-up that fails a test-case if there are any new unraisable exceptions.
|
|
|
|
|
"""
|
2022-04-27 07:03:44 -06:00
|
|
|
|
sys.unraisablehook = orig_unraisablehook
|
2020-10-30 05:15:07 -06:00
|
|
|
|
if unraisable_exceptions:
|
2023-02-08 14:29:49 -07:00
|
|
|
|
exc = unraisable_exceptions.pop()
|
|
|
|
|
assert exc is not None
|
|
|
|
|
raise exc
|
2020-10-30 05:15:07 -06:00
|
|
|
|
|
2022-04-27 07:03:44 -06:00
|
|
|
|
sys.unraisablehook = unraisablehook
|
2020-10-30 05:15:07 -06:00
|
|
|
|
|
|
|
|
|
return cleanup
|
2020-12-02 11:22:01 -07:00
|
|
|
|
|
|
|
|
|
|
2022-10-25 08:25:02 -06:00
|
|
|
|
# Type ignore: it does not fully implement IResponse, but is good enough for tests
|
|
|
|
|
@zope.interface.implementer(IResponse)
|
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
|
class FakeResponse: # type: ignore[misc]
|
2020-12-02 11:22:01 -07:00
|
|
|
|
"""A fake twisted.web.IResponse object
|
|
|
|
|
|
|
|
|
|
there is a similar class at treq.test.test_response, but it lacks a `phrase`
|
|
|
|
|
attribute, and didn't support deliverBody until recently.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-10-25 08:25:02 -06:00
|
|
|
|
version: Tuple[bytes, int, int] = (b"HTTP", 1, 1)
|
2020-12-02 11:22:01 -07:00
|
|
|
|
|
2022-10-25 08:25:02 -06:00
|
|
|
|
# HTTP response code
|
|
|
|
|
code: int = 200
|
2020-12-02 11:22:01 -07:00
|
|
|
|
|
|
|
|
|
# body of the response
|
2022-10-25 08:25:02 -06:00
|
|
|
|
body: bytes = b""
|
|
|
|
|
|
|
|
|
|
headers: Headers = attr.Factory(Headers)
|
|
|
|
|
|
|
|
|
|
@property
|
2023-02-08 14:29:49 -07:00
|
|
|
|
def phrase(self) -> bytes:
|
2022-10-25 08:25:02 -06:00
|
|
|
|
return RESPONSES.get(self.code, b"Unknown Status")
|
|
|
|
|
|
|
|
|
|
@property
|
2023-02-08 14:29:49 -07:00
|
|
|
|
def length(self) -> int:
|
2022-10-25 08:25:02 -06:00
|
|
|
|
return len(self.body)
|
2020-12-02 11:22:01 -07:00
|
|
|
|
|
2023-02-08 14:29:49 -07:00
|
|
|
|
def deliverBody(self, protocol: IProtocol) -> None:
|
2020-12-02 11:22:01 -07:00
|
|
|
|
protocol.dataReceived(self.body)
|
|
|
|
|
protocol.connectionLost(Failure(ResponseDone()))
|
2021-09-16 10:01:14 -06:00
|
|
|
|
|
2022-10-25 08:25:02 -06:00
|
|
|
|
@classmethod
|
2023-05-22 07:48:57 -06:00
|
|
|
|
def json(cls, *, code: int = 200, payload: JsonSerializable) -> "FakeResponse":
|
2022-10-25 08:25:02 -06:00
|
|
|
|
headers = Headers({"Content-Type": ["application/json"]})
|
|
|
|
|
body = json.dumps(payload).encode("utf-8")
|
|
|
|
|
return cls(code=code, body=body, headers=headers)
|
|
|
|
|
|
2021-09-16 10:01:14 -06:00
|
|
|
|
|
|
|
|
|
# A small image used in some tests.
|
|
|
|
|
#
|
|
|
|
|
# Resolution: 1×1, MIME type: image/png, Extension: png, Size: 67 B
|
|
|
|
|
SMALL_PNG = unhexlify(
|
|
|
|
|
b"89504e470d0a1a0a0000000d4948445200000001000000010806"
|
|
|
|
|
b"0000001f15c4890000000a49444154789c63000100000500010d"
|
|
|
|
|
b"0a2db40000000049454e44ae426082"
|
|
|
|
|
)
|