Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mege setresponsecode-distrib-5525

Author: tom.prince
Reviewer: Fahrenheit, jesstess, exarkun
Fixes: #5525

Add the `message` parameter to the PB view of `twisted.web.server.Request`.
This allows distributed servers (`twisted.web.distrib`) to set the response
message, just as any normal HTTP code in a Twisted Web server may do.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@33885 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information...
commit 9cd9dcabd48029fdf0e4e33646227ddebcd32488 1 parent 5244c10
exarkun authored
View
9 twisted/web/server.py
@@ -310,10 +310,13 @@ def view_setETag(self, issuer, tag):
"""
self.setETag(tag)
- def view_setResponseCode(self, issuer, code):
- """Remote version of setResponseCode; same interface.
+
+ def view_setResponseCode(self, issuer, code, message=None):
+ """
+ Remote version of setResponseCode; same interface.
"""
- self.setResponseCode(code)
+ self.setResponseCode(code, message)
+
def view_registerProducer(self, issuer, producer, streaming):
"""Remote version of registerProducer; same interface.
View
91 twisted/web/test/test_distrib.py
@@ -22,6 +22,7 @@
from twisted.web import http, distrib, client, resource, static, server
from twisted.web.test.test_web import DummyRequest
from twisted.web.test._util import _render
+from twisted.test import proto_helpers
class MySite(server.Site):
@@ -92,16 +93,14 @@ def testDistrib(self):
return d
- def _requestTest(self, child, **kwargs):
+ def _setupDistribServer(self, child):
"""
- Set up a resource on a distrib site using L{ResourcePublisher} and
- then retrieve it from a L{ResourceSubscription} via an HTTP client.
+ Set up a resource on a distrib site using L{ResourcePublisher}.
@param child: The resource to publish using distrib.
- @param **kwargs: Extra keyword arguments to pass to L{getPage} when
- requesting the resource.
- @return: A L{Deferred} which fires with the result of the request.
+ @return: A tuple consisting of the host and port on which to contact
+ the created site.
"""
distribRoot = resource.Resource()
distribRoot.putChild("child", child)
@@ -120,10 +119,52 @@ def _requestTest(self, child, **kwargs):
self.addCleanup(mainPort.stopListening)
mainAddr = mainPort.getHost()
+ return mainPort, mainAddr
+
+
+ def _requestTest(self, child, **kwargs):
+ """
+ Set up a resource on a distrib site using L{ResourcePublisher} and
+ then retrieve it from a L{ResourceSubscription} via an HTTP client.
+
+ @param child: The resource to publish using distrib.
+ @param **kwargs: Extra keyword arguments to pass to L{getPage} when
+ requesting the resource.
+
+ @return: A L{Deferred} which fires with the result of the request.
+ """
+ mainPort, mainAddr = self._setupDistribServer(child)
return client.getPage("http://%s:%s/child" % (
mainAddr.host, mainAddr.port), **kwargs)
+ def _requestAgentTest(self, child, **kwargs):
+ """
+ Set up a resource on a distrib site using L{ResourcePublisher} and
+ then retrieve it from a L{ResourceSubscription} via an HTTP client.
+
+ @param child: The resource to publish using distrib.
+ @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
+ requesting the resource.
+
+ @return: A L{Deferred} which fires with a tuple consisting of a
+ L{twisted.test.proto_helpers.AccumulatingProtocol} containing the
+ body of the response and an L{IResponse} with the response itself.
+ """
+ mainPort, mainAddr = self._setupDistribServer(child)
+
+ d = client.Agent(reactor).request("GET", "http://%s:%s/child" % (
+ mainAddr.host, mainAddr.port), **kwargs)
+
+ def cbCollectBody(response):
+ protocol = proto_helpers.AccumulatingProtocol()
+ response.deliverBody(protocol)
+ d = protocol.closedDeferred = defer.Deferred()
+ d.addCallback(lambda _: (protocol, response))
+ return d
+ d.addCallback(cbCollectBody)
+ return d
+
def test_requestHeaders(self):
"""
@@ -146,6 +187,44 @@ def cbRequested(result):
return request
+ def test_requestResponseCode(self):
+ """
+ The response code can be set by the request object passed to a
+ distributed resource's C{render} method.
+ """
+ class SetResponseCode(resource.Resource):
+ def render(self, request):
+ request.setResponseCode(200)
+ return ""
+
+ request = self._requestAgentTest(SetResponseCode())
+ def cbRequested(result):
+ self.assertEqual(result[0].data, "")
+ self.assertEqual(result[1].code, 200)
+ self.assertEqual(result[1].phrase, "OK")
+ request.addCallback(cbRequested)
+ return request
+
+
+ def test_requestResponseCodeMessage(self):
+ """
+ The response code and message can be set by the request object passed to
+ a distributed resource's C{render} method.
+ """
+ class SetResponseCode(resource.Resource):
+ def render(self, request):
+ request.setResponseCode(200, "some-message")
+ return ""
+
+ request = self._requestAgentTest(SetResponseCode())
+ def cbRequested(result):
+ self.assertEqual(result[0].data, "")
+ self.assertEqual(result[1].code, 200)
+ self.assertEqual(result[1].phrase, "some-message")
+ request.addCallback(cbRequested)
+ return request
+
+
def test_largeWrite(self):
"""
If a string longer than the Banana size limit is passed to the
View
1  twisted/web/topfiles/5525.bugfix
@@ -0,0 +1 @@
+twisted.web.distrib now lets distributed servers set the response message.
Please sign in to comment.
Something went wrong with that request. Please try again.