Ticket #5525: distrib_server_response_code_message_3.patch

File distrib_server_response_code_message_3.patch, 5.9 KB (added by tom.prince, 2 years ago)

Patch v3.

  • twisted/web/server.py

    From f071c3f55a443adacd2fbdaa558ae3b44b3fcb5a Mon Sep 17 00:00:00 2001
    From: Tom Prince <tom.prince@ualberta.net>
    Date: Mon, 12 Mar 2012 00:18:52 -0400
    Subject: [PATCH] Fix view_setResponseCode
    
    ---
     twisted/web/server.py            |    4 +-
     twisted/web/test/test_distrib.py |   88 +++++++++++++++++++++++++++++++++++---
     2 files changed, 84 insertions(+), 8 deletions(-)
    
    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..a412eeb 100755
    from twisted.spread.banana import SIZE_LIMIT 
    2222from twisted.web import http, distrib, client, resource, static, server 
    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): 
    9293        return d 
    9394 
    9495 
    95     def _requestTest(self, child, **kwargs): 
     96    def _setupDistribServer(self, child): 
    9697        """ 
    97         Set up a resource on a distrib site using L{ResourcePublisher} and 
    98         then retrieve it from a L{ResourceSubscription} via an HTTP client. 
     98        Set up a resource on a distrib site using L{ResourcePublisher}. 
    9999 
    100100        @param child: The resource to publish using distrib. 
    101         @param **kwargs: Extra keyword arguments to pass to L{getPage} when 
    102             requesting the resource. 
    103101 
    104         @return: A L{Deferred} which fires with the result of the request. 
     102        @return: A tuple consisting of the host and port on which to contact 
     103            the created site. 
    105104        """ 
    106105        distribRoot = resource.Resource() 
    107106        distribRoot.putChild("child", child) 
    class DistribTest(unittest.TestCase): 
    120119        self.addCleanup(mainPort.stopListening) 
    121120        mainAddr = mainPort.getHost() 
    122121 
     122        return mainPort, mainAddr 
     123 
     124 
     125    def _requestTest(self, child, **kwargs): 
     126        """ 
     127        Set up a resource on a distrib site using L{ResourcePublisher} and 
     128        then retrieve it from a L{ResourceSubscription} via an HTTP client. 
     129 
     130        @param child: The resource to publish using distrib. 
     131        @param **kwargs: Extra keyword arguments to pass to L{getPage} when 
     132            requesting the resource. 
     133 
     134        @return: A L{Deferred} which fires with the result of the request. 
     135        """ 
     136        mainPort, mainAddr = self._setupDistribServer(child) 
    123137        return client.getPage("http://%s:%s/child" % ( 
    124138            mainAddr.host, mainAddr.port), **kwargs) 
    125139 
    126140 
     141    def _requestAgentTest(self, child, **kwargs): 
     142        """ 
     143        Set up a resource on a distrib site using L{ResourcePublisher} and 
     144        then retrieve it from a L{ResourceSubscription} via an HTTP client. 
     145 
     146        @param child: The resource to publish using distrib. 
     147        @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when 
     148            requesting the resource. 
     149 
     150        @return: A L{Deferred} which fires with a tuple consisting of a 
     151            L{twisted.test.proto_helpers.AccumulatingProtocol} containing the 
     152            body of the response and an L{IResponse} with the response itself. 
     153        """ 
     154        mainPort, mainAddr = self._setupDistribServer(child) 
     155 
     156        d = client.Agent(reactor).request("GET", "http://%s:%s/child" % ( 
     157            mainAddr.host, mainAddr.port), **kwargs) 
     158 
     159        def cbCollectBody(response): 
     160            protocol = proto_helpers.AccumulatingProtocol() 
     161            response.deliverBody(protocol) 
     162            d = protocol.closedDeferred = defer.Deferred() 
     163            d.addCallback(lambda _: (protocol, response)) 
     164            return d 
     165        d.addCallback(cbCollectBody) 
     166        return d 
     167 
    127168 
    128169    def test_requestHeaders(self): 
    129170        """ 
    class DistribTest(unittest.TestCase): 
    145186        request.addCallback(cbRequested) 
    146187        return request 
    147188 
     189    def test_requestResponseCode(self): 
     190        """ 
     191        The response code can be set by the request object passed to a 
     192        distributed resource's C{render} method. 
     193        """ 
     194        class SetResponseCode(resource.Resource): 
     195            def render(self, request): 
     196                request.setResponseCode(200) 
     197                return "" 
     198 
     199        request = self._requestAgentTest(SetResponseCode()) 
     200        def cbRequested(result): 
     201            self.assertEqual(result[0].data, "") 
     202            self.assertEqual(result[1].code, 200) 
     203            self.assertEqual(result[1].phrase, "OK") 
     204        request.addCallback(cbRequested) 
     205        return request 
     206 
     207    def test_requestResponseCodeMessage(self): 
     208        """ 
     209        The response code and message can be set by the request object passed to a 
     210        distributed resource's C{render} method. 
     211        """ 
     212        class SetResponseCode(resource.Resource): 
     213            def render(self, request): 
     214                request.setResponseCode(200, "some-message") 
     215                return "" 
     216 
     217        request = self._requestAgentTest(SetResponseCode()) 
     218        def cbRequested(result): 
     219            self.assertEqual(result[0].data, "") 
     220            self.assertEqual(result[1].code, 200) 
     221            self.assertEqual(result[1].phrase, "some-message") 
     222        request.addCallback(cbRequested) 
     223        return request 
    148224 
    149225    def test_largeWrite(self): 
    150226        """