def test_specifiedHostAddress(self): """ If a host address is passed to L{StringTransport.__init__}, that value is returned from L{StringTransport.getHost}. """ address = object() self.assertIs(StringTransport(address).getHost(), address)
def test_specifiedPeerAddress(self): """ If a peer address is passed to L{StringTransport.__init__}, that value is returned from L{StringTransport.getPeer}. """ address = object() self.assertIs(StringTransport(peerAddress=address).getPeer(), address)
def test_defaultPeerAddress(self): """ If no peer address is passed to L{StringTransport.__init__}, an L{IPv4Address} is returned from L{StringTransport.getPeer}. """ address = StringTransport().getPeer() self.assertIsInstance(address, IPv4Address)
def _test(self, factory, testvalue): transport = StringTransport() protocol = client.ScrapyHTTPPageGetter() protocol.factory = factory protocol.makeConnection(transport) self.assertEqual(set(transport.value().splitlines()), set(testvalue.splitlines())) return testvalue
def setUp(self): self.factory = WebSocketServerFactory() self.factory.setProtocolOptions(trustXForwardedFor=2) self.proto = WebSocketServerProtocol() self.proto.transport = StringTransport() self.proto.factory = self.factory self.proto.failHandshake = Mock() self.proto._connectionMade()
def test_overlong_requests_are_rejected(self): # as a control case, first send a regular request. # connect the site to a fake transport. transport = StringTransport() protocol = self.site.buildProtocol(IPv6Address("TCP", "::1", 2345)) protocol.makeConnection(transport) protocol.dataReceived( b"POST / HTTP/1.1\r\n" b"Connection: close\r\n" b"Transfer-Encoding: chunked\r\n" b"\r\n" b"0\r\n" b"\r\n" ) # we should get a 404 self.assertRegex(transport.value().decode(), r"^HTTP/1\.1 404 ") # now send an oversized request transport = StringTransport() protocol = self.site.buildProtocol(IPv6Address("TCP", "::1", 2345)) protocol.makeConnection(transport) protocol.dataReceived( b"POST / HTTP/1.1\r\n" b"Connection: close\r\n" b"Transfer-Encoding: chunked\r\n" b"\r\n" ) # we deliberately send all the data in one big chunk, to ensure that # twisted isn't buffering the data in the chunked transfer decoder. # we start with the chunk size, in hex. (We won't actually send this much) protocol.dataReceived(b"10000000\r\n") sent = 0 while not transport.disconnected: self.assertLess(sent, 0x10000000, "connection did not drop") protocol.dataReceived(b"\0" * 1024) sent += 1024 # default max upload size is 512K, so it should drop on the next buffer after # that. self.assertEqual(sent, 513 * 1024)
def setUp(self): self.factory = WebSocketServerFactory() self.factory.setProtocolOptions( allowedOrigins=[u'127.0.0.1:*', u'*.example.com:*']) self.proto = WebSocketServerProtocol() self.proto.transport = StringTransport() self.proto.factory = self.factory self.proto.failHandshake = Mock() self.proto._connectionMade()
def setUp(self) -> None: self.factory = ServerProtocolFactory() self.proto = self.factory.buildProtocol(('127.0.0.1', 6379)) self.tr = StringTransport() self.proto.makeConnection(self.tr) self.output = '' def fake_print(s): self.output = s self.fake_print = fake_print
def test_data_cancels_timeout(self): """ When data is received, the timeout is canceled """ clock = Clock() protocol = HangCheckProtocol(Protocol(), reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived(b'some-data') assert_clock_idle(self, clock)
def test_transport(self): """ The transport passed to the underlying protocol is the underlying transport. """ clock = Clock() wrapped_protocol = Protocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) self.assertIdentical(wrapped_protocol.transport, transport)
def test_disconnects(self): """ When connecting to a server that doesn't send any data, the protocol disconnects after the timeout. """ clock = Clock() protocol = HangCheckProtocol(Protocol(), reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) clock.advance(120) self.assertTrue(transport.disconnecting) assert_clock_idle(self, clock)
def test_forwards_data(self): """ Data received by the protocol gets passed to the wrapped protocol. """ clock = Clock() wrapped_protocol = AccumulatingProtocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived(b'some-data') self.assertEqual(wrapped_protocol.data, b"some-data")
def setUp(self) -> None: self.factory = ClientProtocolFactory() self.addr = IPv4Address('TCP', '127.0.0.1', 50000) self.tr = StringTransport() self.input = '' self.output = '' def fake_input(s): return self.input def fake_print(s): self.output = s self.fake_input = fake_input self.fake_print = fake_print
def test_disconnect_cancels_timeout(self): """ If the connection is closed, the hang check is cancelled. """ clock = Clock() protocol = HangCheckProtocol( Protocol(), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.connectionLost( Failure(ConnectionDone("Bye.")) ) assert_clock_idle(self, clock)
def test_data_and_disconnect(self): """ If the connection receives data and then is closed, no error results. """ clock = Clock() protocol = HangCheckProtocol( Protocol(), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) protocol.dataReceived(b"some-data") protocol.connectionLost( Failure(ConnectionDone("Bye.")) ) assert_clock_idle(self, clock)
def test_calls_callback(self): """ When connecting to a server that doesn't send any data, the protocol calls the hung callback. """ results = [] clock = Clock() protocol = HangCheckProtocol( Protocol(), hung_callback=lambda: results.append(True), reactor=clock, ) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) clock.advance(120) self.assertEqual(results, [True]) assert_clock_idle(self, clock)
def test_disconnect_forwarded(self): """ If the connection is closed, the underlying protocol is informed. """ clock = Clock() wrapped_protocol = AccumulatingProtocol() protocol = HangCheckProtocol(wrapped_protocol, reactor=clock) transport = StringTransport() transport.protocol = protocol protocol.makeConnection(transport) reason = ConnectionDone("Bye.") protocol.connectionLost( Failure(reason) ) self.assertTrue(wrapped_protocol.closed) self.assertEqual( wrapped_protocol.closedReason.value, reason, )
def setUp(self): self.transport = StringTransport()