Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Stop ignoring tests/server.py #15084

Merged
merged 21 commits into from
Feb 17, 2023
Merged
Changes from 1 commit
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
50 changes: 36 additions & 14 deletions tests/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
Tuple,
Type,
Union,
cast,
)
from unittest.mock import Mock

import attr
from typing_extensions import Deque
from zope.interface import implementer
from zope.interface import implementer, providedBy

from twisted.internet import address, threads, udp
from twisted.internet._resolver import SimpleResolverComplexifier
Expand All @@ -47,6 +48,7 @@
IAddress,
IConsumer,
IHostnameResolver,
IProducer,
IProtocol,
IPullProducer,
IPushProducer,
Expand Down Expand Up @@ -99,12 +101,14 @@ class TimedOutException(Exception):
"""


@implementer(IConsumer)
@implementer(ITransport, IPushProducer, IConsumer)
@attr.s(auto_attribs=True)
class FakeChannel:
"""
A fake Twisted Web Channel (the part that interfaces with the
wire).

See twisted.web.http.HTTPChannel.
"""

site: Union[Site, "FakeSite"]
Expand Down Expand Up @@ -143,7 +147,7 @@ def text_body(self) -> str:

Raises an exception if the request has not yet completed.
"""
if not self.is_finished:
if not self.is_finished():
clokep marked this conversation as resolved.
Show resolved Hide resolved
raise Exception("Request not yet completed")
return self.result["body"].decode("utf8")

Expand All @@ -166,27 +170,35 @@ def headers(self) -> Headers:
h.addRawHeader(*i)
return h

def writeHeaders(self, version, code, reason, headers):
def writeHeaders(
self, version: bytes, code: bytes, reason: bytes, headers: Headers
) -> None:
self.result["version"] = version
self.result["code"] = code
self.result["reason"] = reason
self.result["headers"] = headers

def write(self, content: bytes) -> None:
assert isinstance(content, bytes), "Should be bytes! " + repr(content)
def write(self, data: bytes) -> None:
assert isinstance(data, bytes), "Should be bytes! " + repr(data)

if "body" not in self.result:
self.result["body"] = b""

self.result["body"] += content
self.result["body"] += data

def writeSequence(self, data: Iterable[bytes]) -> None:
for x in data:
self.write(x)

def loseConnection(self) -> None:
self.unregisterProducer()
self.transport.loseConnection()
clokep marked this conversation as resolved.
Show resolved Hide resolved

# Type ignore: mypy doesn't like the fact that producer isn't an IProducer.
def registerProducer( # type: ignore[override]
self,
producer: Union[IPullProducer, IPushProducer],
streaming: bool,
) -> None:
self._producer = producer
def registerProducer(self, producer: IProducer, streaming: bool) -> None:
# Ensure that the producer implements one or more of IPushProducer and IPullProducer.
assert not set(providedBy(producer)).isdisjoint({IPushProducer, IPullProducer})
self._producer = cast(Union[IPushProducer, IPullProducer], producer)
clokep marked this conversation as resolved.
Show resolved Hide resolved
self.producerStreaming = streaming

def _produce() -> None:
Expand All @@ -203,6 +215,16 @@ def unregisterProducer(self) -> None:

self._producer = None

def stopProducing(self) -> None:
if self._producer is not None:
self._producer.stopProducing()

def pauseProducing(self) -> None:
raise NotImplementedError()

def resumeProducing(self) -> None:
raise NotImplementedError()

def requestDone(self, _self: Request) -> None:
self.result["done"] = True
if isinstance(_self, SynapseRequest):
Expand Down Expand Up @@ -282,7 +304,7 @@ def __init__(
self.reactor = reactor
self.experimental_cors_msc3886 = experimental_cors_msc3886

def getResourceFor(self, request):
def getResourceFor(self, request: Request) -> IResource:
return self._resource


Expand Down