<s> """<STR_LIT>""" <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . test . proto_helpers import StringTransportWithDisconnection <EOL> from twisted . test . proto_helpers import MemoryReactor <EOL> from twisted . web . resource import Resource <EOL> from twisted . web . server import Site <EOL> from twisted . web . proxy import ReverseProxyResource , ProxyClientFactory <EOL> from twisted . web . proxy import ProxyClient , ProxyRequest , ReverseProxyRequest <EOL> from twisted . web . test . test_web import DummyRequest <EOL> class ReverseProxyResourceTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def _testRender ( self , uri , expectedURI ) : <EOL> """<STR_LIT>""" <EOL> root = Resource ( ) <EOL> reactor = MemoryReactor ( ) <EOL> resource = ReverseProxyResource ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> , "<STR_LIT>" , reactor ) <EOL> root . putChild ( '<STR_LIT:index>' , resource ) <EOL> site = Site ( root ) <EOL> transport = StringTransportWithDisconnection ( ) <EOL> channel = site . buildProtocol ( None ) <EOL> channel . makeConnection ( transport ) <EOL> self . addCleanup ( channel . connectionLost , None ) <EOL> channel . dataReceived ( "<STR_LIT>" % <EOL> ( uri , ) ) <EOL> self . assertEqual ( len ( reactor . tcpClients ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , "<STR_LIT:127.0.0.1>" ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> factory = reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> self . assertIsInstance ( factory , ProxyClientFactory ) <EOL> self . assertEqual ( factory . rest , expectedURI ) <EOL> self . assertEqual ( factory . headers [ "<STR_LIT:host>" ] , "<STR_LIT>" ) <EOL> def test_render ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testRender ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_renderWithQuery ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testRender ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_getChild ( self ) : <EOL> """<STR_LIT>""" <EOL> reactor = MemoryReactor ( ) <EOL> resource = ReverseProxyResource ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> , "<STR_LIT>" , reactor ) <EOL> child = resource . getChild ( '<STR_LIT:foo>' , None ) <EOL> self . assertIsInstance ( child , ReverseProxyResource ) <EOL> self . assertEqual ( child . path , "<STR_LIT>" ) <EOL> self . assertEqual ( child . port , <NUM_LIT> ) <EOL> self . assertEqual ( child . host , "<STR_LIT:127.0.0.1>" ) <EOL> self . assertIdentical ( child . reactor , resource . reactor ) <EOL> def test_getChildWithSpecial ( self ) : <EOL> """<STR_LIT>""" <EOL> resource = ReverseProxyResource ( "<STR_LIT:127.0.0.1>" , <NUM_LIT> , "<STR_LIT>" ) <EOL> child = resource . getChild ( '<STR_LIT>' , None ) <EOL> self . assertEqual ( child . path , "<STR_LIT>" ) <EOL> class DummyChannel ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> self . transport = transport <EOL> self . lostReason = None <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> self . lostReason = reason <EOL> class ProxyClientTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def _parseOutHeaders ( self , content ) : <EOL> """<STR_LIT>""" <EOL> headers , body = content . split ( '<STR_LIT>' ) <EOL> headers = headers . split ( '<STR_LIT:\r\n>' ) <EOL> requestLine = headers . pop ( <NUM_LIT:0> ) <EOL> return ( <EOL> requestLine , dict ( header . split ( '<STR_LIT>' ) for header in headers ) , body ) <EOL> def makeRequest ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return DummyRequest ( path ) <EOL> def makeProxyClient ( self , request , method = "<STR_LIT:GET>" , headers = None , <EOL> requestBody = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } <EOL> path = '<STR_LIT:/>' + request . postpath <EOL> return ProxyClient ( <EOL> method , path , '<STR_LIT>' , headers , requestBody , request ) <EOL> def connectProxy ( self , proxyClient ) : <EOL> """<STR_LIT>""" <EOL> clientTransport = StringTransportWithDisconnection ( ) <EOL> clientTransport . protocol = proxyClient <EOL> proxyClient . makeConnection ( clientTransport ) <EOL> return clientTransport <EOL> def assertForwardsHeaders ( self , proxyClient , requestLine , headers ) : <EOL> """<STR_LIT>""" <EOL> self . connectProxy ( proxyClient ) <EOL> requestContent = proxyClient . transport . value ( ) <EOL> receivedLine , receivedHeaders , body = self . _parseOutHeaders ( <EOL> requestContent ) <EOL> self . assertEqual ( receivedLine , requestLine ) <EOL> self . assertEqual ( receivedHeaders , headers ) <EOL> return body <EOL> def makeResponseBytes ( self , code , message , headers , body ) : <EOL> lines = [ "<STR_LIT>" % ( code , message ) ] <EOL> for header , values in headers : <EOL> for value in values : <EOL> lines . append ( "<STR_LIT>" % ( header , value ) ) <EOL> lines . extend ( [ '<STR_LIT>' , body ] ) <EOL> return '<STR_LIT:\r\n>' . join ( lines ) <EOL> def assertForwardsResponse ( self , request , code , message , headers , body ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( request . responseCode , code ) <EOL> self . assertEqual ( request . responseMessage , message ) <EOL> receivedHeaders = list ( request . responseHeaders . getAllRawHeaders ( ) ) <EOL> receivedHeaders . sort ( ) <EOL> expectedHeaders = headers [ : ] <EOL> expectedHeaders . sort ( ) <EOL> self . assertEqual ( receivedHeaders , expectedHeaders ) <EOL> self . assertEqual ( '<STR_LIT>' . join ( request . written ) , body ) <EOL> def _testDataForward ( self , code , message , headers , body , method = "<STR_LIT:GET>" , <EOL> requestBody = "<STR_LIT>" , loseConnection = True ) : <EOL> """<STR_LIT>""" <EOL> request = self . makeRequest ( '<STR_LIT:foo>' ) <EOL> client = self . makeProxyClient ( <EOL> request , method , { '<STR_LIT>' : '<STR_LIT>' } , requestBody ) <EOL> receivedBody = self . assertForwardsHeaders ( <EOL> client , '<STR_LIT>' % ( method , ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( receivedBody , requestBody ) <EOL> client . dataReceived ( <EOL> self . makeResponseBytes ( code , message , headers , body ) ) <EOL> self . assertForwardsResponse ( request , code , message , headers , body ) <EOL> if loseConnection : <EOL> client . transport . loseConnection ( ) <EOL> self . assertFalse ( client . transport . connected ) <EOL> self . assertEqual ( request . finished , <NUM_LIT:1> ) <EOL> def test_forward ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testDataForward ( <EOL> <NUM_LIT:200> , "<STR_LIT:OK>" , [ ( "<STR_LIT>" , [ "<STR_LIT:bar>" , "<STR_LIT>" ] ) ] , "<STR_LIT>" ) <EOL> def test_postData ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testDataForward ( <EOL> <NUM_LIT:200> , "<STR_LIT:OK>" , [ ( "<STR_LIT>" , [ "<STR_LIT:bar>" ] ) ] , "<STR_LIT>" , "<STR_LIT:POST>" , "<STR_LIT>" ) <EOL> def test_statusWithMessage ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testDataForward ( <EOL> <NUM_LIT> , "<STR_LIT>" , [ ] , "<STR_LIT>" ) <EOL> def test_contentLength ( self ) : <EOL> """<STR_LIT>""" <EOL> data = "<STR_LIT>" <EOL> return self . _testDataForward ( <EOL> <NUM_LIT:200> , "<STR_LIT:OK>" , [ ( "<STR_LIT>" , [ str ( len ( data ) ) ] ) ] , data ) <EOL> def test_losesConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> data = "<STR_LIT>" <EOL> return self . _testDataForward ( <EOL> <NUM_LIT:200> , "<STR_LIT:OK>" , [ ( "<STR_LIT>" , [ str ( len ( data ) ) ] ) ] , data , <EOL> loseConnection = False ) <EOL> def test_headersCleanups ( self ) : <EOL> """<STR_LIT>""" <EOL> client = ProxyClient ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT:foo>" } , '<STR_LIT>' , None ) <EOL> self . assertEqual ( client . headers , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_keepaliveNotForwarded ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> expectedHeaders = headers . copy ( ) <EOL> expectedHeaders [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> del expectedHeaders [ '<STR_LIT>' ] <EOL> client = ProxyClient ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , headers , '<STR_LIT>' , None ) <EOL> self . assertForwardsHeaders ( <EOL> client , '<STR_LIT>' , expectedHeaders ) <EOL> def test_defaultHeadersOverridden ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . makeRequest ( '<STR_LIT:foo>' ) <EOL> request . responseHeaders . setRawHeaders ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> request . responseHeaders . setRawHeaders ( '<STR_LIT:date>' , [ '<STR_LIT>' ] ) <EOL> request . responseHeaders . setRawHeaders ( '<STR_LIT>' , [ "<STR_LIT>" ] ) <EOL> client = self . makeProxyClient ( request , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . connectProxy ( client ) <EOL> headers = { <EOL> '<STR_LIT>' : [ '<STR_LIT:bar>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT:Content-Type>' : [ '<STR_LIT>' ] , <EOL> } <EOL> client . dataReceived ( <EOL> self . makeResponseBytes ( <NUM_LIT:200> , "<STR_LIT:OK>" , headers . items ( ) , '<STR_LIT>' ) ) <EOL> self . assertForwardsResponse ( <EOL> request , <NUM_LIT:200> , '<STR_LIT:OK>' , headers . items ( ) , '<STR_LIT>' ) <EOL> class ProxyClientFactoryTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_connectionFailed ( self ) : <EOL> """<STR_LIT>""" <EOL> request = DummyRequest ( [ '<STR_LIT:foo>' ] ) <EOL> factory = ProxyClientFactory ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> { "<STR_LIT>" : "<STR_LIT>" } , '<STR_LIT>' , request ) <EOL> factory . clientConnectionFailed ( None , None ) <EOL> self . assertEqual ( request . responseCode , <NUM_LIT> ) <EOL> self . assertEqual ( request . responseMessage , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( request . responseHeaders . getAllRawHeaders ( ) ) , <EOL> [ ( "<STR_LIT:Content-Type>" , [ "<STR_LIT>" ] ) ] ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' . join ( request . written ) , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( request . finished , <NUM_LIT:1> ) <EOL> def test_buildProtocol ( self ) : <EOL> """<STR_LIT>""" <EOL> factory = ProxyClientFactory ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> { "<STR_LIT>" : "<STR_LIT>" } , '<STR_LIT>' , <EOL> None ) <EOL> proto = factory . buildProtocol ( None ) <EOL> self . assertIsInstance ( proto , ProxyClient ) <EOL> self . assertEqual ( proto . command , '<STR_LIT:GET>' ) <EOL> self . assertEqual ( proto . rest , '<STR_LIT>' ) <EOL> self . assertEqual ( proto . data , '<STR_LIT>' ) <EOL> self . assertEqual ( proto . headers , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> class ProxyRequestTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def _testProcess ( self , uri , expectedURI , method = "<STR_LIT:GET>" , data = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransportWithDisconnection ( ) <EOL> channel = DummyChannel ( transport ) <EOL> reactor = MemoryReactor ( ) <EOL> request = ProxyRequest ( channel , False , reactor ) <EOL> request . gotLength ( len ( data ) ) <EOL> request . handleContentChunk ( data ) <EOL> request . requestReceived ( method , '<STR_LIT>' % ( uri , ) , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( len ( reactor . tcpClients ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> factory = reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> self . assertIsInstance ( factory , ProxyClientFactory ) <EOL> self . assertEqual ( factory . command , method ) <EOL> self . assertEqual ( factory . version , '<STR_LIT>' ) <EOL> self . assertEqual ( factory . headers , { '<STR_LIT:host>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( factory . data , data ) <EOL> self . assertEqual ( factory . rest , expectedURI ) <EOL> self . assertEqual ( factory . father , request ) <EOL> def test_process ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testProcess ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_processWithoutTrailingSlash ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testProcess ( "<STR_LIT>" , "<STR_LIT:/>" ) <EOL> def test_processWithData ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _testProcess ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:POST>" , "<STR_LIT>" ) <EOL> def test_processWithPort ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransportWithDisconnection ( ) <EOL> channel = DummyChannel ( transport ) <EOL> reactor = MemoryReactor ( ) <EOL> request = ProxyRequest ( channel , False , reactor ) <EOL> request . gotLength ( <NUM_LIT:0> ) <EOL> request . requestReceived ( '<STR_LIT:GET>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( len ( reactor . tcpClients ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> class DummyFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port ) : <EOL> self . host = host <EOL> self . port = port <EOL> class ReverseProxyRequestTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_process ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransportWithDisconnection ( ) <EOL> channel = DummyChannel ( transport ) <EOL> reactor = MemoryReactor ( ) <EOL> request = ReverseProxyRequest ( channel , False , reactor ) <EOL> request . factory = DummyFactory ( "<STR_LIT>" , <NUM_LIT> ) <EOL> request . gotLength ( <NUM_LIT:0> ) <EOL> request . requestReceived ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( reactor . tcpClients ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <NUM_LIT> ) <EOL> factory = reactor . tcpClients [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> self . assertIsInstance ( factory , ProxyClientFactory ) <EOL> self . assertEqual ( factory . headers , { '<STR_LIT:host>' : '<STR_LIT>' } ) </s>
