def test_parse_length_payload_eof(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_length_payload(out, buf, 4) next(p) p.send(b'da') self.assertRaises(aiohttp.EofStream, p.throw, aiohttp.EofStream)
def test_parse_length_payload_eof(self): out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_length_payload(out, buf, 4) next(p) p.send(b'da') self.assertRaises(errors.IncompleteRead, p.throw, aiohttp.EofStream)
def test_parse_chunked_payload_incomplete(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_chunked_payload(out, buf) next(p) p.send(b'4\r\ndata\r\n') self.assertRaises(aiohttp.EofStream, p.throw, aiohttp.EofStream)
def test_http_payload_parser_length_broken(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('CONTENT-LENGTH', 'qwe')], None, None) out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) self.assertRaises(errors.InvalidHeader, next, p)
def test_http_payload_parser_no_length(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [], None, None) out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg, readall=False)(out, buf) self.assertRaises(StopIteration, next, p) self.assertEqual(b'', b''.join(out._buffer)) self.assertTrue(out._eof)
def test_http_payload_parser_length_wrong(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), CIMultiDict([('CONTENT-LENGTH', '-1')]), None, None) out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) self.assertRaises(errors.InvalidHeader, next, p)
def test_http_payload_parser_length(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, length=2) eof, tail = p.feed_data(b'1245') self.assertTrue(eof) self.assertEqual(b'12', b''.join(d for d, _ in out._buffer)) self.assertEqual(b'45', tail)
def test_http_payload_parser_chunked(self): out = aiohttp.FlowControlDataQueue(self.stream) parser = protocol.HttpPayloadParser(out, chunked=True) assert not parser.done parser.feed_data(b'4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer)) self.assertTrue(out.is_eof())
def _test_parse_length_payload_eof(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(None) p.start(4, out) p.feed_data(b'da') p.feed_eof()
def test_parse_chunked_payload(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, chunked=True) eof, tail = p.feed_data(b'4\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer)) self.assertEqual(b'', tail) self.assertTrue(eof) self.assertTrue(out.is_eof())
def test_parse_eof_payload(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, readall=True) p.feed_data(b'data') p.feed_eof() self.assertTrue(out.is_eof()) self.assertEqual([(bytearray(b'data'), 4)], list(out._buffer))
def test_http_payload_parser_length_zero(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('CONTENT-LENGTH', '0')], None, None) out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) self.assertRaises(StopIteration, next, p) self.assertEqual(b'', b''.join(out._buffer))
def test_http_payload_parser_deflate(self): length = len(self._COMPRESSED) out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, length=length, compression='deflate') p.feed_data(self._COMPRESSED) self.assertEqual(b'data', b''.join(d for d, _ in out._buffer)) self.assertTrue(out.is_eof())
def test_http_payload_parser_eof(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, readall=True) assert not p.done p.feed_data(b'data') p.feed_data(b'line') p.feed_eof() self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer)) self.assertTrue(out.is_eof())
def test_parse_length_payload(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, length=4) p.feed_data(b'da') p.feed_data(b't') eof, tail = p.feed_data(b'aline') self.assertEqual(3, len(out._buffer)) self.assertEqual(b'data', b''.join(d for d, _ in out._buffer)) self.assertEqual(b'line', tail)
def test_parse_chunked_payload_extension(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_chunked_payload(out, buf) next(p) try: p.send(b'4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') except StopIteration: pass self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer))
def test_http_payload_parser_websocket(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), CIMultiDict([('SEC-WEBSOCKET-KEY1', '13')]), None, None) out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) next(p) self.assertRaises(StopIteration, p.send, b'1234567890') self.assertEqual(b'12345678', b''.join(d for d, _ in out._buffer))
def test_http_payload_parser_eof(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [], None, None) out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg, readall=True)(out, buf) next(p) p.send(b'data') p.send(b'line') self.assertRaises(aiohttp.EofStream, p.throw, aiohttp.EofStream()) self.assertEqual(b'dataline', b''.join(out._buffer))
def test_parse_chunked_payload(self): out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_chunked_payload(out, buf) next(p) try: p.send(b'4\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') except StopIteration: pass self.assertEqual(b'dataline', b''.join(out._buffer)) self.assertEqual(b'', bytes(buf))
def test_http_payload_parser_deflate_disabled(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), [('CONTENT-LENGTH', len(self._COMPRESSED))], None, 'deflate') out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg, compression=False)(out, buf) next(p) self.assertRaises(StopIteration, p.send, self._COMPRESSED) self.assertEqual(self._COMPRESSED, b''.join(out._buffer))
def test_http_payload_parser_eof(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), CIMultiDict(), None, None) out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg, readall=True)(out, buf) next(p) p.send(b'data') p.send(b'line') self.assertRaises(StopIteration, p.throw, aiohttp.EofStream()) self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer))
def test_http_payload_parser_chunked(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), CIMultiDict([('TRANSFER-ENCODING', 'chunked')]), None, None) out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) next(p) self.assertRaises(StopIteration, p.send, b'4;test\r\ndata\r\n4\r\nline\r\n0\r\ntest\r\n') self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer))
def test_parse_eof_payload(self): out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_eof_payload(out, buf) next(p) p.send(b'data') try: p.throw(aiohttp.EofStream()) except aiohttp.EofStream: pass self.assertEqual([b'data'], list(out._buffer))
def test_http_payload_parser_deflate(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), CIMultiDict([('CONTENT-LENGTH', len(self._COMPRESSED))]), None, 'deflate') out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) next(p) self.assertRaises(StopIteration, p.send, self._COMPRESSED) self.assertEqual(b'data', b''.join(d for d, _ in out._buffer))
def test_parse_eof_payload(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_eof_payload(out, buf) next(p) p.send(b'data') try: p.throw(aiohttp.EofStream()) except StopIteration: pass self.assertEqual([(bytearray(b'data'), 4)], list(out._buffer))
def test_parse_chunked_payload_chunks(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_chunked_payload(out, buf) next(p) p.send(b'4\r\ndata\r') p.send(b'\n4') p.send(b'\r') p.send(b'\n') p.send(b'line\r\n0\r\n') self.assertRaises(StopIteration, p.send, b'test\r\n') self.assertEqual(b'dataline', b''.join(d for d, _ in out._buffer))
def test_http_payload_parser_length(self): msg = protocol.RawRequestMessage('GET', '/', (1, 1), [('CONTENT-LENGTH', '2')], None, None) out = aiohttp.DataQueue() buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(msg)(out, buf) next(p) try: p.send(b'1245') except StopIteration: pass self.assertEqual(b'12', b''.join(out._buffer)) self.assertEqual(b'45', bytes(buf))
def test_parse_length_payload(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_length_payload(out, buf, 4) next(p) p.send(b'da') p.send(b't') try: p.send(b'aline') except StopIteration: pass self.assertEqual(3, len(out._buffer)) self.assertEqual(b'data', b''.join(d for d, _ in out._buffer)) self.assertEqual(b'line', bytes(buf))
def test_http_payload_parser_length_zero(self): out = aiohttp.FlowControlDataQueue(self.stream) p = protocol.HttpPayloadParser(out, length=0) self.assertTrue(p.done) self.assertTrue(out.is_eof())
def test_parse_chunked_payload_size_error(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpPayloadParser(None).parse_chunked_payload(out, buf) next(p) self.assertRaises(errors.TransferEncodingError, p.send, b'blah\r\n')