From d2bcebecaa96d0d634c921214f46b628e9e11bba Mon Sep 17 00:00:00 2001 From: Sajith Sasidharan Date: Wed, 19 Aug 2020 14:52:20 -0400 Subject: [PATCH] Render resource in a simpler manner --- src/allmydata/test/web/test_web.py | 31 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/allmydata/test/web/test_web.py b/src/allmydata/test/web/test_web.py index 70fea4f54..b34a50c49 100644 --- a/src/allmydata/test/web/test_web.py +++ b/src/allmydata/test/web/test_web.py @@ -691,20 +691,27 @@ class MultiFormatResourceTests(TrialTestCase): :param query_args: The query arguments to put into the request being rendered. A mapping from ``bytes`` to ``list`` of ``bytes``. - :return: The rendered response body as ``bytes``. + :return: The rendered response body as ``str``. """ - ctx = WebContext(tag=resource) - req = FakeRequest(args=query_args) - ctx.remember(DefaultExceptionHandler(), ICanHandleException) - ctx.remember(req, IRequest) - ctx.remember(None, IData) - d = maybeDeferred(resource.render, ctx) - d.addErrback(processingFailed, req, ctx) - res = self.successResultOf(d) - if isinstance(res, bytes): - return req.v + res - return req.v + # TODO: probably should: (1) refactor this out of here to a + # common module (test.common_web maybe?), and (2) replace + # nevow.inevow.IRequest with twisted.web.iweb.IRequest. For + # (2) to happen, we will have to update web.common.get_arg() + # etc first. + from zope.interface import implementer + from nevow.inevow import IRequest + from twisted.web.server import Request + from twisted.web.test.requesthelper import DummyChannel + + @implementer(IRequest) + class FakeRequest(Request): + def __init__(self, args): + Request.__init__(self, DummyChannel()) + self.args = args + self.fields = dict() + + return resource.render(FakeRequest(args=query_args)) def test_select_format(self):