Python twisted.test.proto_helpers.StringTransportWithDisconnection() Examples
The following are 30
code examples of twisted.test.proto_helpers.StringTransportWithDisconnection().
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_proxy.py From python-for-android with Apache License 2.0 | 6 votes |
def test_processWithPort(self): """ Check that L{ProxyRequest.process} correctly parse port in the incoming URL, and create a outgoing connection with this port. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ProxyRequest(channel, False, reactor) request.gotLength(0) request.requestReceived('GET', 'http://example.com:1234/foo/bar', 'HTTP/1.0') # That should create one connection, with the port parsed from the URL self.assertEquals(len(reactor.tcpClients), 1) self.assertEquals(reactor.tcpClients[0][0], "example.com") self.assertEquals(reactor.tcpClients[0][1], 1234)
Example #2
Source File: test_imap.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_idleClientDoesDisconnect(self): """ The *server* has a timeout mechanism which will close connections that are inactive for a period. """ c = Clock() # Hook up our server protocol transport = StringTransportWithDisconnection() transport.protocol = self.server self.server.callLater = c.callLater self.server.makeConnection(transport) # Make sure we can notice when the connection goes away lost = [] connLost = self.server.connectionLost self.server.connectionLost = lambda reason: (lost.append(None), connLost(reason))[1] # 2/3rds of the idle timeout elapses... c.pump([0.0] + [self.server.timeOut / 3.0] * 2) self.assertFalse(lost, lost) # Now some more c.pump([0.0, self.server.timeOut / 2.0]) self.assertTrue(lost)
Example #3
Source File: test_ftp.py From learn_python3_spider with MIT License | 6 votes |
def test_errbacksUponDisconnect(self): """ Test the ftp command errbacks when a connection lost happens during the operation. """ ftpClient = ftp.FTPClient() tr = proto_helpers.StringTransportWithDisconnection() ftpClient.makeConnection(tr) tr.protocol = ftpClient d = ftpClient.list('some path', Dummy()) m = [] def _eb(failure): m.append(failure) return None d.addErrback(_eb) from twisted.internet.main import CONNECTION_LOST ftpClient.connectionLost(failure.Failure(CONNECTION_LOST)) self.assertTrue(m, m) return d
Example #4
Source File: test_policies.py From learn_python3_spider with MIT License | 6 votes |
def getProtocolAndClock(self): """ Helper to set up an already connected protocol to be tested. @return: A new protocol with its attached clock. @rtype: Tuple of (L{policies.TimeoutProtocol}, L{task.Clock}) """ clock = task.Clock() wrappedFactory = protocol.ServerFactory() wrappedFactory.protocol = SimpleProtocol factory = TestableTimeoutFactory(clock, wrappedFactory, None) proto = factory.buildProtocol( address.IPv4Address('TCP', '127.0.0.1', 12345)) transport = StringTransportWithDisconnection() transport.protocol = proto proto.makeConnection(transport) return (proto, clock)
Example #5
Source File: test_wrapper.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_validUNIXHeaderResolves_getPeerHost(self): """ Test if UNIX headers result in the correct host and peer data. """ factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol)) proto = factory.buildProtocol( address.UNIXAddress(b'/home/test/sockets/server.sock'), ) transport = StringTransportWithDisconnection() proto.makeConnection(transport) proto.dataReceived(self.UNIXHEADER) self.assertEqual(proto.getPeer().name, b'/home/tests/mysockets/sock') self.assertEqual( proto.wrappedProtocol.transport.getPeer().name, b'/home/tests/mysockets/sock', ) self.assertEqual(proto.getHost().name, b'/home/tests/mysockets/sock') self.assertEqual( proto.wrappedProtocol.transport.getHost().name, b'/home/tests/mysockets/sock', )
Example #6
Source File: test_proxy.py From learn_python3_spider with MIT License | 6 votes |
def test_processWithPort(self): """ Check that L{ProxyRequest.process} correctly parse port in the incoming URL, and create an outgoing connection with this port. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ProxyRequest(channel, False, reactor) request.gotLength(0) request.requestReceived(b'GET', b'http://example.com:1234/foo/bar', b'HTTP/1.0') # That should create one connection, with the port parsed from the URL self.assertEqual(len(reactor.tcpClients), 1) self.assertEqual(reactor.tcpClients[0][0], u"example.com") self.assertEqual(reactor.tcpClients[0][1], 1234)
Example #7
Source File: test_proxy.py From learn_python3_spider with MIT License | 6 votes |
def test_process(self): """ L{ReverseProxyRequest.process} should create a connection to its factory host/port, using a L{ProxyClientFactory} instantiated with the correct parameters, and particularly set the B{host} header to the factory host. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ReverseProxyRequest(channel, False, reactor) request.factory = DummyFactory(u"example.com", 1234) request.gotLength(0) request.requestReceived(b'GET', b'/foo/bar', b'HTTP/1.0') # Check that one connection has been created, to the good host/port self.assertEqual(len(reactor.tcpClients), 1) self.assertEqual(reactor.tcpClients[0][0], u"example.com") self.assertEqual(reactor.tcpClients[0][1], 1234) # Check the factory passed to the connect, and its headers factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEqual(factory.headers, {b'host': b'example.com'})
Example #8
Source File: test_ftp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_errbacksUponDisconnect(self): """ Test the ftp command errbacks when a connection lost happens during the operation. """ ftpClient = ftp.FTPClient() tr = proto_helpers.StringTransportWithDisconnection() ftpClient.makeConnection(tr) tr.protocol = ftpClient d = ftpClient.list('some path', Dummy()) m = [] def _eb(failure): m.append(failure) return None d.addErrback(_eb) from twisted.internet.main import CONNECTION_LOST ftpClient.connectionLost(failure.Failure(CONNECTION_LOST)) self.assertTrue(m, m) return d
Example #9
Source File: test_policies.py From learn_python3_spider with MIT License | 6 votes |
def test_breakReferenceCycle(self): """ L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to C{None} in order to break reference cycle between wrapper and wrapped protocols. :return: """ wrapper = policies.ProtocolWrapper(policies.WrappingFactory(Server()), protocol.Protocol()) transport = StringTransportWithDisconnection() transport.protocol = wrapper wrapper.makeConnection(transport) self.assertIsNotNone(wrapper.wrappedProtocol) transport.loseConnection() self.assertIsNone(wrapper.wrappedProtocol)
Example #10
Source File: test_proxy.py From python-for-android with Apache License 2.0 | 6 votes |
def test_process(self): """ L{ReverseProxyRequest.process} should create a connection to its factory host/port, using a L{ProxyClientFactory} instantiated with the correct parameters, and particulary set the B{host} header to the factory host. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ReverseProxyRequest(channel, False, reactor) request.factory = DummyFactory("example.com", 1234) request.gotLength(0) request.requestReceived('GET', '/foo/bar', 'HTTP/1.0') # Check that one connection has been created, to the good host/port self.assertEquals(len(reactor.tcpClients), 1) self.assertEquals(reactor.tcpClients[0][0], "example.com") self.assertEquals(reactor.tcpClients[0][1], 1234) # Check the factory passed to the connect, and its headers factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEquals(factory.headers, {'host': 'example.com'})
Example #11
Source File: test_jabberxmlstream.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def setUp(self): """ Set up XmlStream and several observers. """ self.gotStreamStart = False self.gotStreamEnd = False self.gotStreamError = False xs = xmlstream.XmlStream(xmlstream.Authenticator()) xs.addObserver('//event/stream/start', self.onStreamStart) xs.addObserver('//event/stream/end', self.onStreamEnd) xs.addObserver('//event/stream/error', self.onStreamError) xs.makeConnection(proto_helpers.StringTransportWithDisconnection()) xs.transport.protocol = xs xs.namespace = 'testns' xs.version = (1, 0) self.xmlstream = xs
Example #12
Source File: test_proxy.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_process(self): """ L{ReverseProxyRequest.process} should create a connection to its factory host/port, using a L{ProxyClientFactory} instantiated with the correct parameters, and particularly set the B{host} header to the factory host. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ReverseProxyRequest(channel, False, reactor) request.factory = DummyFactory(u"example.com", 1234) request.gotLength(0) request.requestReceived(b'GET', b'/foo/bar', b'HTTP/1.0') # Check that one connection has been created, to the good host/port self.assertEqual(len(reactor.tcpClients), 1) self.assertEqual(reactor.tcpClients[0][0], u"example.com") self.assertEqual(reactor.tcpClients[0][1], 1234) # Check the factory passed to the connect, and its headers factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEqual(factory.headers, {b'host': b'example.com'})
Example #13
Source File: test_proxy.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_processWithPort(self): """ Check that L{ProxyRequest.process} correctly parse port in the incoming URL, and create an outgoing connection with this port. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ProxyRequest(channel, False, reactor) request.gotLength(0) request.requestReceived(b'GET', b'http://example.com:1234/foo/bar', b'HTTP/1.0') # That should create one connection, with the port parsed from the URL self.assertEqual(len(reactor.tcpClients), 1) self.assertEqual(reactor.tcpClients[0][0], u"example.com") self.assertEqual(reactor.tcpClients[0][1], 1234)
Example #14
Source File: test_imap.py From python-for-android with Apache License 2.0 | 6 votes |
def test_idleClientDoesDisconnect(self): """ The *server* has a timeout mechanism which will close connections that are inactive for a period. """ c = Clock() # Hook up our server protocol transport = StringTransportWithDisconnection() transport.protocol = self.server self.server.callLater = c.callLater self.server.makeConnection(transport) # Make sure we can notice when the connection goes away lost = [] connLost = self.server.connectionLost self.server.connectionLost = lambda reason: (lost.append(None), connLost(reason))[1] # 2/3rds of the idle timeout elapses... c.pump([0.0] + [self.server.timeOut / 3.0] * 2) self.failIf(lost, lost) # Now some more c.pump([0.0, self.server.timeOut / 2.0]) self.failUnless(lost)
Example #15
Source File: test_service.py From python-for-android with Apache License 2.0 | 5 votes |
def __init__(self, user, realm, factory, address=address.IPv4Address('TCP', '127.0.0.1', 54321)): self.user = user self.transport = proto_helpers.StringTransportWithDisconnection() self.protocol = factory.buildProtocol(address) self.transport.protocol = self.protocol self.user.mind = self.protocol self.protocol.makeConnection(self.transport)
Example #16
Source File: test_ftp.py From learn_python3_spider with MIT License | 5 votes |
def test_lostRETR(self): """ Try a RETR, but disconnect during the transfer. L{ftp.FTPClient.retrieveFile} should return a Deferred which errbacks with L{ftp.ConnectionLost) """ self.client.passive = False l = [] def generatePort(portCmd): portCmd.text = 'PORT %s' % (ftp.encodeHostPort('127.0.0.1', 9876),) tr = proto_helpers.StringTransportWithDisconnection() portCmd.protocol.makeConnection(tr) tr.protocol = portCmd.protocol portCmd.protocol.dataReceived(b"x" * 500) l.append(tr) self.client.generatePortCommand = generatePort self._testLogin() proto = _BufferingProtocol() d = self.client.retrieveFile("spam", proto) self.assertEqual(self.transport.value(), ('PORT %s\r\n' % (ftp.encodeHostPort('127.0.0.1', 9876),)).encode( self.client._encoding)) self.transport.clear() self.client.lineReceived(b'200 PORT OK') self.assertEqual(self.transport.value(), b'RETR spam\r\n') self.assertTrue(l) l[0].loseConnection() self.transport.loseConnection() self.assertFailure(d, ftp.ConnectionLost) return d
Example #17
Source File: test_proxy.py From python-for-android with Apache License 2.0 | 5 votes |
def _testRender(self, uri, expectedURI): """ Check that a request pointing at C{uri} produce a new proxy connection, with the path of this request pointing at C{expectedURI}. """ root = Resource() reactor = MemoryReactor() resource = ReverseProxyResource("127.0.0.1", 1234, "/path", reactor) root.putChild('index', resource) site = Site(root) transport = StringTransportWithDisconnection() channel = site.buildProtocol(None) channel.makeConnection(transport) # Clear the timeout if the tests failed self.addCleanup(channel.connectionLost, None) channel.dataReceived("GET %s HTTP/1.1\r\nAccept: text/html\r\n\r\n" % (uri,)) # Check that one connection has been created, to the good host/port self.assertEquals(len(reactor.tcpClients), 1) self.assertEquals(reactor.tcpClients[0][0], "127.0.0.1") self.assertEquals(reactor.tcpClients[0][1], 1234) # Check the factory passed to the connect, and its given path factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEquals(factory.rest, expectedURI) self.assertEquals(factory.headers["host"], "127.0.0.1:1234")
Example #18
Source File: test_ftp.py From learn_python3_spider with MIT License | 5 votes |
def setUp(self): """ Create a FTP client and connect it to fake transport. """ self.client = ftp.FTPClient() self.transport = proto_helpers.StringTransportWithDisconnection() self.client.makeConnection(self.transport) self.transport.protocol = self.client
Example #19
Source File: test_proxy.py From python-for-android with Apache License 2.0 | 5 votes |
def _testProcess(self, uri, expectedURI, method="GET", data=""): """ Build a request pointing at C{uri}, and check that a proxied request is created, pointing a C{expectedURI}. """ transport = StringTransportWithDisconnection() channel = DummyChannel(transport) reactor = MemoryReactor() request = ProxyRequest(channel, False, reactor) request.gotLength(len(data)) request.handleContentChunk(data) request.requestReceived(method, 'http://example.com%s' % (uri,), 'HTTP/1.0') self.assertEquals(len(reactor.tcpClients), 1) self.assertEquals(reactor.tcpClients[0][0], "example.com") self.assertEquals(reactor.tcpClients[0][1], 80) factory = reactor.tcpClients[0][2] self.assertIsInstance(factory, ProxyClientFactory) self.assertEquals(factory.command, method) self.assertEquals(factory.version, 'HTTP/1.0') self.assertEquals(factory.headers, {'host': 'example.com'}) self.assertEquals(factory.data, data) self.assertEquals(factory.rest, expectedURI) self.assertEquals(factory.father, request)
Example #20
Source File: test_proxy.py From python-for-android with Apache License 2.0 | 5 votes |
def connectProxy(self, proxyClient): """ Connect a proxy client to a L{StringTransportWithDisconnection}. @param proxyClient: A L{ProxyClient}. @return: The L{StringTransportWithDisconnection}. """ clientTransport = StringTransportWithDisconnection() clientTransport.protocol = proxyClient proxyClient.makeConnection(clientTransport) return clientTransport
Example #21
Source File: test_pubsubs.py From twisted-mqtt with MIT License | 5 votes |
def setUp(self): ''' Set up a connencted state ''' self.transport = proto_helpers.StringTransportWithDisconnection() self.clock = task.Clock() MQTTBaseProtocol.callLater = self.clock.callLater self.factory = MQTTFactory(MQTTFactory.PUBLISHER | MQTTFactory.SUBSCRIBER) self._rebuild() self.disconnected = 0
Example #22
Source File: test_policies.py From learn_python3_spider with MIT License | 5 votes |
def test_thingsGetLogged(self): """ Check the output produced by L{policies.TrafficLoggingFactory}. """ wrappedFactory = Server() wrappedFactory.protocol = WriteSequenceEchoProtocol t = StringTransportWithDisconnection() f = TestLoggingFactory(wrappedFactory, 'test') p = f.buildProtocol(('1.2.3.4', 5678)) t.protocol = p p.makeConnection(t) v = f.openFile.getvalue() self.assertIn('*', v) self.assertFalse(t.value()) p.dataReceived(b'here are some bytes') v = f.openFile.getvalue() self.assertIn("C 1: %r" % (b'here are some bytes',), v) self.assertIn("S 1: %r" % (b'here are some bytes',), v) self.assertEqual(t.value(), b'here are some bytes') t.clear() p.dataReceived(b'prepare for vector! to the extreme') v = f.openFile.getvalue() self.assertIn("SV 1: %r" % ([b'prepare for vector! to the extreme'],), v) self.assertEqual(t.value(), b'prepare for vector! to the extreme') p.loseConnection() v = f.openFile.getvalue() self.assertIn('ConnectionDone', v)
Example #23
Source File: test_policies.py From learn_python3_spider with MIT License | 5 votes |
def setUp(self): """ Create a testable, deterministic clock, and a set of server factory/protocol/transport. """ self.clock = task.Clock() wrappedFactory = protocol.ServerFactory() wrappedFactory.protocol = SimpleProtocol self.factory = TestableTimeoutFactory(self.clock, wrappedFactory, 3) self.proto = self.factory.buildProtocol( address.IPv4Address('TCP', '127.0.0.1', 12345)) self.transport = StringTransportWithDisconnection() self.transport.protocol = self.proto self.proto.makeConnection(self.transport) self.wrappedProto = self.proto.wrappedProtocol
Example #24
Source File: test_policies.py From learn_python3_spider with MIT License | 5 votes |
def test_readLimit(self): """ Check the readLimit parameter: read data and check for the pause status. """ server = Server() tServer = TestableThrottlingFactory(task.Clock(), server, readLimit=10) port = tServer.buildProtocol(address.IPv4Address('TCP', '127.0.0.1', 0)) tr = StringTransportWithDisconnection() tr.protocol = port port.makeConnection(tr) port.dataReceived(b"0123456789") port.dataReceived(b"abcdefghij") self.assertEqual(tr.value(), b"0123456789abcdefghij") self.assertEqual(tServer.readThisSecond, 20) tServer.clock.advance(1.05) self.assertEqual(tServer.readThisSecond, 0) self.assertEqual(tr.producerState, 'paused') tServer.clock.advance(1.05) self.assertEqual(tServer.readThisSecond, 0) self.assertEqual(tr.producerState, 'producing') tr.clear() port.dataReceived(b"0123456789") port.dataReceived(b"abcdefghij") self.assertEqual(tr.value(), b"0123456789abcdefghij") self.assertEqual(tServer.readThisSecond, 20) tServer.clock.advance(1.05) self.assertEqual(tServer.readThisSecond, 0) self.assertEqual(tr.producerState, 'paused') tServer.clock.advance(1.05) self.assertEqual(tServer.readThisSecond, 0) self.assertEqual(tr.producerState, 'producing')
Example #25
Source File: test_policies.py From learn_python3_spider with MIT License | 5 votes |
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(b"0123456789") port.dataReceived(b"abcdefghij") self.assertEqual(tr.value(), b"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)
Example #26
Source File: test_memcache.py From learn_python3_spider with MIT License | 5 votes |
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()
Example #27
Source File: test_memcache.py From learn_python3_spider with MIT License | 5 votes |
def setUp(self): """ Create a memcache client, connect it to a string protocol, and make it use 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)
Example #28
Source File: test_wrapper.py From learn_python3_spider with MIT License | 5 votes |
def test_overflowBytesSentToWrappedProtocolChunks(self): """ Test if header streaming passes extra data appropriately. """ factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol)) proto = factory.buildProtocol( address.IPv6Address('TCP', b'::1', 8080), ) transport = StringTransportWithDisconnection() proto.makeConnection(transport) proto.dataReceived(self.IPV6HEADER[:18]) proto.dataReceived(self.IPV6HEADER[18:] + b'HTTP/1.1 / GET') self.assertEqual(proto.wrappedProtocol.data, b'HTTP/1.1 / GET')
Example #29
Source File: test_wrapper.py From learn_python3_spider with MIT License | 5 votes |
def test_overflowBytesSentToWrappedProtocol(self): """ Test if non-header bytes are passed to the wrapped protocol. """ factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol)) proto = factory.buildProtocol( address.IPv6Address('TCP', b'::1', 8080), ) transport = StringTransportWithDisconnection() proto.makeConnection(transport) proto.dataReceived(self.IPV6HEADER + b'HTTP/1.1 / GET') self.assertEqual(proto.wrappedProtocol.data, b'HTTP/1.1 / GET')
Example #30
Source File: test_wrapper.py From learn_python3_spider with MIT License | 5 votes |
def test_validIPv6HeaderResolves_getPeerHost(self): """ Test if IPv6 headers result in the correct host and peer data. """ factory = HAProxyWrappingFactory(Factory.forProtocol(StaticProtocol)) proto = factory.buildProtocol( address.IPv4Address('TCP', b'::1', 8080), ) transport = StringTransportWithDisconnection() proto.makeConnection(transport) proto.dataReceived(self.IPV6HEADER) self.assertEqual(proto.getPeer().host, b'0:0:0:0:0:0:0:1') self.assertEqual(proto.getPeer().port, 8080) self.assertEqual( proto.wrappedProtocol.transport.getPeer().host, b'0:0:0:0:0:0:0:1', ) self.assertEqual( proto.wrappedProtocol.transport.getPeer().port, 8080, ) self.assertEqual(proto.getHost().host, b'0:0:0:0:0:0:0:1') self.assertEqual(proto.getHost().port, 8888) self.assertEqual( proto.wrappedProtocol.transport.getHost().host, b'0:0:0:0:0:0:0:1', ) self.assertEqual( proto.wrappedProtocol.transport.getHost().port, 8888, )