mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-24 04:55:53 +00:00
Reuse MultiFormatPage tests for MultiFormatResource
This commit is contained in:
parent
d5853ccb60
commit
320830cf90
@ -33,7 +33,7 @@ from allmydata.immutable import upload
|
||||
from allmydata.immutable.downloader.status import DownloadStatus
|
||||
from allmydata.dirnode import DirectoryNode
|
||||
from allmydata.nodemaker import NodeMaker
|
||||
from allmydata.web.common import WebError
|
||||
from allmydata.web.common import WebError, MultiFormatResource
|
||||
from allmydata.util import fileutil, base32, hashutil
|
||||
from allmydata.util.consumer import download_to_data
|
||||
from allmydata.util.encodingutil import to_str
|
||||
@ -657,6 +657,120 @@ class WebMixin(TimezoneMixin):
|
||||
(which, res))
|
||||
|
||||
|
||||
class MultiFormatResourceTests(TrialTestCase):
|
||||
"""
|
||||
Tests for ``MultiFormatResource``.
|
||||
"""
|
||||
|
||||
def resource(self):
|
||||
"""
|
||||
Create and return an instance of a ``MultiFormatResource``
|
||||
subclass with a default HTML format, and two custom formats:
|
||||
``a`` and ``b``.
|
||||
"""
|
||||
class Content(MultiFormatResource):
|
||||
|
||||
def render_HTML(self, req):
|
||||
return "html"
|
||||
|
||||
def render_A(self, req):
|
||||
return "a"
|
||||
|
||||
def render_B(self, req):
|
||||
return "b"
|
||||
|
||||
return Content()
|
||||
|
||||
|
||||
def render(self, resource, **query_args):
|
||||
"""
|
||||
Render a ``Resource`` 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.render, 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 ``MultiFormatResource``
|
||||
subclass identifies the query argument which selects the
|
||||
result format.
|
||||
"""
|
||||
resource = self.resource()
|
||||
resource.formatArgument = "foo"
|
||||
self.assertEqual("a", self.render(resource, foo=["a"]))
|
||||
|
||||
|
||||
def test_default_format_argument(self):
|
||||
"""
|
||||
If a ``MultiFormatResource`` subclass does not set
|
||||
``formatArgument`` then the ``t`` argument is used.
|
||||
"""
|
||||
resource = self.resource()
|
||||
self.assertEqual("a", self.render(resource, t=["a"]))
|
||||
|
||||
|
||||
def test_no_format(self):
|
||||
"""
|
||||
If no value is given for the format argument and no default
|
||||
format has been defined, the base rendering behavior is used
|
||||
(``render_HTML``).
|
||||
"""
|
||||
resource = self.resource()
|
||||
self.assertEqual("html", self.render(resource))
|
||||
|
||||
|
||||
def test_default_format(self):
|
||||
"""
|
||||
If no value is given for the format argument and the
|
||||
``MultiFormatResource`` subclass defines a ``formatDefault``,
|
||||
that value is used as the format to render.
|
||||
"""
|
||||
resource = self.resource()
|
||||
resource.formatDefault = "b"
|
||||
self.assertEqual("b", self.render(resource))
|
||||
|
||||
|
||||
def test_explicit_none_format_renderer(self):
|
||||
"""
|
||||
If a format is selected which has a renderer set to ``None``,
|
||||
the base rendering behavior is used (``render_HTML``).
|
||||
"""
|
||||
resource = self.resource()
|
||||
resource.render_FOO = None
|
||||
self.assertEqual("html", self.render(resource, t=["foo"]))
|
||||
|
||||
|
||||
def test_unknown_format(self):
|
||||
"""
|
||||
If a format is selected for which there is no renderer, an error is
|
||||
returned.
|
||||
"""
|
||||
resource = self.resource()
|
||||
self.assertIn(
|
||||
"<title>Exception</title>",
|
||||
self.render(resource, t=["foo"]),
|
||||
)
|
||||
self.flushLoggedErrors(WebError)
|
||||
|
||||
|
||||
class Web(WebMixin, WebErrorMixin, testutil.StallMixin, testutil.ReallyEqualMixin, TrialTestCase):
|
||||
maxDiff = None
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user