async def test_http_payload_parser_deflate_no_hdrs(self, stream: Any) -> None: """Tests incorrectly formed data (no zlib headers).""" # c=compressobj(wbits=-15); b''.join([c.compress(b'data'), c.flush()]) COMPRESSED = b"KI,I\x04\x00" length = len(COMPRESSED) out = aiohttp.FlowControlDataQueue( stream, 2 ** 16, loop=asyncio.get_event_loop() ) p = HttpPayloadParser(out, length=length, compression="deflate") p.feed_data(COMPRESSED) assert b"data" == b"".join(d for d, _ in out._buffer) assert out.is_eof()
def test_http_payload_parser_deflate_disabled(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), CIMultiDict([('CONTENT-LENGTH', len(self._COMPRESSED))]), [(b'CONTENT-LENGTH', str(len(self._COMPRESSED)).encode('ascii'))], None, 'deflate') out = aiohttp.FlowControlDataQueue(self.stream) 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(d for d, _ in out._buffer))
def test_http_response_parser_no_reason(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpResponseParser()(out, buf) next(p) try: p.send(b'HTTP/1.1 200\r\n\r\n') except StopIteration: pass v, s, r = out._buffer[0][:3] self.assertEqual(v, (1, 1)) self.assertEqual(s, 200) self.assertEqual(r, '')
def test_http_request_parser_utf8(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpRequestParser()(out, buf) next(p) msg = 'get /path HTTP/1.1\r\nx-test:тест\r\n\r\n'.encode('utf-8') try: p.send(msg) except StopIteration: pass result = out._buffer[0] self.assertEqual( ('GET', '/path', (1, 1), CIMultiDict([('X-TEST', 'тест')]), False, None), result)
async def test_feed_data_err(self, stream: Any) -> None: buf = aiohttp.FlowControlDataQueue(stream, 2**16, loop=asyncio.get_event_loop()) dbuf = DeflateBuffer(buf, "deflate") exc = ValueError() dbuf.decompressor = mock.Mock() dbuf.decompressor.decompress.side_effect = exc with pytest.raises(http_exceptions.ContentEncodingError): # Should be more than 4 bytes to trigger deflate FSM error. # Should start with b'x', otherwise code switch mocked decoder. dbuf.feed_data(b"xsomedata", 9)
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(self): msg = protocol.RawRequestMessage( 'GET', '/', (1, 1), CIMultiDict([('CONTENT-LENGTH', '2')]), None, None) out = aiohttp.FlowControlDataQueue(self.stream) 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(d for d, _ in out._buffer)) self.assertEqual(b'45', bytes(buf))
def test_http_response_parser_utf8(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpResponseParser()(out, buf) next(p) msg = 'HTTP/1.1 200 Ok\r\nx-test:тест\r\n\r\n'.encode('utf-8') try: p.send(msg) except StopIteration: pass v, s, r, h = out._buffer[0][0][:4] self.assertEqual(v, (1, 1)) self.assertEqual(s, 200) self.assertEqual(r, 'Ok') self.assertEqual(h, CIMultiDict([('X-TEST', 'тест')]))
def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) self.wsgi = unittest.mock.Mock() self.reader = unittest.mock.Mock() self.writer = unittest.mock.Mock() self.writer.drain.return_value = () self.transport = unittest.mock.Mock() self.transport.get_extra_info.return_value = '127.0.0.1' self.headers = multidict.MultiDict() self.message = protocol.RawRequestMessage( 'GET', '/path', (1, 0), self.headers, True, 'deflate') self.payload = aiohttp.FlowControlDataQueue(self.reader) self.payload.feed_data(b'data') self.payload.feed_data(b'data') self.payload.feed_eof()
def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) self.wsgi = unittest.mock.Mock() self.reader = unittest.mock.Mock() self.writer = unittest.mock.Mock() self.writer.drain.return_value = () self.transport = unittest.mock.Mock() self.transport.get_extra_info.side_effect = [('1.2.3.4', 1234), ('2.3.4.5', 80)] self.headers = multidict.MultiDict({"HOST": "python.org"}) self.message = protocol.RawRequestMessage('GET', '/path', (1, 0), self.headers, True, 'deflate') self.payload = aiohttp.FlowControlDataQueue(self.reader) self.payload.feed_data(b'data', 4) self.payload.feed_data(b'data', 4) self.payload.feed_eof()
def test_parser(self, m_parse_message): cur = 0 def parse_message(buf): nonlocal cur yield if cur == 0: cur = 1 return websocket.Message(websocket.OPCODE_TEXT, b'line1', b'') else: return websocket.Message(websocket.OPCODE_CLOSE, b'', b'') m_parse_message.side_effect = parse_message out = aiohttp.FlowControlDataQueue(unittest.mock.Mock()) buf = aiohttp.ParserBuffer() p = websocket.WebSocketParser(out, buf) next(p) p.send(b'') self.assertRaises(StopIteration, p.send, b'') self.assertEqual((websocket.OPCODE_TEXT, b'line1', b''), out._buffer[0]) self.assertEqual((websocket.OPCODE_CLOSE, b'', b''), out._buffer[1]) self.assertTrue(out._eof)
def test_parse_no_body(self, stream): out = aiohttp.FlowControlDataQueue(stream) p = HttpPayloadParser(out, method='PUT') assert out.is_eof() assert p.done
def test_http_payload_parser_length_zero(self): out = aiohttp.FlowControlDataQueue(self.stream) p = 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) p = HttpPayloadParser(out, chunked=True) self.assertRaises(errors.TransferEncodingError, p.feed_data, b'blah\r\n') self.assertIsInstance(out.exception(), errors.TransferEncodingError)
def test_parse_length_payload_eof(self): out = aiohttp.FlowControlDataQueue(self.stream) p = HttpPayloadParser(out, length=4) p.feed_data(b'da') p.feed_eof()
def test_parse_no_body(self): out = aiohttp.FlowControlDataQueue(self.stream) p = HttpPayloadParser(out, method='PUT') self.assertTrue(out.is_eof()) self.assertTrue(p.done)
def test_http_response_parser_bad_status_line_eof(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpResponseParser()(out, buf) next(p) self.assertRaises(aiohttp.EofStream, p.throw, aiohttp.EofStream())
def test_http_response_parser_bad_status_line(self): out = aiohttp.FlowControlDataQueue(self.stream) buf = aiohttp.ParserBuffer() p = protocol.HttpResponseParser()(out, buf) next(p) self.assertRaises(errors.BadStatusLine, p.send, b'\r\n\r\n')
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')
def function654(self): var3970 = aiohttp.FlowControlDataQueue(self.attribute1609) var952 = HttpPayloadParser(var3970, method='PUT') self.assertTrue(var3970.is_eof()) self.assertTrue(var952.done)
def function1379(self): var1319 = aiohttp.FlowControlDataQueue(self.attribute1609) var703 = HttpPayloadParser(var1319, length=4) var703.feed_data(b'da') with pytest.raises(http_exceptions.ContentLengthError): var703.feed_eof()
def function1758(self): var2213 = aiohttp.FlowControlDataQueue(self.attribute1609) var1764 = HttpPayloadParser(var2213, length=0) self.assertTrue(var1764.done) self.assertTrue(var2213.is_eof())
def test_http_payload_parser_length_zero(self, stream): out = aiohttp.FlowControlDataQueue(stream) p = HttpPayloadParser(out, length=0) assert p.done assert out.is_eof()
def test_empty_body(self): buf = aiohttp.FlowControlDataQueue(self.stream) dbuf = protocol.DeflateBuffer(buf, 'deflate') dbuf.feed_eof() self.assertTrue(buf.at_eof())
def function279(self): var2756 = aiohttp.FlowControlDataQueue(self.attribute1675) var4405 = DeflateBuffer(var2756, 'deflate') var4405.feed_eof() self.assertTrue(var2756.at_eof())
def test_empty_body(self, stream): buf = aiohttp.FlowControlDataQueue(stream) dbuf = DeflateBuffer(buf, 'deflate') dbuf.feed_eof() assert buf.at_eof()
async def test_http_payload_parser_length_zero(self, stream) -> None: out = aiohttp.FlowControlDataQueue(stream, loop=asyncio.get_event_loop()) p = HttpPayloadParser(out, length=0) assert p.done assert out.is_eof()