def test_write_and_read(self): """Tests writing-to and reading-from TCP Host.""" frame = "%s>%s:%s" % (self.random(6), ','.join([ self.random(6), self.random(6), self.random(6) ]), ' '.join([self.random(), 'test_write_and_read', self.random()])) aprs_frame = aprs.Frame(frame) kiss_frame = aprs_frame.encode_kiss() ks = kiss.TCPKISS(host=self.random_host, port=self.random_port) a = (self.random_host, self.random_port) entry = MocketEntry(a, [kiss_frame]) entry_1 = MocketEntry(('localhost', 80), True) Mocket.register(entry) ks.interface = create_connection(a) ks._write_handler = ks.interface.sendall def _pass(): pass ks.stop = _pass ks.write(kiss_frame) _read_data = ks.read(len(kiss_frame), readmode=False) self._logger.info('_read_data(%s)="%s"', len(_read_data), _read_data)
def _test_write(self): frame = "%s>%s:%s" % ( self.random(6), ','.join([self.random(6), self.random(6), self.random(6)]), ' '.join([ self.random(), 'test_write', self.random()]) ) aprs_frame = aprs.Frame(frame) kiss_frame = aprs_frame.encode_kiss() ks = kiss.TCPKISS(host=self.random_host, port=self.random_port) a = (self.random_host, self.random_port) entry = MocketEntry(a, kiss_frame) Mocket.register(entry) self._logger.debug(a) self._logger.debug(entry.get_response()) ks.interface = create_connection(a) ks._write_handler = ks.interface.sendall def _pass(): pass ks.stop = _pass ks.write(kiss_frame)
def _test_write(self): frame = "%s>%s:%s" % (self.random(6), ','.join([ self.random(6), self.random(6), self.random(6) ]), ' '.join([self.random(), 'test_write', self.random()])) aprs_frame = aprs.Frame(frame) kiss_frame = aprs_frame.encode_kiss() ks = kiss.TCPKISS(host=self.random_host, port=self.random_port) a = (self.random_host, self.random_port) entry = MocketEntry(a, kiss_frame) Mocket.register(entry) self._logger.debug(a) self._logger.debug(entry.get_response()) ks.interface = create_connection(a) ks._write_handler = ks.interface.sendall def _pass(): pass ks.stop = _pass ks.write(kiss_frame)
def test_wrong_ack_additional_answer(self): with mock.patch.object(pylogbeat, 'LOGGER', new=self._mocked_logger): # respond with wrong ACK sequence response = b'2A\\x00\x00\x00\x03\x03\x03' Mocket.register(MocketEntry((SOCKET_HOST, SOCKET_PORT), [response])) exc = None client = self._factor_client() try: with self.assertRaises(pylogbeat.ConnectionException) as exc: client.send([MESSAGE, MESSAGE]) finally: client.close() if exc is not None: message = str(exc.exception) expected_message = 'No ACK received or wrong frame type "0x00"' self.assertEqual(message, expected_message) self.assertEqual(len(Mocket._requests), 1) # logger self._mocked_logger.log.assert_any_call( logging.WARNING, 'Waited for ACK from server but received an unexpected frame: "0x00". Aborting.' ) self._mocked_logger.reset_mock()
def test_ok(self): # window size (we will send two messages below, so let's answer with two) response = b'2A\x00\x00\x00\x02' Mocket.register(MocketEntry((SOCKET_HOST, SOCKET_PORT), [response])) client = self._factor_client() try: client.send([MESSAGE, MESSAGE]) finally: client.close() self.assertEqual(len(Mocket._requests), 1)
def test_wrong_ack_end_of_stream(self): # respond with wrong ACK sequence response = b'2A\x00\x00\x00\x03' Mocket.register(MocketEntry((SOCKET_HOST, SOCKET_PORT), [response])) client = self._factor_client() try: # on real sockets, here we would expect a timeout or no data; # Mocket raises BlockingIOError with self.assertRaises(BlockingIOError): client.send([MESSAGE, MESSAGE]) finally: client.close() self.assertEqual(len(Mocket._requests), 1)
def test_empty_getresponse(self): entry = MocketEntry(('localhost', 8080), []) self.assertEqual(entry.get_response(), encode_utf8(''))
def test_getresponse(self): entry = MocketEntry(('localhost', 8080), ['Show me.\r\n']) self.assertEqual(entry.get_response(), encode_utf8('Show me.\r\n'))