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