Ticket #5527: twisted-web-test-util.diff

File twisted-web-test-util.diff, 14.4 KB (added by Tom Prince, 5 years ago)

initial patch

  • twisted/web/test/test_cgi.py

     
    1212from twisted.python import util, failure
    1313from twisted.web.http import NOT_FOUND, INTERNAL_SERVER_ERROR
    1414from twisted.web import client, twcgi, server, resource
    15 from twisted.web.test._util import _render
     15from twisted.web.test.util import render
    1616from twisted.web.test.test_web import DummyRequest
    1717
    1818DUMMY_CGI = '''\
     
    226226        """
    227227        resource = twcgi.CGIDirectory(self.mktemp())
    228228        request = DummyRequest([''])
    229         d = _render(resource, request)
     229        d = render(resource, request)
    230230        def cbRendered(ignored):
    231231            self.assertEqual(request.responseCode, NOT_FOUND)
    232232        d.addCallback(cbRendered)
     
    245245        resource = twcgi.CGIDirectory(path)
    246246        request = DummyRequest(['foo'])
    247247        child = resource.getChild("foo", request)
    248         d = _render(child, request)
     248        d = render(child, request)
    249249        def cbRendered(ignored):
    250250            self.assertEqual(request.responseCode, NOT_FOUND)
    251251        d.addCallback(cbRendered)
  • twisted/web/test/test_script.py

     
    1010from twisted.trial.unittest import TestCase
    1111from twisted.web.http import NOT_FOUND
    1212from twisted.web.script import ResourceScriptDirectory, PythonScript
    13 from twisted.web.test._util import _render
     13from twisted.web.test.util import render
    1414from twisted.web.test.test_web import DummyRequest
    1515
    1616
     
    2525        """
    2626        resource = ResourceScriptDirectory(self.mktemp())
    2727        request = DummyRequest([''])
    28         d = _render(resource, request)
     28        d = render(resource, request)
    2929        def cbRendered(ignored):
    3030            self.assertEqual(request.responseCode, NOT_FOUND)
    3131        d.addCallback(cbRendered)
     
    4444        resource = ResourceScriptDirectory(path)
    4545        request = DummyRequest(['foo'])
    4646        child = resource.getChild("foo", request)
    47         d = _render(child, request)
     47        d = render(child, request)
    4848        def cbRendered(ignored):
    4949            self.assertEqual(request.responseCode, NOT_FOUND)
    5050        d.addCallback(cbRendered)
     
    6363        """
    6464        resource = PythonScript(self.mktemp(), None)
    6565        request = DummyRequest([''])
    66         d = _render(resource, request)
     66        d = render(resource, request)
    6767        def cbRendered(ignored):
    6868            self.assertEqual(request.responseCode, NOT_FOUND)
    6969        d.addCallback(cbRendered)
  • twisted/web/test/_util.py

     
    1414from twisted.web.error import FlattenerError
    1515
    1616
    17 def _render(resource, request):
    18     result = resource.render(request)
    19     if isinstance(result, str):
    20         request.write(result)
    21         request.finish()
    22         return succeed(None)
    23     elif result is server.NOT_DONE_YET:
    24         if request.finished:
    25             return succeed(None)
    26         else:
    27             return request.notifyFinish()
    28     else:
    29         raise ValueError("Unexpected return value: %r" % (result,))
    30 
    31 
    32 
    3317class FlattenTestCase(TestCase):
    3418    """
    3519    A test case that assists with testing L{twisted.web._flatten}.
  • twisted/web/test/test_static.py

     
    1818from twisted.web import static, http, script, resource
    1919from twisted.web.server import UnsupportedMethod
    2020from twisted.web.test.test_web import DummyRequest
    21 from twisted.web.test._util import _render
     21from twisted.web.test.util import render
    2222
    2323
    2424class StaticDataTests(TestCase):
     
    3232        data = static.Data("foo", "bar")
    3333        request = DummyRequest([''])
    3434        request.method = 'HEAD'
    35         d = _render(data, request)
    36         def cbRendered(ignored):
    37             self.assertEqual(''.join(request.written), "")
     35        d = render(data, request)
     36        def cbRendered(res):
     37            self.assertEqual(res, "")
    3838        d.addCallback(cbRendered)
    3939        return d
    4040
     
    5656    Tests for the basic behavior of L{File}.
    5757    """
    5858    def _render(self, resource, request):
    59         return _render(resource, request)
     59        return render(resource, request)
    6060
    6161
    6262    def test_invalidMethod(self):
     
    168168        child = resource.getChildForRequest(file, request)
    169169
    170170        d = self._render(child, request)
    171         def cbRendered(ignored):
    172             self.assertEqual(''.join(request.written), 'baz')
     171        def cbRendered(res):
     172            self.assertEqual(res, 'baz')
    173173            self.assertEqual(request.outgoingHeaders['content-length'], '3')
    174174        d.addCallback(cbRendered)
    175175        return d
     
    190190        child = resource.getChildForRequest(file, request)
    191191
    192192        d = self._render(child, request)
    193         def cbRendered(ignored):
    194             self.assertEqual(''.join(request.written), 'baz')
     193        def cbRendered(res):
     194            self.assertEqual(res, 'baz')
    195195            self.assertEqual(request.outgoingHeaders['content-length'], '3')
    196196        d.addCallback(cbRendered)
    197197        return d
     
    218218        request2 = DummyRequest(['foo.bar'])
    219219        d = self._render(staticFile, request)
    220220        d2 = self._render(staticFile.childNotFound, request2)
    221         def cbRendered2(ignored):
    222             def cbRendered(ignored):
    223                 self.assertEqual(''.join(request.written),
    224                                   ''.join(request2.written))
     221        def cbRendered2(res):
     222            def cbRendered(res2):
     223                self.assertEqual(res,
     224                                  res2)
    225225            d.addCallback(cbRendered)
    226226            return d
    227227        d2.addCallback(cbRendered2)
     
    238238        file = static.File(path.path)
    239239        request = DummyRequest([''])
    240240        request.method = 'HEAD'
    241         d = _render(file, request)
    242         def cbRendered(ignored):
    243             self.assertEqual("".join(request.written), "")
     241        d = render(file, request)
     242        def cbRendered(res):
     243            self.assertEqual(res, "")
    244244        d.addCallback(cbRendered)
    245245        return d
    246246
     
    264264        child = resource.getChildForRequest(file, request)
    265265
    266266        d = self._render(child, request)
    267         def cbRendered(ignored):
    268             self.assertEqual(''.join(request.written), 'dynamic world')
     267        def cbRendered(res):
     268            self.assertEqual(res, 'dynamic world')
    269269            self.assertEqual(request.outgoingHeaders['content-length'], '13')
    270270        d.addCallback(cbRendered)
    271271        return d
     
    303303        child = resource.getChildForRequest(file, request)
    304304
    305305        d = self._render(child, request)
    306         def cbRendered(ignored):
    307             self.assertEqual(''.join(request.written), 'baz')
     306        def cbRendered(res):
     307            self.assertEqual(res, 'baz')
    308308        d.addCallback(cbRendered)
    309309        return d
    310310
     
    14541454        lister = static.DirectoryLister(path.path)
    14551455        request = self._request('')
    14561456        child = resource.getChildForRequest(lister, request)
    1457         result = _render(child, request)
     1457        result = render(child, request)
    14581458        def cbRendered(ignored):
    14591459            self.assertEqual(request.responseCode, http.NOT_FOUND)
    14601460        result.addCallback(cbRendered)
  • twisted/web/test/test_distrib.py

     
    2121from twisted.spread.banana import SIZE_LIMIT
    2222from twisted.web import http, distrib, client, resource, static, server
    2323from twisted.web.test.test_web import DummyRequest
    24 from twisted.web.test._util import _render
     24from twisted.web.test.util import render
    2525
    2626
    2727class MySite(server.Site):
     
    190190        self.sub = subscription = distrib.ResourceSubscription(
    191191            "127.0.0.1", serverPort.getHost().port)
    192192        request = DummyRequest([''])
    193         d = _render(subscription, request)
     193        d = render(subscription, request)
    194194        def cbRendered(ignored):
    195195            self.assertEqual(request.responseCode, 500)
    196196            # This is the error we caused the request to fail with.  It should
     
    244244        """
    245245        request = DummyRequest([name])
    246246        result = self.directory.getChild(name, request)
    247         d = _render(result, request)
     247        d = render(result, request)
    248248        def cbRendered(ignored):
    249249            self.assertEqual(request.responseCode, 404)
    250250        d.addCallback(cbRendered)
     
    326326        web.child('.twistd-web-pb').setContent("")
    327327
    328328        request = DummyRequest([''])
    329         result = _render(self.directory, request)
    330         def cbRendered(ignored):
    331             document = parseString(''.join(request.written))
     329        result = render(self.directory, request)
     330        def cbRendered(res):
     331            document = parseString(res)
    332332
    333333            # Each user should have an li with a link to their page.
    334334            [alice, bob] = document.getElementsByTagName('li')
  • twisted/web/test/test_vhost.py

     
    1111from twisted.web.static import Data
    1212from twisted.web.vhost import NameVirtualHost
    1313from twisted.web.test.test_web import DummyRequest
    14 from twisted.web.test._util import _render
     14from twisted.web.test.util import render
    1515
    1616class NameVirtualHostTests(TestCase):
    1717    """
     
    3838        """
    3939        virtualHostResource = NameVirtualHost()
    4040        request = DummyRequest([''])
    41         d = _render(virtualHostResource, request)
     41        d = render(virtualHostResource, request)
    4242        def cbRendered(ignored):
    4343            self.assertEqual(request.responseCode, NOT_FOUND)
    4444        d.addCallback(cbRendered)
     
    5656
    5757        request = DummyRequest([''])
    5858        request.headers['host'] = 'example.org'
    59         d = _render(virtualHostResource, request)
    60         def cbRendered(ignored, request):
    61             self.assertEqual(''.join(request.written), "winner")
    62         d.addCallback(cbRendered, request)
     59        d = render(virtualHostResource, request)
     60        def cbRendered(res):
     61            self.assertEqual(res, "winner")
     62        d.addCallback(cbRendered)
    6363
    6464        # The port portion of the Host header should not be considered.
    6565        requestWithPort = DummyRequest([''])
    6666        requestWithPort.headers['host'] = 'example.org:8000'
    67         dWithPort = _render(virtualHostResource, requestWithPort)
    68         def cbRendered(ignored, requestWithPort):
    69             self.assertEqual(''.join(requestWithPort.written), "winner")
    70         dWithPort.addCallback(cbRendered, requestWithPort)
     67        dWithPort = render(virtualHostResource, requestWithPort)
     68        def cbRendered(res):
     69            self.assertEqual(res, "winner")
     70        dWithPort.addCallback(cbRendered)
    7171
    7272        return gatherResults([d, dWithPort])
    7373
     
    8282        virtualHostResource.default = Data("correct data", "")
    8383        request = DummyRequest([''])
    8484        request.headers['host'] = 'example.com'
    85         d = _render(virtualHostResource, request)
    86         def cbRendered(ignored):
    87             self.assertEqual(''.join(request.written), "correct data")
     85        d = render(virtualHostResource, request)
     86        def cbRendered(res):
     87            self.assertEqual(res, "correct data")
    8888        d.addCallback(cbRendered)
    8989        return d
    9090
     
    9898        virtualHostResource = NameVirtualHost()
    9999        request = DummyRequest([''])
    100100        request.headers['host'] = 'example.com'
    101         d = _render(virtualHostResource, request)
     101        d = render(virtualHostResource, request)
    102102        def cbRendered(ignored):
    103103            self.assertEqual(request.responseCode, NOT_FOUND)
    104104        d.addCallback(cbRendered)
  • twisted/web/test/test_util.py

     
    22# See LICENSE for details.
    33
    44"""
    5 Tests for L{twisted.web.util}.
     5Tests for L{twisted.web.util} and L{twisted.web.test.util}.
    66"""
    77
    88from twisted.python.failure import Failure
     
    1515    FailureElement, formatFailure)
    1616
    1717from twisted.web.http import FOUND
    18 from twisted.web.server import Request
     18from twisted.web.server import Request, NOT_DONE_YET
    1919from twisted.web.template import TagLoader, flattenString, tags
     20from twisted.web.resource import Resource
     21from twisted.web.test.util import render
    2022
    2123from twisted.web.test.test_web import DummyChannel
    2224
     
    378380        L{twisted.web.util.stylesheet} is deprecated.
    379381        """
    380382        self._htmlHelperDeprecationTest("stylesheet")
     383
     384
     385
     386class RenderTestCase(TestCase):
     387    """
     388    Tests for L{render}.
     389    """
     390
     391    def test_render(self):
     392        request = DummyRequest("")
     393        class TestResource(Resource):
     394            def render(self, request):
     395                return "test-result"
     396        d = render(TestResource(), request)
     397        def cbRendered(res):
     398            self.assertEqual(res, "test-result")
     399        d.addCallback(cbRendered)
     400        return d
     401
     402
     403    def test_render_write(self):
     404        request = DummyRequest("")
     405        class TestResource(Resource):
     406            def render(self, request):
     407                request.write("test-result")
     408                return ""
     409        d = render(TestResource(), request)
     410        def cbRendered(res):
     411            self.assertEqual(res, "test-result")
     412        d.addCallback(cbRendered)
     413        return d
     414
     415    def test_render_NOT_DONE_YET(self):
     416        request = DummyRequest("")
     417        class TestResource(Resource):
     418            def render(self, request):
     419                return NOT_DONE_YET
     420        d = render(TestResource(), request)
     421        def cbRendered(res):
     422            self.assertEqual(res, "test-result")
     423        d.addCallback(cbRendered)
     424        request.write("test-results")
     425        request.finish()
     426        return d