本文整理汇总了Python中twisted.test.proto_helpers.AccumulatingProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python proto_helpers.AccumulatingProtocol方法的具体用法?Python proto_helpers.AccumulatingProtocol怎么用?Python proto_helpers.AccumulatingProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.test.proto_helpers
的用法示例。
在下文中一共展示了proto_helpers.AccumulatingProtocol方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bodyDataFinishedBeforeStartProducing
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例2: test_finishedWithErrorWhenInitial
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例3: test_serverRepr
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例4: _agent
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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))
示例5: test_bodyDataFinishedBeforeStartProducing
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例6: test_finishedWithErrorWhenInitial
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例7: test_serverRepr
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例8: _requestAgentTest
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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
示例9: test_connectionLostAfterReceivingResponseBeforeRequestGenerationDone
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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
示例10: test_receiveResponseBody
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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
示例11: test_responseBodyFinishedWhenConnectionLostWhenContentLengthIsUnknown
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例12: test_quiescentCallbackNotCalled
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例13: test_quiescentCallbackNotCalledNonPersistentQuery
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例14: test_quiescentCallbackThrows
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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)
示例15: buildProtocol
# 需要导入模块: from twisted.test import proto_helpers [as 别名]
# 或者: from twisted.test.proto_helpers import AccumulatingProtocol [as 别名]
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