def test_reader_read_error(self): s = BytesIO(b"foobar\nfoobar") s = tcp.Reader(s) o = mock.MagicMock() o.read = mock.MagicMock(side_effect=socket.error) s.o = o tutils.raises(exceptions.TcpDisconnect, s.read, 10)
def test_first_byte_timestamp_updated_on_readline(self): s = BytesIO(b"foobar\nfoobar\nfoobar") s = tcp.Reader(s) s.readline() assert s.first_byte_timestamp expected = s.first_byte_timestamp s.readline() assert s.first_byte_timestamp == expected
def test_wrap(self): s = BytesIO(b"foobar\nfoobar") s.flush() s = tcp.Reader(s) assert s.readline() == b"foobar\n" assert s.readline() == b"foobar" # Test __getattr__ assert s.isatty
def test_blocksize(self): s = BytesIO(b"1234567890abcdefghijklmnopqrstuvwxyz") s = tcp.Reader(s) s.BLOCKSIZE = 2 assert s.read(1) == b"1" assert s.read(2) == b"23" assert s.read(3) == b"456" assert s.read(4) == b"7890" d = s.read(-1) assert d.startswith(b"abc") and d.endswith(b"xyz")
def test_readlog(self): s = BytesIO(b"foobar\nfoobar") s = tcp.Reader(s) assert not s.is_logging() s.start_log() assert s.is_logging() s.readline() assert s.get_log() == b"foobar\n" s.read(1) assert s.get_log() == b"foobar\nf" s.start_log() assert s.get_log() == b"" s.read(1) assert s.get_log() == b"o" s.stop_log() tutils.raises(ValueError, s.get_log)
def test_reader_incomplete_error(self): s = BytesIO(b"foobar") s = tcp.Reader(s) with pytest.raises(exceptions.TcpReadIncomplete): s.safe_read(10)
def test_reader_readline_disconnect(self): o = mock.MagicMock() o.read = mock.MagicMock(side_effect=socket.error) s = tcp.Reader(o) with pytest.raises(exceptions.TcpDisconnect): s.readline(10)
def test_read_syscall_ssl_error(self): s = mock.MagicMock() s.read = mock.MagicMock(side_effect=SSL.SysCallError()) s = tcp.Reader(s) with pytest.raises(exceptions.TlsException): s.read(1)
def test_reset_timestamps(self): s = BytesIO(b"foobar\nfoobar") s = tcp.Reader(s) s.first_byte_timestamp = 500 s.reset_timestamps() assert not s.first_byte_timestamp
def test_limitless(self): s = BytesIO(b"f" * (50 * 1024)) s = tcp.Reader(s) ret = s.read(-1) assert len(ret) == 50 * 1024
def test_limit(self): s = BytesIO(b"foobar\nfoobar") s = tcp.Reader(s) assert s.readline(3) == b"foo"
def treader(bytes): """ Construct a tcp.Read object from bytes. """ fp = io.BytesIO(bytes) return tcp.Reader(fp)
def test_reader_incomplete_error(self): s = BytesIO(b"foobar") s = tcp.Reader(s) tutils.raises(exceptions.TcpReadIncomplete, s.safe_read, 10)
def test_reader_readline_disconnect(self): o = mock.MagicMock() o.read = mock.MagicMock(side_effect=socket.error) s = tcp.Reader(o) tutils.raises(exceptions.TcpDisconnect, s.readline, 10)
def test_read_ssl_error(self): s = mock.MagicMock() s.read = mock.MagicMock(side_effect=SSL.Error()) s = tcp.Reader(s) tutils.raises(exceptions.TlsException, s.read, 1)
def from_bytes(cls, bytestring): """ Construct a websocket frame from an in-memory bytestring to construct a frame from a stream of bytes, use from_file() directly """ return cls.from_file(tcp.Reader(io.BytesIO(bytestring)))