Use twisted.web.server.Request instead of DummyRequest in the tests

Always prefer the real thing if possible
This commit is contained in:
Jean-Paul Calderone 2020-10-21 07:15:36 -04:00
parent e7c04f8810
commit 1ed74604c7

View File

@ -8,7 +8,10 @@ from twisted.internet.defer import (
succeed,
)
from twisted.web.test.requesthelper import (
DummyRequest,
DummyChannel,
)
from twisted.web.server import (
Request,
)
from twisted.web.iweb import (
IRequest,
@ -17,8 +20,6 @@ from twisted.web.server import (
NOT_DONE_YET,
)
classImplements(DummyRequest, IRequest)
def render(resource, query_args):
"""
Render (in the manner of the Twisted Web Site) a Twisted ``Resource``
@ -32,7 +33,8 @@ def render(resource, query_args):
:return Deferred: A Deferred that fires with the rendered response body as
``bytes``.
"""
request = DummyRequest([])
channel = DummyChannel()
request = Request(channel)
request.args = query_args
result = resource.render(request)
if isinstance(result, bytes):
@ -50,5 +52,9 @@ def render(resource, query_args):
result,
),
)
done.addCallback(lambda ignored: b"".join(request.written))
def get_body(ignored):
complete_response = channel.transport.written.getvalue()
header, body = complete_response.split(b"\r\n\r\n", 1)
return body
done.addCallback(get_body)
return done