Python twisted.test.proto_helpers.AccumulatingProtocol() Examples
The following are 30
code examples of twisted.test.proto_helpers.AccumulatingProtocol().
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_bodyDataFinishedBeforeStartProducing(self): """ If the entire body is delivered to the L{Response} before the response's C{deliverBody} method is called, the protocol passed to C{deliverBody} is immediately given the body data and then disconnected. """ transport = StringTransport() response = justTransportResponse(transport) response._bodyDataReceived(b'foo') response._bodyDataReceived(b'bar') response._bodyDataFinished() protocol = AccumulatingProtocol() response.deliverBody(protocol) self.assertEqual(protocol.data, b'foobar') protocol.closedReason.trap(ResponseDone)
Example #2
Source File: test_tcp.py From learn_python3_spider with MIT License | 6 votes |
def test_serverRepr(self): """ Check that the repr string of the server transport get the good port number if the server listens on 0. """ server = MyServerFactory() serverConnMade = server.protocolConnectionMade = defer.Deferred() port = reactor.listenTCP(0, server) self.addCleanup(port.stopListening) client = MyClientFactory() clientConnMade = client.protocolConnectionMade = defer.Deferred() connector = reactor.connectTCP("127.0.0.1", port.getHost().port, client) self.addCleanup(connector.disconnect) def check(result): serverProto, clientProto = result portNumber = port.getHost().port self.assertEqual( repr(serverProto.transport), "<AccumulatingProtocol #0 on %s>" % (portNumber,)) serverProto.transport.loseConnection() clientProto.transport.loseConnection() return defer.gatherResults([serverConnMade, clientConnMade] ).addCallback(check)
Example #3
Source File: test_integration.py From autopush with Mozilla Public License 2.0 | 6 votes |
def _agent(method, url, contextFactory=None, headers=None, body=None): kwargs = {} if contextFactory: kwargs['contextFactory'] = contextFactory agent = Agent(reactor, **kwargs) rbody = None if body: rbody = FileBodyProducer(StringIO(body)) response = yield agent.request(method, url, headers=headers, bodyProducer=rbody) proto = AccumulatingProtocol() proto.closedDeferred = Deferred() response.deliverBody(proto) yield proto.closedDeferred returnValue((response, proto.data))
Example #4
Source File: test_tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_serverRepr(self): """ Check that the repr string of the server transport get the good port number if the server listens on 0. """ server = MyServerFactory() serverConnMade = server.protocolConnectionMade = defer.Deferred() port = reactor.listenTCP(0, server) self.addCleanup(port.stopListening) client = MyClientFactory() clientConnMade = client.protocolConnectionMade = defer.Deferred() connector = reactor.connectTCP("127.0.0.1", port.getHost().port, client) self.addCleanup(connector.disconnect) def check(result): serverProto, clientProto = result portNumber = port.getHost().port self.assertEqual( repr(serverProto.transport), "<AccumulatingProtocol #0 on %s>" % (portNumber,)) serverProto.transport.loseConnection() clientProto.transport.loseConnection() return defer.gatherResults([serverConnMade, clientConnMade] ).addCallback(check)
Example #5
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_finishedWithErrorWhenInitial(self): """ The L{Failure} passed to L{Response._bodyDataFinished} when the response is in the I{initial} state is passed to the C{connectionLost} method of the L{IProtocol} provider passed to the L{Response}'s C{deliverBody} method. """ transport = StringTransport() response = justTransportResponse(transport) # Sanity check - this test is for the initial state self.assertEqual(response._state, u'INITIAL') response._bodyDataFinished(Failure(ArbitraryException())) protocol = AccumulatingProtocol() response.deliverBody(protocol) protocol.closedReason.trap(ArbitraryException)
Example #6
Source File: test_newclient.py From python-for-android with Apache License 2.0 | 6 votes |
def test_bodyDataFinishedBeforeStartProducing(self): """ If the entire body is delivered to the L{Response} before the response's C{deliverBody} method is called, the protocol passed to C{deliverBody} is immediately given the body data and then disconnected. """ transport = StringTransport() response = justTransportResponse(transport) response._bodyDataReceived('foo') response._bodyDataReceived('bar') response._bodyDataFinished() protocol = AccumulatingProtocol() response.deliverBody(protocol) self.assertEqual(protocol.data, 'foobar') protocol.closedReason.trap(ResponseDone)
Example #7
Source File: test_newclient.py From python-for-android with Apache License 2.0 | 6 votes |
def test_finishedWithErrorWhenInitial(self): """ The L{Failure} passed to L{Response._bodyDataFinished} when the response is in the I{initial} state is passed to the C{connectionLost} method of the L{IProtocol} provider passed to the L{Response}'s C{deliverBody} method. """ transport = StringTransport() response = justTransportResponse(transport) # Sanity check - this test is for the initial state self.assertEqual(response._state, 'INITIAL') response._bodyDataFinished(Failure(ArbitraryException())) protocol = AccumulatingProtocol() response.deliverBody(protocol) protocol.closedReason.trap(ArbitraryException)
Example #8
Source File: test_tcp.py From python-for-android with Apache License 2.0 | 6 votes |
def test_serverRepr(self): """ Check that the repr string of the server transport get the good port number if the server listens on 0. """ server = MyServerFactory() serverConnMade = server.protocolConnectionMade = defer.Deferred() port = reactor.listenTCP(0, server) self.addCleanup(port.stopListening) client = MyClientFactory() clientConnMade = client.protocolConnectionMade = defer.Deferred() connector = reactor.connectTCP("127.0.0.1", port.getHost().port, client) self.addCleanup(connector.disconnect) def check((serverProto, clientProto)): portNumber = port.getHost().port self.assertEquals( repr(serverProto.transport), "<AccumulatingProtocol #0 on %s>" % (portNumber,)) serverProto.transport.loseConnection() clientProto.transport.loseConnection() return defer.gatherResults([serverConnMade, clientConnMade] ).addCallback(check)
Example #9
Source File: test_newclient.py From python-for-android with Apache License 2.0 | 5 votes |
def test_chunkedResponseBodyUnfinishedWhenConnectionLost(self): """ If the final chunk has not been received when the connection is lost (for any reason), the protocol passed to C{deliverBody} has its C{connectionLost} method called with a L{Failure} wrapping the exception for that reason. """ requestDeferred = self.protocol.request(Request('GET', '/', _boringHeaders, None)) self.protocol.dataReceived( "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "\r\n") result = [] requestDeferred.addCallback(result.append) response = result[0] protocol = AccumulatingProtocol() response.deliverBody(protocol) self.protocol.dataReceived("3\r\nfoo\r\n") self.protocol.dataReceived("3\r\nbar\r\n") self.assertEqual(protocol.data, "foobar") self.protocol.connectionLost(Failure(ArbitraryException())) return assertResponseFailed( self, fail(protocol.closedReason), [ArbitraryException, _DataLoss])
Example #10
Source File: test_newclient.py From python-for-android with Apache License 2.0 | 5 votes |
def test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknown( self): """ If the length of the response body is unknown, the protocol passed to the response's C{deliverBody} method has its C{connectionLost} method called with a L{Failure} wrapping a L{PotentialDataLoss} exception. """ requestDeferred = self.protocol.request(Request('GET', '/', _boringHeaders, None)) self.protocol.dataReceived( "HTTP/1.1 200 OK\r\n" "\r\n") result = [] requestDeferred.addCallback(result.append) response = result[0] protocol = AccumulatingProtocol() response.deliverBody(protocol) self.protocol.dataReceived("foo") self.protocol.dataReceived("bar") self.assertEqual(protocol.data, "foobar") self.protocol.connectionLost( Failure(ConnectionDone("low-level transport disconnected"))) protocol.closedReason.trap(PotentialDataLoss)
Example #11
Source File: test_tcp.py From python-for-android with Apache License 2.0 | 5 votes |
def setUp(self): self.f = f = MyServerFactory() self.f.protocolConnectionMade = defer.Deferred() self.p = p = reactor.listenTCP(0, f, interface="127.0.0.1") # XXX we don't test server side yet since we don't do it yet d = protocol.ClientCreator(reactor, AccumulatingProtocol).connectTCP( p.getHost().host, p.getHost().port) d.addCallback(self._gotClient) return d
Example #12
Source File: test_tcp.py From python-for-android with Apache License 2.0 | 5 votes |
def buildProtocol(self, addr): """ Create a L{AccumulatingProtocol} and set it up to be able to perform callbacks. """ self.peerAddresses.append(addr) self.called += 1 p = self.protocolFactory() p.factory = self p.closedDeferred = self.protocolConnectionLost self.protocolConnectionLost = None self.protocol = p return p
Example #13
Source File: test_newclient.py From python-for-android with Apache License 2.0 | 5 votes |
def test_receiveResponseBody(self): """ The C{deliverBody} method of the response object with which the L{Deferred} returned by L{HTTP11ClientProtocol.request} fires can be used to get the body of the response. """ protocol = AccumulatingProtocol() whenFinished = protocol.closedDeferred = Deferred() requestDeferred = self.protocol.request(Request('GET', '/', _boringHeaders, None)) self.protocol.dataReceived( "HTTP/1.1 200 OK\r\n" "Content-Length: 6\r\n" "\r") # Here's what's going on: all the response headers have been delivered # by this point, so the request Deferred can fire with a Response # object. The body is yet to come, but that's okay, because the # Response object is how you *get* the body. result = [] requestDeferred.addCallback(result.append) self.assertEqual(result, []) # Deliver the very last byte of the response. It is exactly at this # point which the Deferred returned by request should fire. self.protocol.dataReceived("\n") response = result[0] response.deliverBody(protocol) self.protocol.dataReceived("foo") self.protocol.dataReceived("bar") def cbAllResponse(ignored): self.assertEqual(protocol.data, "foobar") protocol.closedReason.trap(ResponseDone) whenFinished.addCallback(cbAllResponse) return whenFinished
Example #14
Source File: test_newclient.py From python-for-android with Apache License 2.0 | 5 votes |
def test_receiveResponseBeforeRequestGenerationDone(self): """ If response bytes are delivered to L{HTTP11ClientProtocol} before the L{Deferred} returned by L{Request.writeTo} fires, those response bytes are parsed as part of the response. """ request = SlowRequest() d = self.protocol.request(request) self.protocol.dataReceived( "HTTP/1.1 200 OK\r\n" "X-Foo: bar\r\n" "Content-Length: 6\r\n" "\r\n" "foobar") def cbResponse(response): p = AccumulatingProtocol() whenFinished = p.closedDeferred = Deferred() response.deliverBody(p) return whenFinished.addCallback( lambda ign: (response, p.data)) d.addCallback(cbResponse) def cbAllResponse((response, body)): self.assertEqual(response.version, ('HTTP', 1, 1)) self.assertEqual(response.code, 200) self.assertEqual(response.phrase, 'OK') self.assertEqual(response.headers, Headers({'x-foo': ['bar']})) self.assertEqual(body, "foobar") # Also nothing bad should happen if the request does finally # finish, even though it is completely irrelevant. request.finished.callback(None) d.addCallback(cbAllResponse) return d
Example #15
Source File: test_tcp.py From learn_python3_spider with MIT License | 5 votes |
def setUp(self): self.f = f = MyServerFactory() self.f.protocolConnectionMade = defer.Deferred() self.p = p = reactor.listenTCP(0, f, interface="127.0.0.1") # XXX we don't test server side yet since we don't do it yet d = protocol.ClientCreator(reactor, AccumulatingProtocol).connectTCP( p.getHost().host, p.getHost().port) d.addCallback(self._gotClient) return d
Example #16
Source File: test_tcp.py From python-for-android with Apache License 2.0 | 5 votes |
def buildProtocol(self, address): self.address = address self.protocol = AccumulatingProtocol() return self.protocol
Example #17
Source File: test_tcp.py From learn_python3_spider with MIT License | 5 votes |
def buildProtocol(self, address): self.address = address self.protocol = AccumulatingProtocol() return self.protocol
Example #18
Source File: test_distrib.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def _requestAgentTest(self, child, **kwargs): """ Set up a resource on a distrib site using L{ResourcePublisher} and then retrieve it from a L{ResourceSubscription} via an HTTP client. @param child: The resource to publish using distrib. @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when requesting the resource. @return: A L{Deferred} which fires with a tuple consisting of a L{twisted.test.proto_helpers.AccumulatingProtocol} containing the body of the response and an L{IResponse} with the response itself. """ mainPort, mainAddr = self._setupDistribServer(child) d = client.Agent(reactor).request(b"GET", "http://%s:%s/child" % ( mainAddr.host, mainAddr.port), **kwargs) def cbCollectBody(response): protocol = proto_helpers.AccumulatingProtocol() response.deliverBody(protocol) d = protocol.closedDeferred = defer.Deferred() d.addCallback(lambda _: (protocol, response)) return d d.addCallback(cbCollectBody) return d
Example #19
Source File: test_tcp.py From learn_python3_spider with MIT License | 5 votes |
def buildProtocol(self, addr): """ Create a L{AccumulatingProtocol} and set it up to be able to perform callbacks. """ self.peerAddresses.append(addr) self.called += 1 p = self.protocolFactory() p.factory = self p.closedDeferred = self.protocolConnectionLost self.protocolConnectionLost = None self.protocol = p return p
Example #20
Source File: test_distrib.py From learn_python3_spider with MIT License | 5 votes |
def _requestAgentTest(self, child, **kwargs): """ Set up a resource on a distrib site using L{ResourcePublisher} and then retrieve it from a L{ResourceSubscription} via an HTTP client. @param child: The resource to publish using distrib. @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when requesting the resource. @return: A L{Deferred} which fires with a tuple consisting of a L{twisted.test.proto_helpers.AccumulatingProtocol} containing the body of the response and an L{IResponse} with the response itself. """ mainPort, mainAddr = self._setupDistribServer(child) url = "http://{}:{}/child".format(mainAddr.host, mainAddr.port) url = url.encode("ascii") d = client.Agent(reactor).request(b"GET", url, **kwargs) def cbCollectBody(response): protocol = proto_helpers.AccumulatingProtocol() response.deliverBody(protocol) d = protocol.closedDeferred = defer.Deferred() d.addCallback(lambda _: (protocol, response)) return d d.addCallback(cbCollectBody) return d
Example #21
Source File: test_tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def setUp(self): self.f = f = MyServerFactory() self.f.protocolConnectionMade = defer.Deferred() self.p = p = reactor.listenTCP(0, f, interface="127.0.0.1") # XXX we don't test server side yet since we don't do it yet d = protocol.ClientCreator(reactor, AccumulatingProtocol).connectTCP( p.getHost().host, p.getHost().port) d.addCallback(self._gotClient) return d
Example #22
Source File: test_tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def buildProtocol(self, address): self.address = address self.protocol = AccumulatingProtocol() return self.protocol
Example #23
Source File: test_tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def buildProtocol(self, addr): """ Create a L{AccumulatingProtocol} and set it up to be able to perform callbacks. """ self.peerAddresses.append(addr) self.called += 1 p = self.protocolFactory() p.factory = self p.closedDeferred = self.protocolConnectionLost self.protocolConnectionLost = None self.protocol = p return p
Example #24
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_quiescentCallbackThrows(self): """ If C{quiescentCallback} throws an exception, the error is logged and protocol is disconnected. """ def callback(p): raise ZeroDivisionError() transport = StringTransport() protocol = HTTP11ClientProtocol(callback) protocol.makeConnection(transport) requestDeferred = protocol.request( Request(b'GET', b'/', _boringHeaders, None, persistent=True)) protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"Content-length: 0\r\n" b"\r\n") result = [] requestDeferred.addCallback(result.append) response = result[0] bodyProtocol = AccumulatingProtocol() response.deliverBody(bodyProtocol) bodyProtocol.closedReason.trap(ResponseDone) errors = self.flushLoggedErrors(ZeroDivisionError) self.assertEqual(len(errors), 1) self.assertTrue(transport.disconnecting)
Example #25
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_quiescentCallbackNotCalledNonPersistentQuery(self): """ If the request was non-persistent (i.e. sent C{Connection: close}), the C{quiescentCallback} is not called and the connection is lost. """ quiescentResult = [] transport = StringTransport() protocol = HTTP11ClientProtocol(quiescentResult.append) protocol.makeConnection(transport) requestDeferred = protocol.request( Request(b'GET', b'/', _boringHeaders, None, persistent=False)) protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"Content-length: 0\r\n" b"\r\n") result = [] requestDeferred.addCallback(result.append) response = result[0] bodyProtocol = AccumulatingProtocol() response.deliverBody(bodyProtocol) bodyProtocol.closedReason.trap(ResponseDone) self.assertEqual(quiescentResult, []) self.assertTrue(transport.disconnecting)
Example #26
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_quiescentCallbackNotCalled(self): """ If after a response is done the {HTTP11ClientProtocol} returns a C{Connection: close} header in the response, the C{quiescentCallback} is not called and the connection is lost. """ quiescentResult = [] transport = StringTransport() protocol = HTTP11ClientProtocol(quiescentResult.append) protocol.makeConnection(transport) requestDeferred = protocol.request( Request(b'GET', b'/', _boringHeaders, None, persistent=True)) protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"Content-length: 0\r\n" b"Connection: close\r\n" b"\r\n") result = [] requestDeferred.addCallback(result.append) response = result[0] bodyProtocol = AccumulatingProtocol() response.deliverBody(bodyProtocol) bodyProtocol.closedReason.trap(ResponseDone) self.assertEqual(quiescentResult, []) self.assertTrue(transport.disconnecting)
Example #27
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknown( self): """ If the length of the response body is unknown, the protocol passed to the response's C{deliverBody} method has its C{connectionLost} method called with a L{Failure} wrapping a L{PotentialDataLoss} exception. """ requestDeferred = self.protocol.request(Request(b'GET', b'/', _boringHeaders, None)) self.protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"\r\n") result = [] requestDeferred.addCallback(result.append) response = result[0] protocol = AccumulatingProtocol() response.deliverBody(protocol) self.protocol.dataReceived(b"foo") self.protocol.dataReceived(b"bar") self.assertEqual(protocol.data, b"foobar") self.protocol.connectionLost( Failure(ConnectionDone(u"low-level transport disconnected"))) protocol.closedReason.trap(PotentialDataLoss)
Example #28
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_receiveResponseBody(self): """ The C{deliverBody} method of the response object with which the L{Deferred} returned by L{HTTP11ClientProtocol.request} fires can be used to get the body of the response. """ protocol = AccumulatingProtocol() whenFinished = protocol.closedDeferred = Deferred() requestDeferred = self.protocol.request(Request(b'GET', b'/', _boringHeaders, None)) self.protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"Content-Length: 6\r\n" b"\r") # Here's what's going on: all the response headers have been delivered # by this point, so the request Deferred can fire with a Response # object. The body is yet to come, but that's okay, because the # Response object is how you *get* the body. result = [] requestDeferred.addCallback(result.append) self.assertEqual(result, []) # Deliver the very last byte of the response. It is exactly at this # point which the Deferred returned by request should fire. self.protocol.dataReceived(b"\n") response = result[0] response.deliverBody(protocol) self.protocol.dataReceived(b"foo") self.protocol.dataReceived(b"bar") def cbAllResponse(ignored): self.assertEqual(protocol.data, b"foobar") protocol.closedReason.trap(ResponseDone) whenFinished.addCallback(cbAllResponse) return whenFinished
Example #29
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone(self): """ If response bytes are delivered to L{HTTP11ClientProtocol} before the request completes, calling C{connectionLost} on the protocol will result in protocol being moved to C{'CONNECTION_LOST'} state. """ request = SlowRequest() d = self.protocol.request(request) self.protocol.dataReceived( b"HTTP/1.1 400 BAD REQUEST\r\n" b"Content-Length: 9\r\n" b"\r\n" b"tisk tisk") def cbResponse(response): p = AccumulatingProtocol() whenFinished = p.closedDeferred = Deferred() response.deliverBody(p) return whenFinished.addCallback( lambda ign: (response, p.data)) d.addCallback(cbResponse) def cbAllResponse(ignore): request.finished.callback(None) # Nothing dire will happen when the connection is lost self.protocol.connectionLost(Failure(ArbitraryException())) self.assertEqual(self.protocol._state, u'CONNECTION_LOST') d.addCallback(cbAllResponse) return d
Example #30
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 4 votes |
def test_quiescentCallbackCalled(self): """ If after a response is done the {HTTP11ClientProtocol} stays open and returns to QUIESCENT state, all per-request state is reset and the C{quiescentCallback} is called with the protocol instance. This is useful for implementing a persistent connection pool. The C{quiescentCallback} is called *before* the response-receiving protocol's C{connectionLost}, so that new requests triggered by end of first request can re-use a persistent connection. """ quiescentResult = [] def callback(p): self.assertEqual(p, protocol) self.assertEqual(p.state, u"QUIESCENT") quiescentResult.append(p) transport = StringTransport() protocol = HTTP11ClientProtocol(callback) protocol.makeConnection(transport) requestDeferred = protocol.request( Request(b'GET', b'/', _boringHeaders, None, persistent=True)) protocol.dataReceived( b"HTTP/1.1 200 OK\r\n" b"Content-length: 3\r\n" b"\r\n") # Headers done, but still no quiescent callback: self.assertEqual(quiescentResult, []) result = [] requestDeferred.addCallback(result.append) response = result[0] # When response body is done (i.e. connectionLost is called), note the # fact in quiescentResult: bodyProtocol = AccumulatingProtocol() bodyProtocol.closedDeferred = Deferred() bodyProtocol.closedDeferred.addCallback( lambda ign: quiescentResult.append(u"response done")) response.deliverBody(bodyProtocol) protocol.dataReceived(b"abc") bodyProtocol.closedReason.trap(ResponseDone) # Quiescent callback called *before* protocol handling the response # body gets its connectionLost called: self.assertEqual(quiescentResult, [protocol, u"response done"]) # Make sure everything was cleaned up: self.assertEqual(protocol._parser, None) self.assertEqual(protocol._finishedRequest, None) self.assertEqual(protocol._currentRequest, None) self.assertEqual(protocol._transportProxy, None) self.assertEqual(protocol._responseDeferred, None)