Python twisted.test.proto_helpers.StringTransport() Examples
The following are 30
code examples of twisted.test.proto_helpers.StringTransport().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
twisted.test.proto_helpers
, or try the search function
.
Example #1
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_abortClosesConnection(self): """ L{HTTP11ClientProtocol.abort} will tell the transport to close its connection when it is invoked, and returns a C{Deferred} that fires when the connection is lost. """ transport = StringTransport() protocol = HTTP11ClientProtocol() protocol.makeConnection(transport) r1 = [] r2 = [] protocol.abort().addCallback(r1.append) protocol.abort().addCallback(r2.append) self.assertEqual((r1, r2), ([], [])) self.assertTrue(transport.disconnecting) # Disconnect protocol, the Deferreds will fire: protocol.connectionLost(Failure(ConnectionDone())) self.assertEqual(r1, [None]) self.assertEqual(r2, [None])
Example #2
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_unknownContentLength(self): """ If a response does not include a I{Transfer-Encoding} or a I{Content-Length}, the end of response body is indicated by the connection being closed. """ finished = [] protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), finished.append) transport = StringTransport() protocol.makeConnection(transport) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') body = [] protocol.response._bodyDataReceived = body.append protocol.dataReceived(b'\r\n') protocol.dataReceived(b'foo') protocol.dataReceived(b'bar') self.assertEqual(body, [b'foo', b'bar']) protocol.connectionLost(ConnectionDone(u"simulated end of connection")) self.assertEqual(finished, [b''])
Example #3
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_connectionLost(self): """ The L{IProtocol} provider passed to L{Response.deliverBody} has its C{connectionLost} method called with a L{Failure} wrapping L{ResponseDone} when the response's C{_bodyDataFinished} method is called. """ lost = [] class ListConsumer(Protocol): def connectionLost(self, reason): lost.append(reason) consumer = ListConsumer() response = justTransportResponse(StringTransport()) response.deliverBody(consumer) response._bodyDataFinished() lost[0].trap(ResponseDone) self.assertEqual(len(lost), 1) # The protocol reference should be dropped, too, to facilitate GC or # whatever. self.assertIdentical(response._bodyProtocol, None)
Example #4
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_wrappedOnPersistentReturned(self): """ If L{client.HTTPConnectionPool.getConnection} returns a previously cached connection, it will get wrapped in a L{client._RetryingHTTP11ClientProtocol}. """ pool = client.HTTPConnectionPool(Clock()) # Add a connection to the cache: protocol = StubHTTPProtocol() protocol.makeConnection(StringTransport()) pool._putConnection(123, protocol) # Retrieve it, it should come back wrapped in a # _RetryingHTTP11ClientProtocol: d = pool.getConnection(123, DummyEndpoint()) def gotConnection(connection): self.assertIsInstance(connection, client._RetryingHTTP11ClientProtocol) self.assertIdentical(connection._clientProtocol, protocol) return d.addCallback(gotConnection)
Example #5
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_dataReceived(self): """ The L{IProtocol} provider passed to L{Response.deliverBody} has its C{dataReceived} method called with bytes received as part of the response body. """ bytes = [] class ListConsumer(Protocol): def dataReceived(self, data): bytes.append(data) consumer = ListConsumer() response = justTransportResponse(StringTransport()) response.deliverBody(consumer) response._bodyDataReceived(b'foo') self.assertEqual(bytes, [b'foo'])
Example #6
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_getCachedConnection(self): """ Getting an address which has a cached connection returns the cached connection, removes it from the cache and cancels its timeout. """ # We start out with one cached connection: protocol = StubHTTPProtocol() protocol.makeConnection(StringTransport()) self.pool._putConnection(("http", b"example.com", 80), protocol) def gotConnection(conn): # We got the cached connection: self.assertIdentical(protocol, conn) self.assertNotIn( conn, self.pool._connections[("http", b"example.com", 80)]) # And the timeout was cancelled: self.fakeReactor.advance(241) self.assertEqual(conn.transport.disconnecting, False) self.assertNotIn(conn, self.pool._timeouts) return self.pool.getConnection(("http", b"example.com", 80), BadEndpoint(), ).addCallback(gotConnection)
Example #7
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_maxPersistentPerHost(self): """ C{maxPersistentPerHost} is enforced per C{(scheme, host, port)}: different keys have different max connections. """ def addProtocol(scheme, host, port): p = StubHTTPProtocol() p.makeConnection(StringTransport()) self.pool._putConnection((scheme, host, port), p) return p persistent = [] persistent.append(addProtocol("http", b"example.com", 80)) persistent.append(addProtocol("http", b"example.com", 80)) addProtocol("https", b"example.com", 443) addProtocol("http", b"www2.example.com", 80) self.assertEqual( self.pool._connections[("http", b"example.com", 80)], persistent) self.assertEqual( len(self.pool._connections[("https", b"example.com", 443)]), 1) self.assertEqual( len(self.pool._connections[("http", b"www2.example.com", 80)]), 1)
Example #8
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_dontRetryIfRetryAutomaticallyFalse(self): """ If L{HTTPConnectionPool.retryAutomatically} is set to C{False}, don't wrap connections with retrying logic. """ pool = client.HTTPConnectionPool(Clock()) pool.retryAutomatically = False # Add a connection to the cache: protocol = StubHTTPProtocol() protocol.makeConnection(StringTransport()) pool._putConnection(123, protocol) # Retrieve it, it should come back unwrapped: d = pool.getConnection(123, DummyEndpoint()) def gotConnection(connection): self.assertIdentical(connection, protocol) return d.addCallback(gotConnection)
Example #9
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_connectionLostBeforeBody(self): """ If L{HTTPClientParser.connectionLost} is called before the headers are finished, the C{_responseDeferred} is fired with the L{Failure} passed to C{connectionLost}. """ transport = StringTransport() protocol = HTTPClientParser(Request(b'GET', b'/', _boringHeaders, None), None) protocol.makeConnection(transport) # Grab this here because connectionLost gets rid of the attribute responseDeferred = protocol._responseDeferred protocol.connectionLost(Failure(ArbitraryException())) return assertResponseFailed( self, responseDeferred, [ArbitraryException])
Example #10
Source File: test_gateway.py From bitmask-dev with GNU General Public License v3.0 | 6 votes |
def test_gateway_accepts_valid_email(self): """ Test if SMTP server responds correctly for valid interaction. """ SMTP_ANSWERS = ['220 ' + IP_OR_HOST_REGEX + ' NO UCE NO UBE NO RELAY PROBES', '250 ' + IP_OR_HOST_REGEX + ' Hello ' + IP_OR_HOST_REGEX + ', nice to meet you', '250 Sender address accepted', '250 Recipient address accepted', '354 Continue'] user = TEST_USER proto = getSMTPFactory({user: OutgoingMail(user, self.km)}, {user: None}) transport = proto_helpers.StringTransport() proto.makeConnection(transport) reply = "" for i, line in enumerate(self.EMAIL_DATA): reply += yield self.getReply(line + '\r\n', proto, transport) self.assertMatch(reply, '\r\n'.join(SMTP_ANSWERS), 'Did not get expected answer from gateway.') proto.setTimeout(None)
Example #11
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_bufferEarlyData(self): """ If data is delivered to the L{Response} before a protocol is registered with C{deliverBody}, that data is buffered until the protocol is registered and then is delivered. """ bytes = [] class ListConsumer(Protocol): def dataReceived(self, data): bytes.append(data) protocol = ListConsumer() response = justTransportResponse(StringTransport()) response._bodyDataReceived(b'foo') response._bodyDataReceived(b'bar') response.deliverBody(protocol) response._bodyDataReceived(b'baz') self.assertEqual(bytes, [b'foo', b'bar', b'baz']) # Make sure the implementation-detail-byte-buffer is cleared because # not clearing it wastes memory. self.assertIdentical(response._bodyBuffer, None)
Example #12
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_stopProducing(self): """ L{TransportProxyProducer.stopProducing} calls the wrapped transport's C{stopProducing} method unless told to stop proxying. """ transport = StringTransport() proxy = TransportProxyProducer(transport) # The transport should still be producing. self.assertEqual(transport.producerState, u'producing') proxy.stopProducing() # The transport should now be stopped. self.assertEqual(transport.producerState, u'stopped') transport = StringTransport() proxy = TransportProxyProducer(transport) proxy._stopProxying() proxy.stopProducing() # The transport should not have been stopped. self.assertEqual(transport.producerState, u'producing')
Example #13
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_extraBytesPassedBackHEAD(self): """ If extra bytes are received past the end of the headers of a response to a HEAD request, they are passed to the finish callback. """ finished = [] protocol = HTTPClientParser( Request(b'HEAD', b'/', _boringHeaders, None), finished.append) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'Content-Length: 12\r\n') protocol.dataReceived(b'\r\nHere is another thing!') self.assertEqual(protocol.state, DONE) self.assertEqual(finished, [b'Here is another thing!'])
Example #14
Source File: test_agent.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_deprecatedTransport(self): """ Calling L{client.readBody} with a transport that does not implement L{twisted.internet.interfaces.ITCPTransport} produces a deprecation warning, but no exception when cancelling. """ response = DummyResponse(transportFactory=StringTransport) response.transport.abortConnection = None d = self.assertWarns( DeprecationWarning, 'Using readBody with a transport that does not have an ' 'abortConnection method', __file__, lambda: client.readBody(response)) d.cancel() self.failureResultOf(d, defer.CancelledError)
Example #15
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_extraBytesPassedBack(self): """ If extra bytes are received past the end of a response, they are passed to the finish callback. """ finished = [] protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), finished.append) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'Content-Length: 0\r\n') protocol.dataReceived(b'\r\nHere is another thing!') self.assertEqual(protocol.state, DONE) self.assertEqual(finished, [b'Here is another thing!'])
Example #16
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_multipleContentLengthHeaders(self): """ If a response includes multiple I{Content-Length} headers, L{HTTPClientParser.dataReceived} raises L{ValueError} to indicate that the response is invalid and the transport is now unusable. """ protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), None) protocol.makeConnection(StringTransport()) self.assertRaises( ValueError, protocol.dataReceived, b'HTTP/1.1 200 OK\r\n' b'Content-Length: 1\r\n' b'Content-Length: 2\r\n' b'\r\n')
Example #17
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_zeroContentLength(self): """ If a response includes a I{Content-Length} header indicating zero bytes in the response, L{Response.length} is set accordingly and no data is delivered to L{Response._bodyDataReceived}. """ finished = [] protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), finished.append) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') body = [] protocol.response._bodyDataReceived = body.append protocol.dataReceived(b'Content-Length: 0\r\n') protocol.dataReceived(b'\r\n') self.assertEqual(protocol.state, DONE) self.assertEqual(body, []) self.assertEqual(finished, [b'']) self.assertEqual(protocol.response.length, 0)
Example #18
Source File: test_http2.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_resetAfterBody(self): """ A client that immediately resets after sending the body causes Twisted to send no response. """ frameFactory = FrameFactory() transport = StringTransport() a = H2Connection() a.requestFactory = DummyHTTPHandler requestBytes = frameFactory.clientConnectionPreface() requestBytes += buildRequestBytes( headers=self.getRequestHeaders, data=[], frameFactory=frameFactory ) requestBytes += frameFactory.buildRstStreamFrame( streamID=1 ).serialize() a.makeConnection(transport) a.dataReceived(requestBytes) frames = framesFromBytes(transport.value()) self.assertEqual(len(frames), 1) self.assertNotIn(1, a._streamCleanupCallbacks)
Example #19
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_connectionHeaders(self): """ The connection control headers are added to the parser's C{connHeaders} L{Headers} instance. """ protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda rest: None) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'Content-Length: 123\r\n') protocol.dataReceived(b'Connection: close\r\n') protocol.dataReceived(b'\r\n') self.assertEqual( protocol.response.headers, Headers({})) self.assertEqual( protocol.connHeaders, Headers({b'content-length': [b'123'], b'connection': [b'close']})) self.assertEqual(protocol.response.length, 123)
Example #20
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_responseHeaders(self): """ The response headers are added to the response object's C{headers} L{Headers} instance. """ protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda rest: None) protocol.makeConnection(StringTransport()) protocol.dataReceived(b'HTTP/1.1 200 OK\r\n') protocol.dataReceived(b'X-Foo: bar\r\n') protocol.dataReceived(b'\r\n') self.assertEqual( protocol.connHeaders, Headers({})) self.assertEqual( protocol.response.headers, Headers({b'x-foo': [b'bar']})) self.assertIdentical(protocol.response.length, UNKNOWN_LENGTH)
Example #21
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_pauseProducing(self): """ L{TransportProxyProducer.pauseProducing} calls the wrapped transport's C{pauseProducing} method unless told to stop proxying. """ transport = StringTransport() proxy = TransportProxyProducer(transport) # The transport should still be producing. self.assertEqual(transport.producerState, u'producing') proxy.pauseProducing() # The transport should now be paused. self.assertEqual(transport.producerState, u'paused') transport.resumeProducing() proxy._stopProxying() # The proxy should no longer do anything to the transport. proxy.pauseProducing() self.assertEqual(transport.producerState, u'producing')
Example #22
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_parserDataReceivedException(self): """ If the parser L{HTTP11ClientProtocol} delivers bytes to in C{dataReceived} raises an exception, the exception is wrapped in a L{Failure} and passed to the parser's C{connectionLost} and then the L{HTTP11ClientProtocol}'s transport is disconnected. """ requestDeferred = self.protocol.request(Request(b'GET', b'/', _boringHeaders, None)) self.protocol.dataReceived(b'unparseable garbage goes here\r\n') d = assertResponseFailed(self, requestDeferred, [ParseError]) def cbFailed(exc): self.assertTrue(self.transport.disconnecting) self.assertEqual( exc.reasons[0].value.data, b'unparseable garbage goes here') # Now do what StringTransport doesn't do but a real transport would # have, call connectionLost on the HTTP11ClientProtocol. Nothing # is asserted about this, but it's important for it to not raise an # exception. self.protocol.connectionLost(Failure(ConnectionDone(u"it is done"))) d.addCallback(cbFailed) return d
Example #23
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_resumeProducing(self): """ L{TransportProxyProducer.resumeProducing} calls the wrapped transport's C{resumeProducing} method unless told to stop proxying. """ transport = StringTransport() transport.pauseProducing() proxy = TransportProxyProducer(transport) # The transport should still be paused. self.assertEqual(transport.producerState, u'paused') proxy.resumeProducing() # The transport should now be resumed. self.assertEqual(transport.producerState, u'producing') transport.pauseProducing() proxy._stopProxying() # The proxy should no longer do anything to the transport. proxy.resumeProducing() self.assertEqual(transport.producerState, u'paused')
Example #24
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_noResponseAtAll(self): """ If no response at all was received and the connection is lost, the resulting error is L{ResponseNeverReceived}. """ protocol = HTTPClientParser( Request(b'HEAD', b'/', _boringHeaders, None), lambda ign: None) d = protocol._responseDeferred protocol.makeConnection(StringTransport()) protocol.connectionLost(ConnectionLost()) return self.assertFailure(d, ResponseNeverReceived)
Example #25
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_ignored1XXResponseCausesLog(self): """ When a 1XX response is ignored, Twisted emits a log. """ sample103Response = ( b'HTTP/1.1 103 Early Hints\r\n' b'Server: socketserver/1.0.0\r\n' b'Link: </other/styles.css>; rel=preload; as=style\r\n' b'Link: </other/action.js>; rel=preload; as=script\r\n' b'\r\n' ) # Catch the logs. logs = [] log.addObserver(logs.append) self.addCleanup(log.removeObserver, logs.append) protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda ign: None ) protocol.makeConnection(StringTransport()) protocol.dataReceived(sample103Response) self.assertEqual( logs[0]['message'][0], 'Ignoring unexpected 103 response' )
Example #26
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_1XXFollowedByFinalResponseOnlyEmitsFinal(self): """ When a 1XX response is swallowed, the final response that follows it is the only one that gets sent to the application. """ sample103Response = ( b'HTTP/1.1 103 Early Hints\r\n' b'Server: socketserver/1.0.0\r\n' b'Link: </other/styles.css>; rel=preload; as=style\r\n' b'Link: </other/action.js>; rel=preload; as=script\r\n' b'\r\n' ) following200Response = ( b'HTTP/1.1 200 OK\r\n' b'Content-Length: 123\r\n' b'\r\n' ) protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda ign: None ) protocol.makeConnection(StringTransport()) protocol.dataReceived(sample103Response + following200Response) self.assertEqual(protocol.response.code, 200) self.assertEqual( protocol.response.headers, Headers({})) self.assertEqual( protocol.connHeaders, Headers({b'content-length': [b'123']})) self.assertEqual(protocol.response.length, 123)
Example #27
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_1XXResponseIsSwallowed(self): """ If a response in the 1XX range is received it just gets swallowed and the parser resets itself. """ sample103Response = ( b'HTTP/1.1 103 Early Hints\r\n' b'Server: socketserver/1.0.0\r\n' b'Link: </other/styles.css>; rel=preload; as=style\r\n' b'Link: </other/action.js>; rel=preload; as=script\r\n' b'\r\n' ) protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda ign: None ) protocol.makeConnection(StringTransport()) protocol.dataReceived(sample103Response) # The response should have been erased self.assertTrue(getattr(protocol, 'response', None) is None) self.assertEqual(protocol.state, STATUS) self.assertEqual(len(list(protocol.headers.getAllRawHeaders())), 0) self.assertEqual(len(list(protocol.connHeaders.getAllRawHeaders())), 0) self.assertTrue(protocol._everReceivedData)
Example #28
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def setUp(self): """ Create an L{HTTP11ClientProtocol} connected to a fake transport. """ self.transport = StringTransport() self.protocol = HTTP11ClientProtocol() self.protocol.makeConnection(self.transport)
Example #29
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_bodyDataFinishedAfterFinishedFails(self): """ L{Response._bodyDataFinished} raises L{RuntimeError} if called more than once. """ response = justTransportResponse(StringTransport()) response._bodyDataFinished() self.assertRaises(RuntimeError, response._bodyDataFinished)
Example #30
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_multiple1XXResponsesAreIgnored(self): """ It is acceptable for multiple 1XX responses to come through, all of which get ignored. """ sample103Response = ( b'HTTP/1.1 103 Early Hints\r\n' b'Server: socketserver/1.0.0\r\n' b'Link: </other/styles.css>; rel=preload; as=style\r\n' b'Link: </other/action.js>; rel=preload; as=script\r\n' b'\r\n' ) following200Response = ( b'HTTP/1.1 200 OK\r\n' b'Content-Length: 123\r\n' b'\r\n' ) protocol = HTTPClientParser( Request(b'GET', b'/', _boringHeaders, None), lambda ign: None ) protocol.makeConnection(StringTransport()) protocol.dataReceived( sample103Response + sample103Response + sample103Response + following200Response ) self.assertEqual(protocol.response.code, 200) self.assertEqual( protocol.response.headers, Headers({})) self.assertEqual( protocol.connHeaders, Headers({b'content-length': [b'123']})) self.assertEqual(protocol.response.length, 123)