def test_eof_exc(self): def p(out, buf): while True: yield # read chunk class CustomEofErr(Exception): pass stream = parsers.StreamParser( eof_exc_class=CustomEofErr, loop=self.loop) s = stream.set_parser(p) stream.feed_eof() self.assertIsInstance(s.exception(), CustomEofErr)
def test_feed_parser(self): stream = parsers.StreamParser(loop=self.loop) s = stream.set_parser(self.lines_parser) stream.feed_data(b'line1') stream.feed_data(b'\r\nline2\r\ndata') self.assertEqual(b'data', bytes(stream._buffer)) stream.feed_eof() self.assertEqual( [(bytearray(b'line1\r\n'), 7), (bytearray(b'line2\r\n'), 7)], list(s._buffer)) self.assertEqual(b'data', bytes(stream._buffer)) self.assertTrue(s.is_eof())
def test_unset_parser_stop(self): def p(out, buf): try: while True: yield # read chunk except parsers.EofStream: out.feed_eof() stream = parsers.StreamParser() s = stream.set_parser(p) stream.feed_data(b'line1') stream.unset_parser() self.assertTrue(s._eof)
def test_feed_eof_stop(loop): def p(out, buf): try: while True: yield # read chunk except parsers.EofStream: out.feed_eof() stream = parsers.StreamParser(loop=loop) s = stream.set_parser(p) stream.feed_data(b'line1') stream.feed_eof() assert s._eof
def test_set_parser_feed_existing(self): stream = parsers.StreamParser() stream.feed_data(b'line1') stream.feed_data(b'\r\nline2\r\ndata') s = stream.set_parser(self.lines_parser) self.assertEqual([bytearray(b'line1\r\n'), bytearray(b'line2\r\n')], list(s._buffer)) self.assertEqual(b'data', bytes(stream._buffer)) self.assertIsNotNone(stream._parser) stream.unset_parser() self.assertIsNone(stream._parser) self.assertEqual(b'data', bytes(stream._buffer)) self.assertTrue(s._eof)
def test_set_parser_feed_existing(loop, lines_parser): stream = parsers.StreamParser(loop=loop) stream.feed_data(b'line1') stream.feed_data(b'\r\nline2\r\ndata') s = stream.set_parser(lines_parser) assert ([(bytearray(b'line1\r\n'), 7), (bytearray(b'line2\r\n'), 7)] == list(s._buffer)) assert b'data' == bytes(stream._buffer) assert stream._parser is not None stream.unset_parser() assert stream._parser is None assert b'data' == bytes(stream._buffer) assert s._eof
def test_unset_parser_eof_exc(loop): def p(out, buf): try: while True: yield # read chunk except parsers.EofStream: raise ValueError() stream = parsers.StreamParser(loop=loop) s = stream.set_parser(p) stream.feed_data(b'line1') stream.unset_parser() assert isinstance(s.exception(), ValueError) assert stream._parser is None
def test_feed_eof_exc(self): def p(out, buf): try: while True: yield # read chunk except parsers.EofStream: raise ValueError() stream = parsers.StreamParser(loop=self.loop) s = stream.set_parser(p) stream.feed_data(b'line1') self.assertIsNone(s.exception()) stream.feed_eof() self.assertIsInstance(s.exception(), ValueError)
def test_set_parser_feed_existing_stop(self): def LinesParser(out, buf): try: out.feed_data((yield from buf.readuntil(b'\n'))) out.feed_data((yield from buf.readuntil(b'\n'))) finally: out.feed_eof() stream = parsers.StreamParser() stream.feed_data(b'line1') stream.feed_data(b'\r\nline2\r\ndata') s = stream.set_parser(LinesParser) self.assertEqual(b'line1\r\nline2\r\n', b''.join(s._buffer)) self.assertEqual(b'data', bytes(stream._buffer)) self.assertIsNone(stream._parser) self.assertTrue(s._eof)
def test_set_parser_feed_existing_stop(loop): def LinesParser(out, buf): try: chunk = yield from buf.readuntil(b'\n') out.feed_data(chunk, len(chunk)) chunk = yield from buf.readuntil(b'\n') out.feed_data(chunk, len(chunk)) finally: out.feed_eof() stream = parsers.StreamParser(loop=loop) stream.feed_data(b'line1') stream.feed_data(b'\r\nline2\r\ndata') s = stream.set_parser(LinesParser) assert b'line1\r\nline2\r\n' == b''.join(d for d, _ in s._buffer) assert b'data' == bytes(stream._buffer) assert stream._parser is None assert s._eof
def test_feed_none_data(loop): stream = parsers.StreamParser(loop=loop) stream.feed_data(None) assert b'' == bytes(stream._buffer)
def test_feed_data(loop): stream = parsers.StreamParser(loop=loop) stream.feed_data(DATA) assert DATA == bytes(stream._buffer)
def test_at_eof(loop): proto = parsers.StreamParser(loop=loop) assert not proto.at_eof() proto.feed_eof() assert proto.at_eof()
def test_feed_data(self): stream = parsers.StreamParser() stream.feed_data(self.DATA) self.assertEqual(self.DATA, bytes(stream._input))
def test_feed_empty_data(self): stream = parsers.StreamParser() stream.feed_data(b'') self.assertEqual(b'', bytes(stream._input))
def test_feed_none_data(self): stream = parsers.StreamParser(loop=self.loop) stream.feed_data(None) self.assertEqual(b'', bytes(stream._buffer))
def test_feed_data(self): stream = parsers.StreamParser(loop=self.loop) stream.feed_data(self.DATA) self.assertEqual(self.DATA, bytes(stream._buffer))
def test_at_eof(self): proto = parsers.StreamParser(loop=self.loop) self.assertFalse(proto.at_eof()) proto.feed_eof() self.assertTrue(proto.at_eof())