本文整理汇总了Python中twisted.test.proto_helpers.StringTransportWithDisconnection类的典型用法代码示例。如果您正苦于以下问题:Python StringTransportWithDisconnection类的具体用法?Python StringTransportWithDisconnection怎么用?Python StringTransportWithDisconnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StringTransportWithDisconnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_renderProtocol
def test_renderProtocol(self):
"""
If protocols are specified via the C{Sec-WebSocket-Protocol} header,
L{WebSocketsResource} passes them to its C{lookupProtocol} argument,
which can decide which protocol to return, and which is accepted.
"""
def lookupProtocol(names, otherRequest):
self.assertEqual(["foo", "bar"], names)
self.assertIdentical(request, otherRequest)
return self.echoProtocol, "bar"
self.resource = WebSocketsResource(lookupProtocol)
request = DummyRequest("/")
request.requestHeaders = Headers(
{"sec-websocket-protocol": ["foo", "bar"]})
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertEqual(
{"connection": "Upgrade",
"upgrade": "WebSocket",
"sec-websocket-protocol": "bar",
"sec-websocket-accept": "oYBv54i42V5dw6KnZqOFroecUTc="},
request.outgoingHeaders)
self.assertEqual([""], request.written)
self.assertEqual(101, request.responseCode)
示例2: CommandFailureTests
class CommandFailureTests(CommandMixin, TestCase):
"""
Tests for correct failure of commands on a disconnected
L{MemCacheProtocol}.
"""
def setUp(self):
"""
Create a disconnected memcache client, using a deterministic clock.
"""
self.proto = MemCacheProtocol()
self.clock = Clock()
self.proto.callLater = self.clock.callLater
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
self.transport.loseConnection()
def _test(self, d, send, recv, result):
"""
Implementation of C{_test} which checks that the command fails with
C{RuntimeError} because the transport is disconnected. All the
parameters except C{d} are ignored.
"""
return self.assertFailure(d, RuntimeError)
示例3: test_render
def test_render(self):
"""
When rendering a request, L{WebSocketsResource} uses the
C{Sec-WebSocket-Key} header to generate a C{Sec-WebSocket-Accept}
value. It creates a L{WebSocketsProtocol} instance connected to the
protocol provided by the user factory.
"""
request = DummyRequest("/")
request.requestHeaders = Headers()
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertEqual(
{"connection": "Upgrade",
"upgrade": "WebSocket",
"sec-websocket-accept": "oYBv54i42V5dw6KnZqOFroecUTc="},
request.outgoingHeaders)
self.assertEqual([""], request.written)
self.assertEqual(101, request.responseCode)
self.assertIdentical(None, request.transport)
self.assertIsInstance(transport.protocol._receiver,
SavingEchoReceiver)
示例4: test_connectToNode
def test_connectToNode(self):
"""
L{OneShotPortMapperFactory.connectToNode} should return a L{Deferred}
that will be called back with the instance of the protocol connected
to the erlang node.
"""
clientFactory = DummyClientFactory()
self.factory.nodeFactoryClass = lambda a, b, c: clientFactory
d = self.factory.connectToNode("[email protected]")
transport = StringTransportWithDisconnection()
proto = self.factory.buildProtocol(("127.0.01", 4369))
proto.makeConnection(transport)
transport.protocol = proto
self.assertEqual(transport.value(), "\x00\x04zegg")
self.assertEqual(
self.factory.connect, [("127.0.0.1", 4369, self.factory)])
proto.dataReceived(
"w\x00\x00\x09M\x01\x00\x05\x00\x05\x00\x03bar\x00")
clientProto = object()
clientFactory._connectDeferred.callback(clientProto)
self.assertEqual(
self.factory.connect,
[("127.0.0.1", 4369, self.factory),
("127.0.0.1", 9, clientFactory)])
return d.addCallback(self.assertIdentical, clientProto)
示例5: test_getNodeConnection
def test_getNodeConnection(self):
"""
L{Process._getNodeConnection}, if no connection is established, as a
connection to a L{OneShotPortMapperFactory}. Once it gets a connection
it sets the calls handler to the client factory to the process handler.
"""
d = self.process._getNodeConnection("[email protected]")
epmd = self.process.oneShotEpmds["spam"]
transport = StringTransportWithDisconnection()
proto = epmd.buildProtocol(("127.0.01", 4369))
proto.makeConnection(transport)
transport.protocol = proto
self.assertEqual(transport.value(), "\x00\x04zegg")
self.assertEqual(epmd.connect, [("spam", 4369, epmd)])
proto.dataReceived(
"w\x00\x00\x09M\x01\x00\x05\x00\x05\x00\x03bar\x00")
[factory] = epmd.factories
self.assertEqual(
epmd.factoriesArgs,
[("[email protected]", "test_cookie", epmd.onConnectionLost)])
clientProto = TestableNodeProtocol()
clientProto.factory = factory
factory._connectDeferred.callback(clientProto)
def check(proto):
self.assertIdentical(proto, clientProto)
self.assertIdentical(factory.handler, self.process.handler)
return d.addCallback(check)
示例6: test_writeLimit
def test_writeLimit(self):
"""
Check the writeLimit parameter: write data, and check for the pause
status.
"""
server = Server()
tServer = TestableThrottlingFactory(task.Clock(), server, writeLimit=10)
port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0))
tr = StringTransportWithDisconnection()
tr.protocol = port
port.makeConnection(tr)
port.producer = port.wrappedProtocol
port.dataReceived("0123456789")
port.dataReceived("abcdefghij")
self.assertEqual(tr.value(), "0123456789abcdefghij")
self.assertEqual(tServer.writtenThisSecond, 20)
self.assertFalse(port.wrappedProtocol.paused)
# at this point server should've written 20 bytes, 10 bytes
# above the limit so writing should be paused around 1 second
# from 'now', and resumed a second after that
tServer.clock.advance(1.05)
self.assertEqual(tServer.writtenThisSecond, 0)
self.assertTrue(port.wrappedProtocol.paused)
tServer.clock.advance(1.05)
self.assertEqual(tServer.writtenThisSecond, 0)
self.assertFalse(port.wrappedProtocol.paused)
示例7: test_renderSecureRequest
def test_renderSecureRequest(self):
"""
When the rendered request is over HTTPS, L{WebSocketsResource} wraps
the protocol of the C{TLSMemoryBIOProtocol} instance.
"""
request = DummyRequest("/")
request.requestHeaders = Headers()
transport = StringTransportWithDisconnection()
secureProtocol = TLSMemoryBIOProtocol(Factory(), Protocol())
transport.protocol = secureProtocol
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertEqual(
{"connection": "Upgrade",
"upgrade": "WebSocket",
"sec-websocket-accept": "oYBv54i42V5dw6KnZqOFroecUTc="},
request.outgoingHeaders)
self.assertEqual([""], request.written)
self.assertEqual(101, request.responseCode)
self.assertIdentical(None, request.transport)
self.assertIsInstance(
transport.protocol.wrappedProtocol, WebSocketsProtocol)
self.assertIsInstance(
transport.protocol.wrappedProtocol._receiver,
SavingEchoReceiver)
示例8: test_renderIProtocol
def test_renderIProtocol(self):
"""
If the protocol returned by C{lookupProtocol} isn't a
C{WebSocketsProtocol}, L{WebSocketsResource} wraps it automatically
with L{WebSocketsProtocolWrapper}.
"""
def lookupProtocol(names, otherRequest):
return AccumulatingProtocol(), None
self.resource = WebSocketsResource(lookupProtocol)
request = DummyRequest("/")
request.requestHeaders = Headers()
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
request.transport = transport
request.headers.update({
"upgrade": "Websocket",
"connection": "Upgrade",
"sec-websocket-key": "secure",
"sec-websocket-version": "13"})
result = self.resource.render(request)
self.assertEqual(NOT_DONE_YET, result)
self.assertIsInstance(transport.protocol, WebSocketsProtocolWrapper)
self.assertIsInstance(transport.protocol.wrappedProtocol,
AccumulatingProtocol)
示例9: ProtocolTestCase
class ProtocolTestCase(unittest.TestCase):
def setUp(self):
self.proto = Redis()
self.transport = StringTransportWithDisconnection()
self.transport.protocol = self.proto
self.proto.makeConnection(self.transport)
def sendResponse(self, data):
self.proto.dataReceived(data)
def test_error_response(self):
# pretending 'foo' is a set, so get is incorrect
d = self.proto.get("foo")
self.assertEquals(
self.transport.value(),
'*2\r\n$3\r\nGET\r\n$3\r\nfoo\r\n')
msg = "Operation against a key holding the wrong kind of value"
self.sendResponse("-%s\r\n" % msg)
self.failUnlessFailure(d, ResponseError)
def check_err(r):
self.assertEquals(str(r), msg)
return d
@defer.inlineCallbacks
def test_singleline_response(self):
d = self.proto.ping()
self.assertEquals(self.transport.value(), '*1\r\n$4\r\nPING\r\n')
self.sendResponse("+PONG\r\n")
r = yield d
self.assertEquals(r, 'PONG')
@defer.inlineCallbacks
def test_bulk_response(self):
d = self.proto.get("foo")
self.assertEquals(
self.transport.value(),
'*2\r\n$3\r\nGET\r\n$3\r\nfoo\r\n')
self.sendResponse("$3\r\nbar\r\n")
r = yield d
self.assertEquals(r, 'bar')
@defer.inlineCallbacks
def test_multibulk_response(self):
d = self.proto.lrange("foo", 0, 1)
expected = '*4\r\n$6\r\nLRANGE\r\n$3\r\nfoo\r\n$1\r\n0\r\n$1\r\n1\r\n'
self.assertEquals(self.transport.value(), expected)
self.sendResponse("*2\r\n$3\r\nbar\r\n$6\r\nlolwut\r\n")
r = yield d
self.assertEquals(r, ['bar', 'lolwut'])
@defer.inlineCallbacks
def test_integer_response(self):
d = self.proto.dbsize()
self.assertEquals(self.transport.value(), '*1\r\n$6\r\nDBSIZE\r\n')
self.sendResponse(":1234\r\n")
r = yield d
self.assertEquals(r, 1234)
示例10: TestProtocol
class TestProtocol(common.TestCase):
def setUp(self):
self.transport = StringTransportWithDisconnection()
self.protocol = httpclient.Protocol(self, owner=None)
self.protocol.factory = MockFactory()
self.protocol.makeConnection(self.transport)
self.transport.protocol = self.protocol
self.addCleanup(self._disconnect_protocol)
@defer.inlineCallbacks
def testSimpleRequest(self):
self.assertTrue(self.protocol.factory.onConnectionMade_called)
self.assertTrue(self.protocol.is_idle())
d = self.protocol.request(http.Methods.GET, '/',
headers={'accept': 'text/html'})
self.assertEqual('GET / HTTP/1.1\r\n'
'Accept: text/html\r\n\r\n', self.transport.value())
self.assertFalse(self.protocol.is_idle())
self.protocol.dataReceived(
self.protocol.delimiter.join([
"HTTP/1.1 200 OK",
"Content-Type: text/html",
"Content-Length: 12",
"",
"This is body",
]))
response = yield d
self.assertIsInstance(response, httpclient.Response)
self.assertEqual(200, response.status)
self.assertEqual({'content-type': 'text/html',
'content-length': '12'}, response.headers)
self.assertEqual('This is body', response.body)
self.assertTrue(self.protocol.is_idle())
self.assertTrue(self.protocol.factory.onConnectionReset_called)
@defer.inlineCallbacks
def testCancelledRequest(self):
d = self.protocol.request(http.Methods.GET, '/',
headers={'accept': 'text/html'})
d.cancel()
self.assertFalse(self.transport.connected)
self.assertFailure(d, httpclient.RequestCancelled)
f = yield d
exp = ('GET to http://10.0.0.1:12345/ was cancelled '
'0.(\d+)s after it was sent.')
self.assertTrue(re.match(exp, str(f)), str(f))
def _disconnect_protocol(self):
if self.transport.connected:
self.transport.loseConnection()
self.assertTrue(self.protocol.factory.onConnectionLost_called)
示例11: connect
def connect(self, protocolFactory):
string_transport = StringTransportWithDisconnection()
protocol = protocolFactory.buildProtocol(None)
protocol.makeConnection(string_transport)
string_transport.protocol = protocol
self.connect_callback(protocol)
d = Deferred()
d.callback(protocol)
return d
示例12: connect_transport
def connect_transport(protocol, factory=None):
""" Connect a StringTransport to a client protocol. """
if factory is None:
factory = ClientFactory()
transport = StringTransportWithDisconnection()
protocol.makeConnection(transport)
transport.protocol = protocol
protocol.factory = factory
return transport
示例13: test_loseConnectionCodeAndReason
def test_loseConnectionCodeAndReason(self):
"""
L{WebSocketsTransport.loseConnection} accepts a code and a reason which
are used to build the closing frame.
"""
transport = StringTransportWithDisconnection()
transport.protocol = Protocol()
webSocketsTranport = WebSocketsTransport(transport)
webSocketsTranport.loseConnection(STATUSES.GOING_AWAY, "Going away")
self.assertEqual("\x88\x0c\x03\xe9Going away", transport.value())
示例14: gen_prot
def gen_prot(self, app):
from spyne.server.twisted.msgpack import TwistedMessagePackProtocol
from twisted.test.proto_helpers import StringTransportWithDisconnection
prot = TwistedMessagePackProtocol(app)
transport = StringTransportWithDisconnection()
prot.makeConnection(transport)
transport.protocol = prot
return prot
示例15: test_connection
def test_connection(self):
"""
On connection, the EPMD client should send an ALIVE2 request.
"""
self.factory.nodePortNumber = 1234
transport = StringTransportWithDisconnection()
proto = self.factory.buildProtocol(("127.0.01", 4369))
proto.makeConnection(transport)
transport.protocol = proto
self.assertEquals(transport.value(),
"\x00\x10x\x04\xd2H\x00\x00\x05\x00\x05\x00\x03foo\x00\x00")