Use twisted.web.server.Request instead of DummyRequest in the tests
Always prefer the real thing if possible
This commit is contained in:
parent
e7c04f8810
commit
1ed74604c7
|
@ -8,7 +8,10 @@ from twisted.internet.defer import (
|
||||||
succeed,
|
succeed,
|
||||||
)
|
)
|
||||||
from twisted.web.test.requesthelper import (
|
from twisted.web.test.requesthelper import (
|
||||||
DummyRequest,
|
DummyChannel,
|
||||||
|
)
|
||||||
|
from twisted.web.server import (
|
||||||
|
Request,
|
||||||
)
|
)
|
||||||
from twisted.web.iweb import (
|
from twisted.web.iweb import (
|
||||||
IRequest,
|
IRequest,
|
||||||
|
@ -17,8 +20,6 @@ from twisted.web.server import (
|
||||||
NOT_DONE_YET,
|
NOT_DONE_YET,
|
||||||
)
|
)
|
||||||
|
|
||||||
classImplements(DummyRequest, IRequest)
|
|
||||||
|
|
||||||
def render(resource, query_args):
|
def render(resource, query_args):
|
||||||
"""
|
"""
|
||||||
Render (in the manner of the Twisted Web Site) a Twisted ``Resource``
|
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
|
:return Deferred: A Deferred that fires with the rendered response body as
|
||||||
``bytes``.
|
``bytes``.
|
||||||
"""
|
"""
|
||||||
request = DummyRequest([])
|
channel = DummyChannel()
|
||||||
|
request = Request(channel)
|
||||||
request.args = query_args
|
request.args = query_args
|
||||||
result = resource.render(request)
|
result = resource.render(request)
|
||||||
if isinstance(result, bytes):
|
if isinstance(result, bytes):
|
||||||
|
@ -50,5 +52,9 @@ def render(resource, query_args):
|
||||||
result,
|
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
|
return done
|
||||||
|
|
Loading…
Reference in New Issue