<s> """<STR_LIT>""" <EOL> __metaclass__ = type <EOL> from zope . interface import implements <EOL> from zope . interface . verify import verifyObject <EOL> from twisted . python import log <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet . interfaces import IConsumer , IPushProducer <EOL> from twisted . internet . error import ConnectionDone , ConnectionLost <EOL> from twisted . internet . defer import Deferred , succeed , fail , CancelledError <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . trial . unittest import TestCase <EOL> from twisted . test . proto_helpers import StringTransport , AccumulatingProtocol <EOL> from twisted . web . _newclient import UNKNOWN_LENGTH , STATUS , HEADER , BODY , DONE <EOL> from twisted . web . _newclient import Request , Response , HTTPParser , HTTPClientParser <EOL> from twisted . web . _newclient import BadResponseVersion , ParseError , HTTP11ClientProtocol <EOL> from twisted . web . _newclient import ChunkedEncoder , RequestGenerationFailed <EOL> from twisted . web . _newclient import RequestTransmissionFailed , ResponseFailed <EOL> from twisted . web . _newclient import WrongBodyLength , RequestNotSent <EOL> from twisted . web . _newclient import ConnectionAborted , ResponseNeverReceived <EOL> from twisted . web . _newclient import BadHeaders , ResponseDone , PotentialDataLoss , ExcessWrite <EOL> from twisted . web . _newclient import TransportProxyProducer , LengthEnforcingConsumer , makeStatefulDispatcher <EOL> from twisted . web . http_headers import Headers <EOL> from twisted . web . http import _DataLoss <EOL> from twisted . web . iweb import IBodyProducer , IResponse <EOL> class StringTransport ( StringTransport ) : <EOL> """<STR_LIT>""" <EOL> aborting = False <EOL> def abortConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> self . aborting = True <EOL> self . loseConnection ( ) <EOL> class ArbitraryException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class AnotherArbitraryException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> _boringHeaders = Headers ( { '<STR_LIT:host>' : [ '<STR_LIT>' ] } ) <EOL> def assertWrapperExceptionTypes ( self , deferred , mainType , reasonTypes ) : <EOL> """<STR_LIT>""" <EOL> def cbFailed ( err ) : <EOL> for reason , type in zip ( err . reasons , reasonTypes ) : <EOL> reason . trap ( type ) <EOL> self . assertEqual ( len ( err . reasons ) , len ( reasonTypes ) , <EOL> "<STR_LIT>" % ( err . reasons , reasonTypes ) ) <EOL> return err <EOL> d = self . assertFailure ( deferred , mainType ) <EOL> d . addCallback ( cbFailed ) <EOL> return d <EOL> def assertResponseFailed ( self , deferred , reasonTypes ) : <EOL> """<STR_LIT>""" <EOL> return assertWrapperExceptionTypes ( self , deferred , ResponseFailed , reasonTypes ) <EOL> def assertRequestGenerationFailed ( self , deferred , reasonTypes ) : <EOL> """<STR_LIT>""" <EOL> return assertWrapperExceptionTypes ( self , deferred , RequestGenerationFailed , reasonTypes ) <EOL> def assertRequestTransmissionFailed ( self , deferred , reasonTypes ) : <EOL> """<STR_LIT>""" <EOL> return assertWrapperExceptionTypes ( self , deferred , RequestTransmissionFailed , reasonTypes ) <EOL> def justTransportResponse ( transport ) : <EOL> """<STR_LIT>""" <EOL> return Response ( ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) , <NUM_LIT:200> , '<STR_LIT:OK>' , _boringHeaders , transport ) <EOL> class MakeStatefulDispatcherTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_functionCalledByState ( self ) : <EOL> """<STR_LIT>""" <EOL> class Foo : <EOL> _state = '<STR_LIT:A>' <EOL> def bar ( self ) : <EOL> pass <EOL> bar = makeStatefulDispatcher ( '<STR_LIT>' , bar ) <EOL> def _quux_A ( self ) : <EOL> return '<STR_LIT:a>' <EOL> def _quux_B ( self ) : <EOL> return '<STR_LIT:b>' <EOL> stateful = Foo ( ) <EOL> self . assertEqual ( stateful . bar ( ) , '<STR_LIT:a>' ) <EOL> stateful . _state = '<STR_LIT:B>' <EOL> self . assertEqual ( stateful . bar ( ) , '<STR_LIT:b>' ) <EOL> stateful . _state = '<STR_LIT:C>' <EOL> self . assertRaises ( RuntimeError , stateful . bar ) <EOL> class _HTTPParserTests ( object ) : <EOL> """<STR_LIT>""" <EOL> sep = None <EOL> def test_statusCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> status = [ ] <EOL> protocol = HTTPParser ( ) <EOL> protocol . statusReceived = status . append <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> self . assertEqual ( protocol . state , STATUS ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> self . assertEqual ( status , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( protocol . state , HEADER ) <EOL> def _headerTestSetup ( self ) : <EOL> header = { } <EOL> protocol = HTTPParser ( ) <EOL> protocol . headerReceived = header . __setitem__ <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> return header , protocol <EOL> def test_headerCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> header , protocol = self . _headerTestSetup ( ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( self . sep ) <EOL> self . assertEqual ( header , { '<STR_LIT>' : '<STR_LIT:bar>' } ) <EOL> self . assertEqual ( protocol . state , BODY ) <EOL> def test_continuedHeaderCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> header , protocol = self . _headerTestSetup ( ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( self . sep ) <EOL> self . assertEqual ( header , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( protocol . state , BODY ) <EOL> def test_fieldContentWhitespace ( self ) : <EOL> """<STR_LIT>""" <EOL> header , protocol = self . _headerTestSetup ( ) <EOL> value = '<STR_LIT>' % dict ( sep = self . sep ) <EOL> protocol . dataReceived ( '<STR_LIT>' + value ) <EOL> protocol . dataReceived ( '<STR_LIT>' + value ) <EOL> protocol . dataReceived ( self . sep ) <EOL> self . assertEqual ( header , { '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' } ) <EOL> def test_allHeadersCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> called = [ ] <EOL> header , protocol = self . _headerTestSetup ( ) <EOL> def allHeadersReceived ( ) : <EOL> called . append ( protocol . state ) <EOL> protocol . state = STATUS <EOL> protocol . allHeadersReceived = allHeadersReceived <EOL> protocol . dataReceived ( self . sep ) <EOL> self . assertEqual ( called , [ HEADER ] ) <EOL> self . assertEqual ( protocol . state , STATUS ) <EOL> def test_noHeaderCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> header , protocol = self . _headerTestSetup ( ) <EOL> protocol . dataReceived ( self . sep ) <EOL> self . assertEqual ( header , { } ) <EOL> self . assertEqual ( protocol . state , BODY ) <EOL> def test_headersSavedOnResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPParser ( ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( '<STR_LIT>' + self . sep ) <EOL> protocol . dataReceived ( self . sep ) <EOL> expected = [ ( '<STR_LIT>' , [ '<STR_LIT:bar>' , '<STR_LIT>' ] ) ] <EOL> self . assertEqual ( expected , list ( protocol . headers . getAllRawHeaders ( ) ) ) <EOL> def test_connectionControlHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPParser ( ) <EOL> connHeaderNames = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for header in connHeaderNames : <EOL> self . assertTrue ( <EOL> protocol . isConnectionControlHeader ( header ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( header , ) ) <EOL> self . assertFalse ( <EOL> protocol . isConnectionControlHeader ( "<STR_LIT:date>" ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_switchToBodyMode ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPParser ( ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . switchToBodyMode ( object ( ) ) <EOL> self . assertRaises ( RuntimeError , protocol . switchToBodyMode , object ( ) ) <EOL> class HTTPParserRFCComplaintDelimeterTests ( _HTTPParserTests , TestCase ) : <EOL> """<STR_LIT>""" <EOL> sep = '<STR_LIT:\r\n>' <EOL> class HTTPParserNonRFCComplaintDelimeterTests ( _HTTPParserTests , TestCase ) : <EOL> """<STR_LIT>""" <EOL> sep = '<STR_LIT:\n>' <EOL> class HTTPClientParserTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_parseVersion ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( None , None ) <EOL> self . assertEqual ( <EOL> protocol . parseVersion ( '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:7> , <NUM_LIT:2> ) ) <EOL> def test_parseBadVersion ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( None , None ) <EOL> e = BadResponseVersion <EOL> f = protocol . parseVersion <EOL> def checkParsing ( s ) : <EOL> exc = self . assertRaises ( e , f , s ) <EOL> self . assertEqual ( exc . data , s ) <EOL> checkParsing ( '<STR_LIT:foo>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> def test_responseStatusParsing ( self ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) <EOL> protocol = HTTPClientParser ( request , None ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( protocol . response . version , ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( protocol . response . code , <NUM_LIT:200> ) <EOL> self . assertEqual ( protocol . response . phrase , '<STR_LIT:OK>' ) <EOL> def test_badResponseStatus ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( None , None ) <EOL> def checkParsing ( s ) : <EOL> exc = self . assertRaises ( ParseError , protocol . statusReceived , s ) <EOL> self . assertEqual ( exc . data , s ) <EOL> checkParsing ( '<STR_LIT:foo>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> checkParsing ( '<STR_LIT>' ) <EOL> def _noBodyTest ( self , request , status , response ) : <EOL> """<STR_LIT>""" <EOL> header = { } <EOL> finished = [ ] <EOL> body = [ ] <EOL> bodyDataFinished = [ ] <EOL> protocol = HTTPClientParser ( request , finished . append ) <EOL> protocol . headerReceived = header . __setitem__ <EOL> transport = StringTransport ( ) <EOL> protocol . makeConnection ( transport ) <EOL> protocol . dataReceived ( status ) <EOL> protocol . response . _bodyDataReceived = body . append <EOL> protocol . response . _bodyDataFinished = ( <EOL> lambda : bodyDataFinished . append ( True ) ) <EOL> protocol . dataReceived ( response ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> self . assertEqual ( protocol . state , DONE ) <EOL> self . assertEqual ( body , [ ] ) <EOL> self . assertEqual ( finished , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( bodyDataFinished , [ True ] ) <EOL> self . assertEqual ( protocol . response . length , <NUM_LIT:0> ) <EOL> return header <EOL> def test_headResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( '<STR_LIT>' , '<STR_LIT:/>' , _boringHeaders , None ) <EOL> status = '<STR_LIT>' <EOL> response = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' ) <EOL> header = self . _noBodyTest ( request , status , response ) <EOL> self . assertEqual ( header , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_noContentResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) <EOL> status = '<STR_LIT>' <EOL> response = '<STR_LIT:\r\n>' <EOL> self . _noBodyTest ( request , status , response ) <EOL> def test_notModifiedResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) <EOL> status = '<STR_LIT>' <EOL> response = '<STR_LIT:\r\n>' <EOL> self . _noBodyTest ( request , status , response ) <EOL> def test_responseHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> lambda rest : None ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( <EOL> protocol . connHeaders , <EOL> Headers ( { } ) ) <EOL> self . assertEqual ( <EOL> protocol . response . headers , <EOL> Headers ( { '<STR_LIT>' : [ '<STR_LIT:bar>' ] } ) ) <EOL> self . assertIdentical ( protocol . response . length , UNKNOWN_LENGTH ) <EOL> def test_connectionHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> lambda rest : None ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( <EOL> protocol . response . headers , <EOL> Headers ( { } ) ) <EOL> self . assertEqual ( <EOL> protocol . connHeaders , <EOL> Headers ( { '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> self . assertEqual ( protocol . response . length , <NUM_LIT> ) <EOL> def test_headResponseContentLengthEntityHeader ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> lambda rest : None ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( <EOL> protocol . response . headers , <EOL> Headers ( { '<STR_LIT>' : [ '<STR_LIT>' ] } ) ) <EOL> self . assertEqual ( <EOL> protocol . connHeaders , <EOL> Headers ( { } ) ) <EOL> self . assertEqual ( protocol . response . length , <NUM_LIT:0> ) <EOL> def test_contentLength ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> finished . append ) <EOL> transport = StringTransport ( ) <EOL> protocol . makeConnection ( transport ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> body = [ ] <EOL> protocol . response . _bodyDataReceived = body . append <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> self . assertEqual ( protocol . state , BODY ) <EOL> protocol . dataReceived ( '<STR_LIT:x>' * <NUM_LIT:6> ) <EOL> self . assertEqual ( body , [ '<STR_LIT:x>' * <NUM_LIT:6> ] ) <EOL> self . assertEqual ( protocol . state , BODY ) <EOL> protocol . dataReceived ( '<STR_LIT:y>' * <NUM_LIT:4> ) <EOL> self . assertEqual ( body , [ '<STR_LIT:x>' * <NUM_LIT:6> , '<STR_LIT:y>' * <NUM_LIT:4> ] ) <EOL> self . assertEqual ( protocol . state , DONE ) <EOL> self . assertTrue ( finished , [ '<STR_LIT>' ] ) <EOL> def test_zeroContentLength ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> finished . append ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> body = [ ] <EOL> protocol . response . _bodyDataReceived = body . append <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( protocol . state , DONE ) <EOL> self . assertEqual ( body , [ ] ) <EOL> self . assertTrue ( finished , [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( protocol . response . length , <NUM_LIT:0> ) <EOL> def test_multipleContentLengthHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> None ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> self . assertRaises ( <EOL> ValueError , <EOL> protocol . dataReceived , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' ) <EOL> def test_extraBytesPassedBack ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> finished . append ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( protocol . state , DONE ) <EOL> self . assertEqual ( finished , [ '<STR_LIT>' ] ) <EOL> def test_extraBytesPassedBackHEAD ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> finished . append ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( protocol . state , DONE ) <EOL> self . assertEqual ( finished , [ '<STR_LIT>' ] ) <EOL> def test_chunkedResponseBody ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> finished . append ) <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> body = [ ] <EOL> protocol . response . _bodyDataReceived = body . append <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( body , [ ] ) <EOL> self . assertIdentical ( protocol . response . length , UNKNOWN_LENGTH ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( body , [ '<STR_LIT:a>' ] ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( body , [ '<STR_LIT:a>' , '<STR_LIT>' ] ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( finished , [ '<STR_LIT>' ] ) <EOL> def test_unknownContentLength ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , finished . append ) <EOL> transport = StringTransport ( ) <EOL> protocol . makeConnection ( transport ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> body = [ ] <EOL> protocol . response . _bodyDataReceived = body . append <EOL> protocol . dataReceived ( '<STR_LIT:\r\n>' ) <EOL> protocol . dataReceived ( '<STR_LIT:foo>' ) <EOL> protocol . dataReceived ( '<STR_LIT:bar>' ) <EOL> self . assertEqual ( body , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> protocol . connectionLost ( ConnectionDone ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( finished , [ '<STR_LIT>' ] ) <EOL> def test_contentLengthAndTransferEncoding ( self ) : <EOL> """<STR_LIT>""" <EOL> finished = [ ] <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , finished . append ) <EOL> transport = StringTransport ( ) <EOL> protocol . makeConnection ( transport ) <EOL> protocol . dataReceived ( '<STR_LIT>' ) <EOL> body = [ ] <EOL> protocol . response . _bodyDataReceived = body . append <EOL> protocol . dataReceived ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( body , [ '<STR_LIT:abc>' ] ) <EOL> self . assertEqual ( finished , [ '<STR_LIT>' ] ) <EOL> def test_connectionLostBeforeBody ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTPClientParser ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , None ) <EOL> protocol . makeConnection ( transport ) <EOL> responseDeferred = protocol . _responseDeferred <EOL> protocol . connectionLost ( Failure ( ArbitraryException ( ) ) ) <EOL> return assertResponseFailed ( <EOL> self , responseDeferred , [ ArbitraryException ] ) <EOL> def test_connectionLostWithError ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTPClientParser ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> None ) <EOL> protocol . makeConnection ( transport ) <EOL> response = [ ] <EOL> protocol . _responseDeferred . addCallback ( response . append ) <EOL> protocol . dataReceived ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' ) <EOL> response = response [ <NUM_LIT:0> ] <EOL> def fakeBodyDataFinished ( err = None ) : <EOL> raise ArbitraryException ( ) <EOL> response . _bodyDataFinished = fakeBodyDataFinished <EOL> protocol . connectionLost ( None ) <EOL> self . assertEqual ( len ( self . flushLoggedErrors ( ArbitraryException ) ) , <NUM_LIT:1> ) <EOL> def test_noResponseAtAll ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> lambda ign : None ) <EOL> d = protocol . _responseDeferred <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . connectionLost ( ConnectionLost ( ) ) <EOL> return self . assertFailure ( d , ResponseNeverReceived ) <EOL> def test_someResponseButNotAll ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = HTTPClientParser ( <EOL> Request ( '<STR_LIT>' , '<STR_LIT:/>' , _boringHeaders , None ) , <EOL> lambda ign : None ) <EOL> d = protocol . _responseDeferred <EOL> protocol . makeConnection ( StringTransport ( ) ) <EOL> protocol . dataReceived ( '<STR_LIT:2>' ) <EOL> protocol . connectionLost ( ConnectionLost ( ) ) <EOL> return self . assertFailure ( d , ResponseFailed ) . addCallback ( <EOL> self . assertIsInstance , ResponseFailed ) <EOL> class SlowRequest : <EOL> """<STR_LIT>""" <EOL> method = '<STR_LIT:GET>' <EOL> stopped = False <EOL> persistent = False <EOL> def writeTo ( self , transport ) : <EOL> self . finished = Deferred ( ) <EOL> return self . finished <EOL> def stopWriting ( self ) : <EOL> self . stopped = True <EOL> class SimpleRequest : <EOL> """<STR_LIT>""" <EOL> persistent = False <EOL> def writeTo ( self , transport ) : <EOL> transport . write ( '<STR_LIT>' ) <EOL> return succeed ( None ) <EOL> class HTTP11ClientProtocolTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . transport = StringTransport ( ) <EOL> self . protocol = HTTP11ClientProtocol ( ) <EOL> self . protocol . makeConnection ( self . transport ) <EOL> def test_request ( self ) : <EOL> """<STR_LIT>""" <EOL> self . protocol . request ( SimpleRequest ( ) ) <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT>' ) <EOL> def test_secondRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> self . protocol . request ( SlowRequest ( ) ) <EOL> def cbNotSent ( ignored ) : <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT>' ) <EOL> d = self . assertFailure ( <EOL> self . protocol . request ( SimpleRequest ( ) ) , RequestNotSent ) <EOL> d . addCallback ( cbNotSent ) <EOL> return d <EOL> def test_requestAfterConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> self . protocol . connectionLost ( <EOL> Failure ( ConnectionDone ( "<STR_LIT>" ) ) ) <EOL> def cbNotSent ( ignored ) : <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT>' ) <EOL> d = self . assertFailure ( <EOL> self . protocol . request ( SimpleRequest ( ) ) , RequestNotSent ) <EOL> d . addCallback ( cbNotSent ) <EOL> return d <EOL> def test_failedWriteTo ( self ) : <EOL> """<STR_LIT>""" <EOL> class BrokenRequest : <EOL> persistent = False <EOL> def writeTo ( self , transport ) : <EOL> return fail ( ArbitraryException ( ) ) <EOL> d = self . protocol . request ( BrokenRequest ( ) ) <EOL> def cbFailed ( ignored ) : <EOL> self . assertTrue ( self . transport . disconnecting ) <EOL> self . protocol . connectionLost ( <EOL> Failure ( ConnectionDone ( "<STR_LIT>" ) ) ) <EOL> d = assertRequestGenerationFailed ( self , d , [ ArbitraryException ] ) <EOL> d . addCallback ( cbFailed ) <EOL> return d <EOL> def test_synchronousWriteToError ( self ) : <EOL> """<STR_LIT>""" <EOL> class BrokenRequest : <EOL> persistent = False <EOL> def writeTo ( self , transport ) : <EOL> raise ArbitraryException ( ) <EOL> d = self . protocol . request ( BrokenRequest ( ) ) <EOL> return assertRequestGenerationFailed ( self , d , [ ArbitraryException ] ) <EOL> def test_connectionLostDuringRequestGeneration ( self , mode = None ) : <EOL> """<STR_LIT>""" <EOL> request = SlowRequest ( ) <EOL> d = self . protocol . request ( request ) <EOL> d = assertRequestTransmissionFailed ( self , d , [ ArbitraryException ] ) <EOL> self . assertFalse ( request . stopped ) <EOL> self . protocol . connectionLost ( Failure ( ArbitraryException ( ) ) ) <EOL> self . assertTrue ( request . stopped ) <EOL> if mode == '<STR_LIT>' : <EOL> request . finished . callback ( None ) <EOL> elif mode == '<STR_LIT>' : <EOL> request . finished . errback ( Failure ( AnotherArbitraryException ( ) ) ) <EOL> errors = self . flushLoggedErrors ( AnotherArbitraryException ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> else : <EOL> pass <EOL> return d <EOL> def test_connectionLostBeforeGenerationFinished ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . test_connectionLostDuringRequestGeneration ( '<STR_LIT>' ) <EOL> def test_connectionLostBeforeGenerationFailed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . test_connectionLostDuringRequestGeneration ( '<STR_LIT>' ) <EOL> def test_errorMessageOnConnectionLostBeforeGenerationFailedDoesNotConfuse ( self ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> log . addObserver ( errors . append ) <EOL> self . addCleanup ( log . removeObserver , errors . append ) <EOL> def check ( ignore ) : <EOL> error = errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( error [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return self . test_connectionLostDuringRequestGeneration ( <EOL> '<STR_LIT>' ) . addCallback ( check ) <EOL> def test_receiveSimplestResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> def cbRequest ( response ) : <EOL> self . assertEqual ( response . code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . headers , Headers ( ) ) <EOL> self . assertTrue ( self . transport . disconnecting ) <EOL> self . assertEqual ( self . protocol . state , '<STR_LIT>' ) <EOL> d . addCallback ( cbRequest ) <EOL> self . protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> return d <EOL> def test_receiveResponseHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> def cbRequest ( response ) : <EOL> expected = Headers ( { '<STR_LIT>' : [ '<STR_LIT:bar>' , '<STR_LIT>' ] } ) <EOL> self . assertEqual ( response . headers , expected ) <EOL> d . addCallback ( cbRequest ) <EOL> self . protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> return d <EOL> def test_receiveResponseBeforeRequestGenerationDone ( self ) : <EOL> """<STR_LIT>""" <EOL> quiescentResult = [ ] <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( quiescentResult . append ) <EOL> protocol . makeConnection ( transport ) <EOL> request = SlowRequest ( ) <EOL> d = protocol . request ( request ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> "<STR_LIT>" ) <EOL> def cbResponse ( response ) : <EOL> p = AccumulatingProtocol ( ) <EOL> whenFinished = p . closedDeferred = Deferred ( ) <EOL> response . deliverBody ( p ) <EOL> self . assertEqual ( <EOL> protocol . state , '<STR_LIT>' ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> self . assertEqual ( quiescentResult , [ ] ) <EOL> return whenFinished . addCallback ( <EOL> lambda ign : ( response , p . data ) ) <EOL> d . addCallback ( cbResponse ) <EOL> def cbAllResponse ( ( response , body ) ) : <EOL> self . assertEqual ( response . version , ( '<STR_LIT>' , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . assertEqual ( response . code , <NUM_LIT:200> ) <EOL> self . assertEqual ( response . phrase , '<STR_LIT:OK>' ) <EOL> self . assertEqual ( response . headers , Headers ( { '<STR_LIT>' : [ '<STR_LIT:bar>' ] } ) ) <EOL> self . assertEqual ( body , "<STR_LIT>" ) <EOL> request . finished . callback ( None ) <EOL> d . addCallback ( cbAllResponse ) <EOL> return d <EOL> def test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone ( self ) : <EOL> """<STR_LIT>""" <EOL> request = SlowRequest ( ) <EOL> d = self . protocol . request ( request ) <EOL> self . protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> "<STR_LIT>" ) <EOL> def cbResponse ( response ) : <EOL> p = AccumulatingProtocol ( ) <EOL> whenFinished = p . closedDeferred = Deferred ( ) <EOL> response . deliverBody ( p ) <EOL> return whenFinished . addCallback ( <EOL> lambda ign : ( response , p . data ) ) <EOL> d . addCallback ( cbResponse ) <EOL> def cbAllResponse ( ignore ) : <EOL> request . finished . callback ( None ) <EOL> self . protocol . connectionLost ( Failure ( ArbitraryException ( ) ) ) <EOL> self . assertEqual ( self . protocol . _state , '<STR_LIT>' ) <EOL> d . addCallback ( cbAllResponse ) <EOL> return d <EOL> def test_receiveResponseBody ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = AccumulatingProtocol ( ) <EOL> whenFinished = protocol . closedDeferred = Deferred ( ) <EOL> requestDeferred = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> self . protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r>" ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> self . assertEqual ( result , [ ] ) <EOL> self . protocol . dataReceived ( "<STR_LIT:\n>" ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> response . deliverBody ( protocol ) <EOL> self . protocol . dataReceived ( "<STR_LIT:foo>" ) <EOL> self . protocol . dataReceived ( "<STR_LIT:bar>" ) <EOL> def cbAllResponse ( ignored ) : <EOL> self . assertEqual ( protocol . data , "<STR_LIT>" ) <EOL> protocol . closedReason . trap ( ResponseDone ) <EOL> whenFinished . addCallback ( cbAllResponse ) <EOL> return whenFinished <EOL> def test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknown ( <EOL> self ) : <EOL> """<STR_LIT>""" <EOL> requestDeferred = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> self . protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> protocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( protocol ) <EOL> self . protocol . dataReceived ( "<STR_LIT:foo>" ) <EOL> self . protocol . dataReceived ( "<STR_LIT:bar>" ) <EOL> self . assertEqual ( protocol . data , "<STR_LIT>" ) <EOL> self . protocol . connectionLost ( <EOL> Failure ( ConnectionDone ( "<STR_LIT>" ) ) ) <EOL> protocol . closedReason . trap ( PotentialDataLoss ) <EOL> def test_chunkedResponseBodyUnfinishedWhenConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> requestDeferred = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> self . protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> protocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( protocol ) <EOL> self . protocol . dataReceived ( "<STR_LIT>" ) <EOL> self . protocol . dataReceived ( "<STR_LIT>" ) <EOL> self . assertEqual ( protocol . data , "<STR_LIT>" ) <EOL> self . protocol . connectionLost ( Failure ( ArbitraryException ( ) ) ) <EOL> return assertResponseFailed ( <EOL> self , fail ( protocol . closedReason ) , [ ArbitraryException , _DataLoss ] ) <EOL> def test_parserDataReceivedException ( self ) : <EOL> """<STR_LIT>""" <EOL> requestDeferred = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> self . protocol . dataReceived ( '<STR_LIT>' ) <EOL> d = assertResponseFailed ( self , requestDeferred , [ ParseError ] ) <EOL> def cbFailed ( exc ) : <EOL> self . assertTrue ( self . transport . disconnecting ) <EOL> self . assertEqual ( <EOL> exc . reasons [ <NUM_LIT:0> ] . value . data , '<STR_LIT>' ) <EOL> self . protocol . connectionLost ( Failure ( ConnectionDone ( "<STR_LIT>" ) ) ) <EOL> d . addCallback ( cbFailed ) <EOL> return d <EOL> def test_proxyStopped ( self ) : <EOL> """<STR_LIT>""" <EOL> requestDeferred = self . protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> transport = self . protocol . _parser . transport <EOL> self . assertIdentical ( transport . _producer , self . transport ) <EOL> self . protocol . _disconnectParser ( Failure ( ConnectionDone ( "<STR_LIT>" ) ) ) <EOL> self . assertIdentical ( transport . _producer , None ) <EOL> return assertResponseFailed ( self , requestDeferred , [ ConnectionDone ] ) <EOL> def test_abortClosesConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> r1 = [ ] <EOL> r2 = [ ] <EOL> protocol . abort ( ) . addCallback ( r1 . append ) <EOL> protocol . abort ( ) . addCallback ( r2 . append ) <EOL> self . assertEqual ( ( r1 , r2 ) , ( [ ] , [ ] ) ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> protocol . connectionLost ( Failure ( ConnectionDone ( ) ) ) <EOL> self . assertEqual ( r1 , [ None ] ) <EOL> self . assertEqual ( r2 , [ None ] ) <EOL> def test_abortAfterConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> protocol . connectionLost ( Failure ( ConnectionDone ( ) ) ) <EOL> result = [ ] <EOL> protocol . abort ( ) . addCallback ( result . append ) <EOL> self . assertEqual ( result , [ None ] ) <EOL> self . assertEqual ( protocol . _state , "<STR_LIT>" ) <EOL> def test_abortBeforeResponseBody ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> result = protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> protocol . abort ( ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> protocol . connectionLost ( Failure ( ConnectionDone ( ) ) ) <EOL> return assertResponseFailed ( self , result , [ ConnectionAborted ] ) <EOL> def test_abortAfterResponseHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> result = protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> ) <EOL> testResult = Deferred ( ) <EOL> class BodyDestination ( Protocol ) : <EOL> """<STR_LIT>""" <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol . abort ( ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> testResult . errback ( reason ) <EOL> def deliverBody ( response ) : <EOL> """<STR_LIT>""" <EOL> response . deliverBody ( BodyDestination ( ) ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> protocol . connectionLost ( Failure ( ConnectionDone ( ) ) ) <EOL> def checkError ( error ) : <EOL> self . assertIsInstance ( error . response , Response ) <EOL> result . addCallback ( deliverBody ) <EOL> deferred = assertResponseFailed ( self , testResult , <EOL> [ ConnectionAborted , _DataLoss ] ) <EOL> return deferred . addCallback ( checkError ) <EOL> def test_quiescentCallbackCalled ( self ) : <EOL> """<STR_LIT>""" <EOL> quiescentResult = [ ] <EOL> def callback ( p ) : <EOL> self . assertEqual ( p , protocol ) <EOL> self . assertEqual ( p . state , "<STR_LIT>" ) <EOL> quiescentResult . append ( p ) <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( callback ) <EOL> protocol . makeConnection ( transport ) <EOL> requestDeferred = protocol . request ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = True ) ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> self . assertEqual ( quiescentResult , [ ] ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> bodyProtocol = AccumulatingProtocol ( ) <EOL> bodyProtocol . closedDeferred = Deferred ( ) <EOL> bodyProtocol . closedDeferred . addCallback ( <EOL> lambda ign : quiescentResult . append ( "<STR_LIT>" ) ) <EOL> response . deliverBody ( bodyProtocol ) <EOL> protocol . dataReceived ( "<STR_LIT:abc>" ) <EOL> bodyProtocol . closedReason . trap ( ResponseDone ) <EOL> self . assertEqual ( quiescentResult , [ protocol , "<STR_LIT>" ] ) <EOL> self . assertEqual ( protocol . _parser , None ) <EOL> self . assertEqual ( protocol . _finishedRequest , None ) <EOL> self . assertEqual ( protocol . _currentRequest , None ) <EOL> self . assertEqual ( protocol . _transportProxy , None ) <EOL> self . assertEqual ( protocol . _responseDeferred , None ) <EOL> def test_transportProducingWhenQuiescentAfterFullBody ( self ) : <EOL> """<STR_LIT>""" <EOL> quiescentResult = [ ] <EOL> def callback ( p ) : <EOL> self . assertEqual ( p , protocol ) <EOL> self . assertEqual ( p . state , "<STR_LIT>" ) <EOL> quiescentResult . append ( p ) <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( callback ) <EOL> protocol . makeConnection ( transport ) <EOL> requestDeferred = protocol . request ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = True ) ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> "<STR_LIT>" <EOL> ) <EOL> response = self . successResultOf ( requestDeferred ) <EOL> self . assertEqual ( response . _state , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( quiescentResult ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> def test_quiescentCallbackCalledEmptyResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> quiescentResult = [ ] <EOL> def callback ( p ) : <EOL> self . assertEqual ( p , protocol ) <EOL> self . assertEqual ( p . state , "<STR_LIT>" ) <EOL> quiescentResult . append ( p ) <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( callback ) <EOL> protocol . makeConnection ( transport ) <EOL> requestDeferred = protocol . request ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = True ) ) <EOL> requestDeferred . addCallback ( quiescentResult . append ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> self . assertEqual ( len ( quiescentResult ) , <NUM_LIT:2> ) <EOL> self . assertIdentical ( quiescentResult [ <NUM_LIT:0> ] , protocol ) <EOL> self . assertIsInstance ( quiescentResult [ <NUM_LIT:1> ] , Response ) <EOL> def test_quiescentCallbackNotCalled ( self ) : <EOL> """<STR_LIT>""" <EOL> quiescentResult = [ ] <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( quiescentResult . append ) <EOL> protocol . makeConnection ( transport ) <EOL> requestDeferred = protocol . request ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = True ) ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> bodyProtocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( bodyProtocol ) <EOL> bodyProtocol . closedReason . trap ( ResponseDone ) <EOL> self . assertEqual ( quiescentResult , [ ] ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> def test_quiescentCallbackNotCalledNonPersistentQuery ( self ) : <EOL> """<STR_LIT>""" <EOL> quiescentResult = [ ] <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( quiescentResult . append ) <EOL> protocol . makeConnection ( transport ) <EOL> requestDeferred = protocol . request ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = False ) ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> bodyProtocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( bodyProtocol ) <EOL> bodyProtocol . closedReason . trap ( ResponseDone ) <EOL> self . assertEqual ( quiescentResult , [ ] ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> def test_quiescentCallbackThrows ( self ) : <EOL> """<STR_LIT>""" <EOL> def callback ( p ) : <EOL> raise ZeroDivisionError ( ) <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( callback ) <EOL> protocol . makeConnection ( transport ) <EOL> requestDeferred = protocol . request ( <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = True ) ) <EOL> protocol . dataReceived ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> result = [ ] <EOL> requestDeferred . addCallback ( result . append ) <EOL> response = result [ <NUM_LIT:0> ] <EOL> bodyProtocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( bodyProtocol ) <EOL> bodyProtocol . closedReason . trap ( ResponseDone ) <EOL> errors = self . flushLoggedErrors ( ZeroDivisionError ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( transport . disconnecting ) <EOL> def test_cancelBeforeResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> result = protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> result . cancel ( ) <EOL> self . assertTrue ( transport . aborting ) <EOL> return assertWrapperExceptionTypes ( <EOL> self , result , ResponseNeverReceived , [ CancelledError ] ) <EOL> def test_cancelDuringResponse ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> result = protocol . request ( Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) ) <EOL> protocol . dataReceived ( "<STR_LIT>" ) <EOL> result . cancel ( ) <EOL> self . assertTrue ( transport . aborting ) <EOL> return assertResponseFailed ( self , result , [ CancelledError ] ) <EOL> def assertCancelDuringBodyProduction ( self , producerLength ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> protocol = HTTP11ClientProtocol ( ) <EOL> protocol . makeConnection ( transport ) <EOL> producer = StringProducer ( producerLength ) <EOL> nonlocal = { '<STR_LIT>' : False } <EOL> def cancel ( ign ) : <EOL> nonlocal [ '<STR_LIT>' ] = True <EOL> def startProducing ( consumer ) : <EOL> producer . consumer = consumer <EOL> producer . finished = Deferred ( cancel ) <EOL> return producer . finished <EOL> producer . startProducing = startProducing <EOL> result = protocol . request ( Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) ) <EOL> producer . consumer . write ( '<STR_LIT:x>' * <NUM_LIT:5> ) <EOL> result . cancel ( ) <EOL> self . assertTrue ( transport . aborting ) <EOL> self . assertTrue ( nonlocal [ '<STR_LIT>' ] ) <EOL> return assertRequestGenerationFailed ( self , result , [ CancelledError ] ) <EOL> def test_cancelDuringBodyProduction ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertCancelDuringBodyProduction ( <NUM_LIT:10> ) <EOL> def test_cancelDuringChunkedBodyProduction ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . assertCancelDuringBodyProduction ( UNKNOWN_LENGTH ) <EOL> class StringProducer : <EOL> """<STR_LIT>""" <EOL> implements ( IBodyProducer ) <EOL> stopped = False <EOL> def __init__ ( self , length ) : <EOL> self . length = length <EOL> def startProducing ( self , consumer ) : <EOL> self . consumer = consumer <EOL> self . finished = Deferred ( ) <EOL> return self . finished <EOL> def stopProducing ( self ) : <EOL> self . stopped = True <EOL> class RequestTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . transport = StringTransport ( ) <EOL> def test_sendSimplestRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None ) . writeTo ( self . transport ) <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> def test_sendSimplestPersistentRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> req = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , None , persistent = True ) <EOL> req . writeTo ( self . transport ) <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> def test_sendRequestHeaders ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = Headers ( { '<STR_LIT>' : [ '<STR_LIT:bar>' , '<STR_LIT>' ] , '<STR_LIT:host>' : [ '<STR_LIT>' ] } ) <EOL> Request ( '<STR_LIT:GET>' , '<STR_LIT>' , headers , None ) . writeTo ( self . transport ) <EOL> lines = self . transport . value ( ) . split ( '<STR_LIT:\r\n>' ) <EOL> self . assertEqual ( lines [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( lines [ - <NUM_LIT:2> : ] , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> del lines [ <NUM_LIT:0> ] , lines [ - <NUM_LIT:2> : ] <EOL> lines . sort ( ) <EOL> self . assertEqual ( <EOL> lines , <EOL> [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def test_sendChunkedRequestBody ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( UNKNOWN_LENGTH ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> request . writeTo ( self . transport ) <EOL> self . assertNotIdentical ( producer . consumer , None ) <EOL> self . assertIdentical ( self . transport . producer , producer ) <EOL> self . assertTrue ( self . transport . streaming ) <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> self . transport . clear ( ) <EOL> producer . consumer . write ( '<STR_LIT:x>' * <NUM_LIT:3> ) <EOL> producer . consumer . write ( '<STR_LIT:y>' * <NUM_LIT:15> ) <EOL> producer . finished . callback ( None ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> def test_sendChunkedRequestBodyWithError ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( UNKNOWN_LENGTH ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> writeDeferred = request . writeTo ( self . transport ) <EOL> self . transport . clear ( ) <EOL> producer . finished . errback ( ArbitraryException ( ) ) <EOL> def cbFailed ( ignored ) : <EOL> self . assertEqual ( self . transport . value ( ) , "<STR_LIT>" ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> d = self . assertFailure ( writeDeferred , ArbitraryException ) <EOL> d . addCallback ( cbFailed ) <EOL> return d <EOL> def test_sendRequestBodyWithLength ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> request . writeTo ( self . transport ) <EOL> self . assertNotIdentical ( producer . consumer , None ) <EOL> self . assertIdentical ( self . transport . producer , producer ) <EOL> self . assertTrue ( self . transport . streaming ) <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" ) <EOL> self . transport . clear ( ) <EOL> producer . consumer . write ( '<STR_LIT:abc>' ) <EOL> producer . finished . callback ( None ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> self . assertEqual ( self . transport . value ( ) , "<STR_LIT:abc>" ) <EOL> def test_sendRequestBodyWithTooFewBytes ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> writeDeferred = request . writeTo ( self . transport ) <EOL> producer . consumer . write ( '<STR_LIT>' ) <EOL> producer . finished . callback ( None ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> return self . assertFailure ( writeDeferred , WrongBodyLength ) <EOL> def _sendRequestBodyWithTooManyBytesTest ( self , finisher ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> writeDeferred = request . writeTo ( self . transport ) <EOL> producer . consumer . write ( '<STR_LIT>' ) <EOL> self . assertFalse ( producer . stopped ) <EOL> producer . consumer . write ( '<STR_LIT>' ) <EOL> self . assertTrue ( producer . stopped ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> def cbFailed ( exc ) : <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> "<STR_LIT>" ) <EOL> self . transport . clear ( ) <EOL> self . assertRaises ( ExcessWrite , producer . consumer . write , '<STR_LIT>' ) <EOL> finisher ( producer ) <EOL> self . assertEqual ( self . transport . value ( ) , "<STR_LIT>" ) <EOL> d = self . assertFailure ( writeDeferred , WrongBodyLength ) <EOL> d . addCallback ( cbFailed ) <EOL> return d <EOL> def test_sendRequestBodyWithTooManyBytes ( self ) : <EOL> """<STR_LIT>""" <EOL> def finisher ( producer ) : <EOL> producer . finished . callback ( None ) <EOL> return self . _sendRequestBodyWithTooManyBytesTest ( finisher ) <EOL> def test_sendRequestBodyErrorWithTooManyBytes ( self ) : <EOL> """<STR_LIT>""" <EOL> def finisher ( producer ) : <EOL> producer . finished . errback ( ArbitraryException ( ) ) <EOL> errors = self . flushLoggedErrors ( ArbitraryException ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> return self . _sendRequestBodyWithTooManyBytesTest ( finisher ) <EOL> def test_sendRequestBodyErrorWithConsumerError ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> request . writeTo ( self . transport ) <EOL> finishedConsuming = producer . consumer . _finished <EOL> producer . consumer . write ( '<STR_LIT:abc>' ) <EOL> producer . finished . callback ( None ) <EOL> finishedConsuming . errback ( ArbitraryException ( ) ) <EOL> self . assertEqual ( len ( self . flushLoggedErrors ( ArbitraryException ) ) , <NUM_LIT:1> ) <EOL> def _sendRequestBodyFinishedEarlyThenTooManyBytes ( self , finisher ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> writeDeferred = request . writeTo ( self . transport ) <EOL> producer . consumer . write ( '<STR_LIT>' ) <EOL> finisher ( producer ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> self . transport . clear ( ) <EOL> self . assertRaises ( ExcessWrite , producer . consumer . write , '<STR_LIT>' ) <EOL> self . assertEqual ( self . transport . value ( ) , "<STR_LIT>" ) <EOL> return writeDeferred <EOL> def test_sendRequestBodyFinishedEarlyThenTooManyBytes ( self ) : <EOL> """<STR_LIT>""" <EOL> def finisher ( producer ) : <EOL> producer . finished . callback ( None ) <EOL> return self . assertFailure ( <EOL> self . _sendRequestBodyFinishedEarlyThenTooManyBytes ( finisher ) , <EOL> WrongBodyLength ) <EOL> def test_sendRequestBodyErroredEarlyThenTooManyBytes ( self ) : <EOL> """<STR_LIT>""" <EOL> def finisher ( producer ) : <EOL> producer . finished . errback ( ArbitraryException ( ) ) <EOL> return self . assertFailure ( <EOL> self . _sendRequestBodyFinishedEarlyThenTooManyBytes ( finisher ) , <EOL> ArbitraryException ) <EOL> def test_sendChunkedRequestBodyFinishedThenWriteMore ( self , _with = None ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( UNKNOWN_LENGTH ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> writeDeferred = request . writeTo ( self . transport ) <EOL> producer . finished . callback ( _with ) <EOL> self . transport . clear ( ) <EOL> self . assertRaises ( ExcessWrite , producer . consumer . write , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( self . transport . value ( ) , "<STR_LIT>" ) <EOL> return writeDeferred <EOL> def test_sendChunkedRequestBodyFinishedWithErrorThenWriteMore ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . test_sendChunkedRequestBodyFinishedThenWriteMore ( <EOL> Failure ( ArbitraryException ( ) ) ) <EOL> return self . assertFailure ( d , ArbitraryException ) <EOL> def test_sendRequestBodyWithError ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:5> ) <EOL> request = Request ( '<STR_LIT:POST>' , '<STR_LIT>' , _boringHeaders , producer ) <EOL> writeDeferred = request . writeTo ( self . transport ) <EOL> self . assertIdentical ( self . transport . producer , producer ) <EOL> self . assertTrue ( self . transport . streaming ) <EOL> producer . consumer . write ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> self . transport . value ( ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:\r\n>" <EOL> "<STR_LIT>" ) <EOL> self . assertFalse ( self . transport . disconnecting ) <EOL> producer . finished . errback ( Failure ( ArbitraryException ( ) ) ) <EOL> self . assertFalse ( self . transport . disconnecting ) <EOL> self . assertIdentical ( self . transport . producer , None ) <EOL> return self . assertFailure ( writeDeferred , ArbitraryException ) <EOL> def test_hostHeaderRequired ( self ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , Headers ( { } ) , None ) <EOL> self . assertRaises ( BadHeaders , request . writeTo , self . transport ) <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT>' ) <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , Headers ( { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] } ) , None ) <EOL> self . assertRaises ( BadHeaders , request . writeTo , self . transport ) <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT>' ) <EOL> def test_stopWriting ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , producer ) <EOL> request . writeTo ( self . transport ) <EOL> self . assertFalse ( producer . stopped ) <EOL> request . stopWriting ( ) <EOL> self . assertTrue ( producer . stopped ) <EOL> def test_brokenStopProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> producer = StringProducer ( <NUM_LIT:3> ) <EOL> def brokenStopProducing ( ) : <EOL> raise ArbitraryException ( "<STR_LIT>" ) <EOL> producer . stopProducing = brokenStopProducing <EOL> request = Request ( '<STR_LIT:GET>' , '<STR_LIT:/>' , _boringHeaders , producer ) <EOL> request . writeTo ( self . transport ) <EOL> request . stopWriting ( ) <EOL> self . assertEqual ( <EOL> len ( self . flushLoggedErrors ( ArbitraryException ) ) , <NUM_LIT:1> ) <EOL> class LengthEnforcingConsumerTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . result = Deferred ( ) <EOL> self . producer = StringProducer ( <NUM_LIT:10> ) <EOL> self . transport = StringTransport ( ) <EOL> self . enforcer = LengthEnforcingConsumer ( <EOL> self . producer , self . transport , self . result ) <EOL> def test_write ( self ) : <EOL> """<STR_LIT>""" <EOL> self . enforcer . write ( '<STR_LIT:abc>' ) <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT:abc>' ) <EOL> self . transport . clear ( ) <EOL> self . enforcer . write ( '<STR_LIT>' ) <EOL> self . assertEqual ( self . transport . value ( ) , '<STR_LIT>' ) <EOL> def test_finishedEarly ( self ) : <EOL> """<STR_LIT>""" <EOL> self . enforcer . write ( '<STR_LIT:x>' * <NUM_LIT:9> ) <EOL> self . assertRaises ( WrongBodyLength , self . enforcer . _noMoreWritesExpected ) <EOL> def test_writeTooMany ( self , _unregisterAfter = False ) : <EOL> """<STR_LIT>""" <EOL> self . enforcer . write ( '<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . assertFalse ( self . producer . stopped ) <EOL> self . enforcer . write ( '<STR_LIT:x>' ) <EOL> self . assertTrue ( self . producer . stopped ) <EOL> if _unregisterAfter : <EOL> self . enforcer . _noMoreWritesExpected ( ) <EOL> return self . assertFailure ( self . result , WrongBodyLength ) <EOL> def test_writeAfterNoMoreExpected ( self ) : <EOL> """<STR_LIT>""" <EOL> self . enforcer . write ( '<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . enforcer . _noMoreWritesExpected ( ) <EOL> self . assertFalse ( self . producer . stopped ) <EOL> self . assertRaises ( ExcessWrite , self . enforcer . write , '<STR_LIT:x>' ) <EOL> self . assertTrue ( self . producer . stopped ) <EOL> def test_finishedLate ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . test_writeTooMany ( True ) <EOL> def test_finished ( self ) : <EOL> """<STR_LIT>""" <EOL> self . enforcer . write ( '<STR_LIT:x>' * <NUM_LIT:10> ) <EOL> self . assertIdentical ( self . enforcer . _noMoreWritesExpected ( ) , None ) <EOL> def test_stopProducingRaises ( self ) : <EOL> """<STR_LIT>""" <EOL> def brokenStopProducing ( ) : <EOL> StringProducer . stopProducing ( self . producer ) <EOL> raise ArbitraryException ( "<STR_LIT>" ) <EOL> self . producer . stopProducing = brokenStopProducing <EOL> def cbFinished ( ignored ) : <EOL> self . assertEqual ( <EOL> len ( self . flushLoggedErrors ( ArbitraryException ) ) , <NUM_LIT:1> ) <EOL> d = self . test_writeTooMany ( ) <EOL> d . addCallback ( cbFinished ) <EOL> return d <EOL> class RequestBodyConsumerTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_interface ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( <EOL> verifyObject ( IConsumer , ChunkedEncoder ( StringTransport ( ) ) ) ) <EOL> def test_write ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> encoder = ChunkedEncoder ( transport ) <EOL> encoder . write ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( transport . value ( ) , '<STR_LIT>' ) <EOL> transport . clear ( ) <EOL> encoder . write ( '<STR_LIT:x>' * <NUM_LIT:16> ) <EOL> self . assertEqual ( transport . value ( ) , '<STR_LIT>' + '<STR_LIT:x>' * <NUM_LIT:16> + '<STR_LIT:\r\n>' ) <EOL> def test_producerRegistration ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> producer = object ( ) <EOL> encoder = ChunkedEncoder ( transport ) <EOL> encoder . registerProducer ( producer , True ) <EOL> self . assertIdentical ( transport . producer , producer ) <EOL> self . assertTrue ( transport . streaming ) <EOL> encoder . unregisterProducer ( ) <EOL> self . assertIdentical ( transport . producer , None ) <EOL> self . assertEqual ( transport . value ( ) , '<STR_LIT>' ) <EOL> class TransportProxyProducerTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_interface ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( <EOL> verifyObject ( IPushProducer , TransportProxyProducer ( None ) ) ) <EOL> def test_stopProxyingUnreferencesProducer ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> proxy = TransportProxyProducer ( transport ) <EOL> self . assertIdentical ( proxy . _producer , transport ) <EOL> proxy . _stopProxying ( ) <EOL> self . assertIdentical ( proxy . _producer , None ) <EOL> def test_resumeProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> transport . pauseProducing ( ) <EOL> proxy = TransportProxyProducer ( transport ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> proxy . resumeProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> transport . pauseProducing ( ) <EOL> proxy . _stopProxying ( ) <EOL> proxy . resumeProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> def test_pauseProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> proxy = TransportProxyProducer ( transport ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> proxy . pauseProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> transport . resumeProducing ( ) <EOL> proxy . _stopProxying ( ) <EOL> proxy . pauseProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> def test_stopProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> proxy = TransportProxyProducer ( transport ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> proxy . stopProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> transport = StringTransport ( ) <EOL> proxy = TransportProxyProducer ( transport ) <EOL> proxy . _stopProxying ( ) <EOL> proxy . stopProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> class ResponseTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_verifyInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> self . assertTrue ( verifyObject ( IResponse , response ) ) <EOL> def test_makeConnection ( self ) : <EOL> """<STR_LIT>""" <EOL> producers = [ ] <EOL> transport = StringTransport ( ) <EOL> class SomeProtocol ( Protocol ) : <EOL> def makeConnection ( self , producer ) : <EOL> producers . append ( producer ) <EOL> consumer = SomeProtocol ( ) <EOL> response = justTransportResponse ( transport ) <EOL> response . deliverBody ( consumer ) <EOL> [ theProducer ] = producers <EOL> theProducer . pauseProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> theProducer . resumeProducing ( ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> def test_dataReceived ( self ) : <EOL> """<STR_LIT>""" <EOL> bytes = [ ] <EOL> class ListConsumer ( Protocol ) : <EOL> def dataReceived ( self , data ) : <EOL> bytes . append ( data ) <EOL> consumer = ListConsumer ( ) <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . deliverBody ( consumer ) <EOL> response . _bodyDataReceived ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( bytes , [ '<STR_LIT:foo>' ] ) <EOL> def test_connectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> lost = [ ] <EOL> class ListConsumer ( Protocol ) : <EOL> def connectionLost ( self , reason ) : <EOL> lost . append ( reason ) <EOL> consumer = ListConsumer ( ) <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . deliverBody ( consumer ) <EOL> response . _bodyDataFinished ( ) <EOL> lost [ <NUM_LIT:0> ] . trap ( ResponseDone ) <EOL> self . assertEqual ( len ( lost ) , <NUM_LIT:1> ) <EOL> self . assertIdentical ( response . _bodyProtocol , None ) <EOL> def test_bufferEarlyData ( self ) : <EOL> """<STR_LIT>""" <EOL> bytes = [ ] <EOL> class ListConsumer ( Protocol ) : <EOL> def dataReceived ( self , data ) : <EOL> bytes . append ( data ) <EOL> protocol = ListConsumer ( ) <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . _bodyDataReceived ( '<STR_LIT:foo>' ) <EOL> response . _bodyDataReceived ( '<STR_LIT:bar>' ) <EOL> response . deliverBody ( protocol ) <EOL> response . _bodyDataReceived ( '<STR_LIT>' ) <EOL> self . assertEqual ( bytes , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' ] ) <EOL> self . assertIdentical ( response . _bodyBuffer , None ) <EOL> def test_multipleStartProducingFails ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . deliverBody ( Protocol ( ) ) <EOL> self . assertRaises ( RuntimeError , response . deliverBody , Protocol ( ) ) <EOL> def test_startProducingAfterFinishedFails ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . deliverBody ( Protocol ( ) ) <EOL> response . _bodyDataFinished ( ) <EOL> self . assertRaises ( RuntimeError , response . deliverBody , Protocol ( ) ) <EOL> def test_bodyDataReceivedAfterFinishedFails ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . _bodyDataFinished ( ) <EOL> self . assertRaises ( RuntimeError , response . _bodyDataReceived , '<STR_LIT:foo>' ) <EOL> def test_bodyDataReceivedAfterDeliveryFails ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . _bodyDataFinished ( ) <EOL> response . deliverBody ( Protocol ( ) ) <EOL> self . assertRaises ( RuntimeError , response . _bodyDataReceived , '<STR_LIT:foo>' ) <EOL> def test_bodyDataFinishedAfterFinishedFails ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . _bodyDataFinished ( ) <EOL> self . assertRaises ( RuntimeError , response . _bodyDataFinished ) <EOL> def test_bodyDataFinishedAfterDeliveryFails ( self ) : <EOL> """<STR_LIT>""" <EOL> response = justTransportResponse ( StringTransport ( ) ) <EOL> response . _bodyDataFinished ( ) <EOL> response . deliverBody ( Protocol ( ) ) <EOL> self . assertRaises ( RuntimeError , response . _bodyDataFinished ) <EOL> def test_transportResumed ( self ) : <EOL> """<STR_LIT>""" <EOL> transportState = [ ] <EOL> class ListConsumer ( Protocol ) : <EOL> def makeConnection ( self , transport ) : <EOL> transportState . append ( transport . producerState ) <EOL> transport = StringTransport ( ) <EOL> transport . pauseProducing ( ) <EOL> protocol = ListConsumer ( ) <EOL> response = justTransportResponse ( transport ) <EOL> self . assertEqual ( transport . producerState , '<STR_LIT>' ) <EOL> response . deliverBody ( protocol ) <EOL> self . assertEqual ( transportState , [ '<STR_LIT>' ] ) <EOL> def test_bodyDataFinishedBeforeStartProducing ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> response = justTransportResponse ( transport ) <EOL> response . _bodyDataReceived ( '<STR_LIT:foo>' ) <EOL> response . _bodyDataReceived ( '<STR_LIT:bar>' ) <EOL> response . _bodyDataFinished ( ) <EOL> protocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( protocol ) <EOL> self . assertEqual ( protocol . data , '<STR_LIT>' ) <EOL> protocol . closedReason . trap ( ResponseDone ) <EOL> def test_finishedWithErrorWhenConnected ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> response = justTransportResponse ( transport ) <EOL> protocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( protocol ) <EOL> self . assertEqual ( response . _state , '<STR_LIT>' ) <EOL> response . _bodyDataFinished ( Failure ( ArbitraryException ( ) ) ) <EOL> protocol . closedReason . trap ( ArbitraryException ) <EOL> def test_finishedWithErrorWhenInitial ( self ) : <EOL> """<STR_LIT>""" <EOL> transport = StringTransport ( ) <EOL> response = justTransportResponse ( transport ) <EOL> self . assertEqual ( response . _state , '<STR_LIT>' ) <EOL> response . _bodyDataFinished ( Failure ( ArbitraryException ( ) ) ) <EOL> protocol = AccumulatingProtocol ( ) <EOL> response . deliverBody ( protocol ) <EOL> protocol . closedReason . trap ( ArbitraryException ) </s>
