Ticket #5525: distrib_server_response_code_message_2.patch

File distrib_server_response_code_message_2.patch, 5.3 KB (added by tom.prince, 4 years ago)

patch v2

  • twisted/web/server.py

    commit d8edfdb62d4b9210353301497ad19d6ce4932a42
    Author: Tom Prince <tom.prince@ualberta.net>
    Date:   Mon Mar 12 00:18:52 2012 -0400
    
        Fix view_setResponseCode
    
    diff --git twisted/web/server.py twisted/web/server.py
    index 8edab18..9f4555a 100644
    class Request(pb.Copyable, http.Request, components.Componentized): 
    286286        """
    287287        self.setETag(tag)
    288288
    289     def view_setResponseCode(self, issuer, code):
     289    def view_setResponseCode(self, issuer, code, message=None):
    290290        """Remote version of setResponseCode; same interface.
    291291        """
    292         self.setResponseCode(code)
     292        self.setResponseCode(code, message)
    293293
    294294    def view_registerProducer(self, issuer, producer, streaming):
    295295        """Remote version of registerProducer; same interface.
  • twisted/web/test/test_distrib.py

    diff --git twisted/web/test/test_distrib.py twisted/web/test/test_distrib.py
    index 172dab3..2a81f6c 100755
    from twisted.internet import reactor, defer 
    1919from twisted.trial import unittest
    2020from twisted.spread import pb
    2121from twisted.spread.banana import SIZE_LIMIT
    22 from twisted.web import http, distrib, client, resource, static, server
     22from twisted.web import http, distrib, client, resource, static, server, http_headers
    2323from twisted.web.test.test_web import DummyRequest
    2424from twisted.web.test._util import _render
     25from twisted.test import proto_helpers
    2526
    2627
    2728class MySite(server.Site):
    class DistribTest(unittest.TestCase): 
    101102        @param **kwargs: Extra keyword arguments to pass to L{getPage} when
    102103            requesting the resource.
    103104
    104         @return: A L{Deferred} which fires with the result of the request.
     105        @return: A L{Deferred} which fires with a tuple consiting of a
     106            L{twisted.test.proto_helpers.AccumulatingProtocol} containing the
     107            body of the response and an L{IResposne} with the response itself.
    105108        """
    106109        distribRoot = resource.Resource()
    107110        distribRoot.putChild("child", child)
    class DistribTest(unittest.TestCase): 
    120123        self.addCleanup(mainPort.stopListening)
    121124        mainAddr = mainPort.getHost()
    122125
    123         return client.getPage("http://%s:%s/child" % (
     126        d = client.Agent(reactor).request("GET", "http://%s:%s/child" % (
    124127            mainAddr.host, mainAddr.port), **kwargs)
    125128
    126 
     129        def cbCollectBody(response):
     130            protocol = proto_helpers.AccumulatingProtocol()
     131            response.deliverBody(protocol)
     132            d = protocol.closedDeferred = defer.Deferred()
     133            d.addCallback(lambda _: (protocol, response))
     134            return d
     135        d.addCallback(cbCollectBody)
     136        return d
    127137
    128138    def test_requestHeaders(self):
    129139        """
    class DistribTest(unittest.TestCase): 
    138148                    request.requestHeaders.getAllRawHeaders()))
    139149                return ""
    140150
     151        headers = http_headers.Headers()
     152        headers.addRawHeader('foo', 'bar')
    141153        request = self._requestTest(
    142             ReportRequestHeaders(), headers={'foo': 'bar'})
     154            ReportRequestHeaders(), headers=headers)
    143155        def cbRequested(result):
    144156            self.assertEqual(requestHeaders['Foo'], ['bar'])
    145157        request.addCallback(cbRequested)
    146158        return request
    147159
     160    def test_requestResponseCode(self):
     161        """
     162        The response code can be set by the request object passed to a
     163        distributed resource's C{render} method.
     164        """
     165        class SetResponseCode(resource.Resource):
     166            def render(self, request):
     167                request.setResponseCode(200)
     168                return ""
     169
     170        request = self._requestTest(SetResponseCode())
     171        def cbRequested(result):
     172            self.assertEqual(result[0].data, "")
     173            self.assertEqual(result[1].code, 200)
     174            self.assertEqual(result[1].phrase, "OK")
     175        request.addCallback(cbRequested)
     176        return request
     177
     178    def test_requestResponseCodeMessage(self):
     179        """
     180        The response code and message can be set by the request object passed to a
     181        distributed resource's C{render} method.
     182        """
     183        class SetResponseCode(resource.Resource):
     184            def render(self, request):
     185                request.setResponseCode(200, "some-message")
     186                return ""
     187
     188        request = self._requestTest(SetResponseCode())
     189        def cbRequested(result):
     190            self.assertEqual(result[0].data, "")
     191            self.assertEqual(result[1].code, 200)
     192            self.assertEqual(result[1].phrase, "some-message")
     193        request.addCallback(cbRequested)
     194        return request
    148195
    149196    def test_largeWrite(self):
    150197        """
    class DistribTest(unittest.TestCase): 
    159206                return server.NOT_DONE_YET
    160207
    161208        request = self._requestTest(LargeWrite())
     209        request.addCallback(lambda result: result[0].data)
    162210        request.addCallback(self.assertEqual, 'x' * SIZE_LIMIT + 'y')
    163211        return request
    164212
    class DistribTest(unittest.TestCase): 
    173221                return 'x' * SIZE_LIMIT + 'y'
    174222
    175223        request = self._requestTest(LargeReturn())
     224        request.addCallback(lambda result: result[0].data)
    176225        request.addCallback(self.assertEqual, 'x' * SIZE_LIMIT + 'y')
    177226        return request
    178227