# Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ General helpers for L{twisted.web} unit tests. """ from __future__ import division, absolute_import from twisted.internet.defer import succeed from twisted.web import server from twisted.trial.unittest import TestCase from twisted.python.failure import Failure from twisted.web._flatten import flattenString from twisted.web.error import FlattenerError def _render(resource, request): result = resource.render(request) if isinstance(result, bytes): request.write(result) request.finish() return succeed(None) elif result is server.NOT_DONE_YET: if request.finished: return succeed(None) else: return request.notifyFinish() else: raise ValueError("Unexpected return value: %r" % (result,)) class FlattenTestCase(TestCase): """ A test case that assists with testing L{twisted.web._flatten}. """ def assertFlattensTo(self, root, target): """ Assert that a root element, when flattened, is equal to a string. """ d = flattenString(None, root) d.addCallback(lambda s: self.assertEqual(s, target)) return d def assertFlattensImmediately(self, root, target): """ Assert that a root element, when flattened, is equal to a string, and performs no asynchronus Deferred anything. This version is more convenient in tests which wish to make multiple assertions about flattening, since it can be called multiple times without having to add multiple callbacks. @return: the result of rendering L{root}, which should be equivalent to L{target}. @rtype: L{bytes} """ results = [] it = self.assertFlattensTo(root, target) it.addBoth(results.append) # Do our best to clean it up if something goes wrong. self.addCleanup(it.cancel) if not results: self.fail("Rendering did not complete immediately.") result = results[0] if isinstance(result, Failure): result.raiseException() return results[0] def assertFlatteningRaises(self, root, exn): """ Assert flattening a root element raises a particular exception. """ d = self.assertFailure(self.assertFlattensTo(root, b''), FlattenerError) d.addCallback(lambda exc: self.assertIsInstance(exc._exception, exn)) return d __all__ = ["_render", "FlattenTestCase"]
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
__init__.py | File | 108 B | 0644 |
|
_util.py | File | 2.51 KB | 0644 |
|
injectionhelpers.py | File | 5.5 KB | 0644 |
|
requesthelper.py | File | 10.6 KB | 0644 |
|
test_agent.py | File | 113.19 KB | 0644 |
|
test_cgi.py | File | 13.29 KB | 0644 |
|
test_client.py | File | 1.34 KB | 0644 |
|
test_distrib.py | File | 16.02 KB | 0644 |
|
test_domhelpers.py | File | 10.84 KB | 0644 |
|
test_error.py | File | 15.83 KB | 0644 |
|
test_flatten.py | File | 17.83 KB | 0644 |
|
test_html.py | File | 1.23 KB | 0644 |
|
test_http.py | File | 122.6 KB | 0644 |
|
test_http2.py | File | 105.98 KB | 0644 |
|
test_http_headers.py | File | 19.91 KB | 0644 |
|
test_httpauth.py | File | 22.75 KB | 0644 |
|
test_newclient.py | File | 102.47 KB | 0644 |
|
test_proxy.py | File | 19.62 KB | 0644 |
|
test_resource.py | File | 8.02 KB | 0644 |
|
test_script.py | File | 3.7 KB | 0644 |
|
test_stan.py | File | 5.53 KB | 0644 |
|
test_static.py | File | 62.22 KB | 0644 |
|
test_tap.py | File | 10.34 KB | 0644 |
|
test_template.py | File | 24.99 KB | 0644 |
|
test_util.py | File | 12.29 KB | 0644 |
|
test_vhost.py | File | 7.22 KB | 0644 |
|
test_web.py | File | 55.24 KB | 0644 |
|
test_web__responses.py | File | 877 B | 0644 |
|
test_webclient.py | File | 57.45 KB | 0644 |
|
test_wsgi.py | File | 73.06 KB | 0644 |
|
test_xml.py | File | 41.36 KB | 0644 |
|
test_xmlrpc.py | File | 28.24 KB | 0644 |
|