mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-24 07:06:41 +00:00
Merge pull request #828 from tahoe-lafs/3440.render-helper
Make the render test helper generally useful Fixes: ticket:3440
This commit is contained in:
commit
0f8cb7aedf
0
newsfragments/3440.minor
Normal file
0
newsfragments/3440.minor
Normal file
@ -1,9 +1,26 @@
|
||||
|
||||
import treq
|
||||
from twisted.internet import defer
|
||||
from twisted.internet.defer import (
|
||||
maybeDeferred,
|
||||
inlineCallbacks,
|
||||
returnValue,
|
||||
)
|
||||
from twisted.web.error import Error
|
||||
|
||||
@defer.inlineCallbacks
|
||||
from nevow.context import WebContext
|
||||
from nevow.testutil import FakeRequest
|
||||
from nevow.appserver import (
|
||||
processingFailed,
|
||||
DefaultExceptionHandler,
|
||||
)
|
||||
from nevow.inevow import (
|
||||
ICanHandleException,
|
||||
IRequest,
|
||||
IResource as INevowResource,
|
||||
IData,
|
||||
)
|
||||
|
||||
@inlineCallbacks
|
||||
def do_http(method, url, **kwargs):
|
||||
response = yield treq.request(method, url, persistent=False, **kwargs)
|
||||
body = yield treq.content(response)
|
||||
@ -11,4 +28,35 @@ def do_http(method, url, **kwargs):
|
||||
# https://github.com/twisted/treq/pull/159 has landed
|
||||
if 400 <= response.code < 600:
|
||||
raise Error(response.code, response=body)
|
||||
defer.returnValue(body)
|
||||
returnValue(body)
|
||||
|
||||
|
||||
def render(resource, query_args):
|
||||
"""
|
||||
Render (in the manner of the Nevow appserver) a Nevow ``Page`` or a
|
||||
Twisted ``Resource`` against a request with the given query arguments .
|
||||
|
||||
:param resource: The page or resource to render.
|
||||
|
||||
:param query_args: The query arguments to put into the request being
|
||||
rendered. A mapping from ``bytes`` to ``list`` of ``bytes``.
|
||||
|
||||
:return Deferred: A Deferred that fires with the rendered response body as
|
||||
``bytes``.
|
||||
"""
|
||||
ctx = WebContext(tag=resource)
|
||||
req = FakeRequest(args=query_args)
|
||||
ctx.remember(DefaultExceptionHandler(), ICanHandleException)
|
||||
ctx.remember(req, IRequest)
|
||||
ctx.remember(None, IData)
|
||||
|
||||
def maybe_concat(res):
|
||||
if isinstance(res, bytes):
|
||||
return req.v + res
|
||||
return req.v
|
||||
|
||||
resource = INevowResource(resource)
|
||||
d = maybeDeferred(resource.renderHTTP, ctx)
|
||||
d.addErrback(processingFailed, req, ctx)
|
||||
d.addCallback(maybe_concat)
|
||||
return d
|
||||
|
@ -8,24 +8,13 @@ from bs4 import BeautifulSoup
|
||||
|
||||
from twisted.application import service
|
||||
from twisted.internet import defer
|
||||
from twisted.internet.defer import inlineCallbacks, returnValue, maybeDeferred
|
||||
from twisted.internet.defer import inlineCallbacks, returnValue
|
||||
from twisted.internet.task import Clock
|
||||
from twisted.web import client, error, http
|
||||
from twisted.python import failure, log
|
||||
|
||||
from nevow.context import WebContext
|
||||
from nevow.inevow import (
|
||||
ICanHandleException,
|
||||
IRequest,
|
||||
IData,
|
||||
)
|
||||
from nevow.util import escapeToXML
|
||||
from nevow.loaders import stan
|
||||
from nevow.testutil import FakeRequest
|
||||
from nevow.appserver import (
|
||||
processingFailed,
|
||||
DefaultExceptionHandler,
|
||||
)
|
||||
|
||||
from allmydata import interfaces, uri, webish
|
||||
from allmydata.storage_client import StorageFarmBroker, StubServer
|
||||
@ -70,6 +59,7 @@ from ..common_py3 import TimezoneMixin
|
||||
from ..common_web import (
|
||||
do_http,
|
||||
Error,
|
||||
render,
|
||||
)
|
||||
from ...web.common import (
|
||||
humanize_exception,
|
||||
@ -670,6 +660,9 @@ class MultiFormatPageTests(TrialTestCase):
|
||||
"""
|
||||
Tests for ``MultiFormatPage``.
|
||||
"""
|
||||
def render(self, resource, **queryargs):
|
||||
return self.successResultOf(render(resource, queryargs))
|
||||
|
||||
def resource(self):
|
||||
"""
|
||||
Create and return an instance of a ``MultiFormatPage`` subclass with two
|
||||
@ -686,31 +679,6 @@ class MultiFormatPageTests(TrialTestCase):
|
||||
return Content()
|
||||
|
||||
|
||||
def render(self, resource, **query_args):
|
||||
"""
|
||||
Render a Nevow ``Page`` against a request with the given query arguments.
|
||||
|
||||
:param resource: The Nevow resource to render.
|
||||
|
||||
: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``.
|
||||
"""
|
||||
ctx = WebContext(tag=resource)
|
||||
req = FakeRequest(args=query_args)
|
||||
ctx.remember(DefaultExceptionHandler(), ICanHandleException)
|
||||
ctx.remember(req, IRequest)
|
||||
ctx.remember(None, IData)
|
||||
|
||||
d = maybeDeferred(resource.renderHTTP, ctx)
|
||||
d.addErrback(processingFailed, req, ctx)
|
||||
res = self.successResultOf(d)
|
||||
if isinstance(res, bytes):
|
||||
return req.v + res
|
||||
return req.v
|
||||
|
||||
|
||||
def test_select_format(self):
|
||||
"""
|
||||
The ``formatArgument`` attribute of a ``MultiFormatPage`` subclass
|
||||
|
Loading…
Reference in New Issue
Block a user