2021-09-16 18:01:14 +02:00
|
|
|
|
# Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-01-29 13:07:00 +01: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 16:25:02 +02:00
|
|
|
|
import json
|
2020-10-30 12:15:07 +01:00
|
|
|
|
import sys
|
|
|
|
|
import warnings
|
2021-09-16 18:01:14 +02:00
|
|
|
|
from binascii import unhexlify
|
2023-08-25 15:27:21 +02:00
|
|
|
|
from typing import TYPE_CHECKING, Awaitable, Callable, Tuple, TypeVar
|
2020-12-15 21:56:10 +01:00
|
|
|
|
|
2020-12-02 19:22:01 +01:00
|
|
|
|
import attr
|
2022-10-25 16:25:02 +02:00
|
|
|
|
import zope.interface
|
2020-12-02 19:22:01 +01:00
|
|
|
|
|
2023-02-08 22:29:49 +01:00
|
|
|
|
from twisted.internet.interfaces import IProtocol
|
2020-12-02 19:22:01 +01:00
|
|
|
|
from twisted.python.failure import Failure
|
|
|
|
|
from twisted.web.client import ResponseDone
|
2022-10-25 16:25:02 +02:00
|
|
|
|
from twisted.web.http import RESPONSES
|
|
|
|
|
from twisted.web.http_headers import Headers
|
|
|
|
|
from twisted.web.iweb import IResponse
|
|
|
|
|
|
2023-05-22 15:48:57 +02:00
|
|
|
|
from synapse.types import JsonSerializable
|
2020-12-02 19:22:01 +01:00
|
|
|
|
|
2023-02-08 22:29:49 +01:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from sys import UnraisableHookArgs
|
|
|
|
|
|
2020-04-29 13:30:36 +02: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 16:59:51 +02:00
|
|
|
|
|
|
|
|
|
|
2020-10-30 12:15:07 +01: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 15:03:44 +02:00
|
|
|
|
orig_unraisablehook = sys.unraisablehook
|
2020-10-30 12:15:07 +01:00
|
|
|
|
|
2023-02-08 22:29:49 +01:00
|
|
|
|
def unraisablehook(unraisable: "UnraisableHookArgs") -> None:
|
2020-10-30 12:15:07 +01:00
|
|
|
|
unraisable_exceptions.append(unraisable.exc_value)
|
|
|
|
|
|
2023-02-08 22:29:49 +01:00
|
|
|
|
def cleanup() -> None:
|
2020-10-30 12:15:07 +01: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 15:03:44 +02:00
|
|
|
|
sys.unraisablehook = orig_unraisablehook
|
2020-10-30 12:15:07 +01:00
|
|
|
|
if unraisable_exceptions:
|
2023-02-08 22:29:49 +01:00
|
|
|
|
exc = unraisable_exceptions.pop()
|
|
|
|
|
assert exc is not None
|
|
|
|
|
raise exc
|
2020-10-30 12:15:07 +01:00
|
|
|
|
|
2022-04-27 15:03:44 +02:00
|
|
|
|
sys.unraisablehook = unraisablehook
|
2020-10-30 12:15:07 +01:00
|
|
|
|
|
|
|
|
|
return cleanup
|
2020-12-02 19:22:01 +01:00
|
|
|
|
|
|
|
|
|
|
2022-10-25 16:25:02 +02: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 19:22:01 +01: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 16:25:02 +02:00
|
|
|
|
version: Tuple[bytes, int, int] = (b"HTTP", 1, 1)
|
2020-12-02 19:22:01 +01:00
|
|
|
|
|
2022-10-25 16:25:02 +02:00
|
|
|
|
# HTTP response code
|
|
|
|
|
code: int = 200
|
2020-12-02 19:22:01 +01:00
|
|
|
|
|
|
|
|
|
# body of the response
|
2022-10-25 16:25:02 +02:00
|
|
|
|
body: bytes = b""
|
|
|
|
|
|
|
|
|
|
headers: Headers = attr.Factory(Headers)
|
|
|
|
|
|
|
|
|
|
@property
|
2023-02-08 22:29:49 +01:00
|
|
|
|
def phrase(self) -> bytes:
|
2022-10-25 16:25:02 +02:00
|
|
|
|
return RESPONSES.get(self.code, b"Unknown Status")
|
|
|
|
|
|
|
|
|
|
@property
|
2023-02-08 22:29:49 +01:00
|
|
|
|
def length(self) -> int:
|
2022-10-25 16:25:02 +02:00
|
|
|
|
return len(self.body)
|
2020-12-02 19:22:01 +01:00
|
|
|
|
|
2023-02-08 22:29:49 +01:00
|
|
|
|
def deliverBody(self, protocol: IProtocol) -> None:
|
2020-12-02 19:22:01 +01:00
|
|
|
|
protocol.dataReceived(self.body)
|
|
|
|
|
protocol.connectionLost(Failure(ResponseDone()))
|
2021-09-16 18:01:14 +02:00
|
|
|
|
|
2022-10-25 16:25:02 +02:00
|
|
|
|
@classmethod
|
2023-05-22 15:48:57 +02:00
|
|
|
|
def json(cls, *, code: int = 200, payload: JsonSerializable) -> "FakeResponse":
|
2022-10-25 16:25:02 +02: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 18:01:14 +02: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"
|
|
|
|
|
)
|