Skip to content

Commit

Permalink
Merge pull request twisted#1034 from dmollerm/9192-dmollerm-reverse_p…
Browse files Browse the repository at this point in the history
…roxy_resource_docstring

Author: dmollerm
Reviewer: wiml
Fixes: ticket:9192

Fix reverse proxy resource docstring; elaborate tests.
  • Loading branch information
wiml authored Nov 21, 2018
2 parents 8d18e4f + cdac057 commit 9a7ce38
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/web/examples/reverse-proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
from twisted.internet import reactor
from twisted.web import proxy, server

site = server.Site(proxy.ReverseProxyResource('www.yahoo.com', 80, ''))
site = server.Site(proxy.ReverseProxyResource('example.com', 80, b''))
reactor.listenTCP(8080, site)
reactor.run()
1 change: 1 addition & 0 deletions src/twisted/newsfragments/9192.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.web.proxy.ReverseProxyResource fixed documentation and example snippet
4 changes: 2 additions & 2 deletions src/twisted/web/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def __init__(self, host, port, path, reactor=reactor):
be proxied to B{/foo/bar}. Any required encoding of special
characters (such as " " or "/") should have been done already.
@type path: C{str}
@type path: C{bytes}
"""
Resource.__init__(self)
self.host = host
Expand Down Expand Up @@ -288,7 +288,7 @@ def render(self, request):
if self.port == 80:
host = self.host
else:
host = self.host + u":" + str(self.port)
host = u"%s:%d" % (self.host, self.port)
request.requestHeaders.setRawHeaders(b"host", [host.encode('ascii')])
request.content.seek(0, 0)
qs = urllib_parse.urlparse(request.uri)[4]
Expand Down
16 changes: 12 additions & 4 deletions src/twisted/web/test/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ def _testRender(self, uri, expectedURI):
uri +
b" HTTP/1.1\r\nAccept: text/html\r\n\r\n")

[(host, port, factory, _timeout, _bind_addr)] = reactor.tcpClients
# Check that one connection has been created, to the good host/port
self.assertEqual(len(reactor.tcpClients), 1)
self.assertEqual(reactor.tcpClients[0][0], u"127.0.0.1")
self.assertEqual(reactor.tcpClients[0][1], 1234)
self.assertEqual(host, u"127.0.0.1")
self.assertEqual(port, 1234)

# Check the factory passed to the connect, and its given path
factory = reactor.tcpClients[0][2]
self.assertIsInstance(factory, ProxyClientFactory)
self.assertEqual(factory.rest, expectedURI)
self.assertEqual(factory.headers[b"host"], b"127.0.0.1:1234")
Expand All @@ -62,6 +61,15 @@ def test_render(self):
return self._testRender(b"/index", b"/path")


def test_render_subpage(self):
"""
Test that L{ReverseProxyResource.render} will instantiate a child
resource that will initiate a connection to the given server
requesting the apropiate url subpath.
"""
return self._testRender(b"/index/page1", b"/path/page1")


def test_renderWithQuery(self):
"""
Test that L{ReverseProxyResource.render} passes query parameters to the
Expand Down

0 comments on commit 9a7ce38

Please sign in to comment.