async def test_read_form_while_closed(self) -> None: stream = Stream(b'') obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_TYPE: 'application/x-www-form-urlencoded'}, stream) obj._at_eof = True result = await obj.form() assert not result
async def test_read_with_content_encoding_unknown(self, newline) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_ENCODING: 'snappy'}, Stream(b'\x0e4Time to Relax!%s--:--' % newline), _newline=newline, ) with pytest.raises(RuntimeError): await obj.read(decode=True)
async def test_read_json_compressed(self) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, { CONTENT_ENCODING: 'deflate', CONTENT_TYPE: 'application/json' }, Stream(b'\xabV*I-.Q\xb2RP*H,.NMQ\xaa\x05\x00\r\n--:--')) result = await obj.json() assert {'test': 'passed'} == result
async def test_read_text_compressed(self) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, { CONTENT_ENCODING: 'deflate', CONTENT_TYPE: 'text/plain' }, Stream(b'\x0b\xc9\xccMU(\xc9W\x08J\xcdI\xacP\x04\x00\r\n--:--')) result = await obj.text() assert 'Time to Relax!' == result
async def test_read_with_content_encoding_identity(self) -> None: thing = (b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x0b\xc9\xccMU' b'(\xc9W\x08J\xcdI\xacP\x04\x00$\xfb\x9eV\x0e\x00\x00\x00' b'\r\n') obj = aiohttp.BodyPartReader(BOUNDARY, {CONTENT_ENCODING: 'identity'}, Stream(thing + b'--:--')) result = await obj.read(decode=True) assert thing[:-2] == result
async def test_read_json(self, newline) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_TYPE: 'application/json'}, Stream(b'{"test": "passed"}%s--:--' % newline), _newline=newline, ) result = await obj.json() assert {'test': 'passed'} == result
async def test_read_multiline(self, newline: Any) -> None: data = b"Hello\n,\r\nworld!%s--:--" % newline obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) result = await obj.read() assert b"Hello\n,\r\nworld!" == result result = await obj.read() assert b"" == result assert obj.at_eof()
async def test_read_multiline(self) -> None: obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(b'Hello\n,\r\nworld!\r\n--:--')) result = await obj.read() assert b'Hello\n,\r\nworld!' == result result = await obj.read() assert b'' == result assert obj.at_eof()
async def test_multi_read_chunk(self): stream = Stream(b'Hello,\r\n--:\r\n\r\nworld!\r\n--:--') obj = aiohttp.BodyPartReader(BOUNDARY, {}, stream) result = await obj.read_chunk(8) assert b'Hello,' == result result = await obj.read_chunk(8) assert b'' == result assert obj.at_eof()
async def test_read_all_at_once(self): stream = Stream(b'Hello, World!\r\n--:--\r\n') obj = aiohttp.BodyPartReader(BOUNDARY, {}, stream) result = await obj.read_chunk() assert b'Hello, World!' == result result = await obj.read_chunk() assert b'' == result assert obj.at_eof()
async def test_read_incomplete_body_chunked(self): stream = Stream(b'Hello, World!\r\n-') obj = aiohttp.BodyPartReader(BOUNDARY, {}, stream) result = b'' with pytest.raises(AssertionError): for _ in range(4): result += await obj.read_chunk(7) assert b'Hello, World!\r\n-' == result
async def test_next_next(self): obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(b'Hello, world!\r\n--:')) result = await obj.next() assert b'Hello, world!' == result assert obj.at_eof() result = await obj.next() assert result is None
async def test_read_chunk_without_content_length(self): obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(b'Hello, world!\r\n--:')) c1 = await obj.read_chunk(8) c2 = await obj.read_chunk(8) c3 = await obj.read_chunk(8) assert c1 + c2 == b'Hello, world!' assert c3 == b''
async def test_read_with_content_encoding_gzip(self) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_ENCODING: 'gzip'}, Stream(b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x0b\xc9\xccMU' b'(\xc9W\x08J\xcdI\xacP\x04\x00$\xfb\x9eV\x0e\x00\x00\x00' b'\r\n--:--')) result = await obj.read(decode=True) assert b'Time to Relax!' == result
async def test_next_next(self, newline: Any) -> None: data = b"Hello, world!%s--:" % newline obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) result = await obj.next() assert b"Hello, world!" == result assert obj.at_eof() result = await obj.next() assert result is None
async def test_read(self, newline) -> None: data = b'Hello, world!%s--:' % newline obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) result = await obj.read() assert b'Hello, world!' == result assert obj.at_eof()
async def test_multiread(self, newline: Any) -> None: data = b"Hello,%s--:%s%sworld!%s--:--" % ((newline,) * 4) obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) result = await obj.read() assert b"Hello," == result result = await obj.read() assert b"" == result assert obj.at_eof()
async def test_read_incomplete_body_chunked(self, newline: Any) -> None: data = b"Hello, World!%s--" % newline obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) result = b"" with pytest.raises(AssertionError): for _ in range(4): result += await obj.read_chunk(7) assert data == result
async def test_read_all_at_once(self, newline: Any) -> None: data = b"Hello, World!%s--:--%s" % (newline, newline) obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) result = await obj.read_chunk() assert b"Hello, World!" == result result = await obj.read_chunk() assert b"" == result assert obj.at_eof()
async def test_read_chunk_without_content_length(self, newline: Any) -> None: data = b"Hello, world!%s--:" % newline obj = aiohttp.BodyPartReader(BOUNDARY, {}, Stream(data), _newline=newline) c1 = await obj.read_chunk(8) c2 = await obj.read_chunk(8) c3 = await obj.read_chunk(8) assert c1 + c2 == b"Hello, world!" assert c3 == b""
async def test_multiread(self): obj = aiohttp.BodyPartReader( BOUNDARY, {}, Stream(b'Hello,\r\n--:\r\n\r\nworld!\r\n--:--')) result = await obj.read() assert b'Hello,' == result result = await obj.read() assert b'' == result assert obj.at_eof()
async def test_read_json_while_closed(self) -> None: stream = Stream(b'') obj = aiohttp.BodyPartReader(BOUNDARY, {CONTENT_TYPE: 'application/json'}, stream) obj._at_eof = True result = await obj.json() assert result is None
async def test_read_text(self, newline: Any) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, {}, Stream(b"Hello, world!%s--:--" % newline), _newline=newline, ) result = await obj.text() assert "Hello, world!" == result
async def test_reading_long_part(self) -> None: size = 2 * stream_reader_default_limit protocol = mock.Mock(_reading_paused=False) stream = StreamReader(protocol) stream.feed_data(b'0' * size + b'\r\n--:--') stream.feed_eof() obj = aiohttp.BodyPartReader(BOUNDARY, {}, stream) data = await obj.read() assert len(data) == size
async def test_read_with_content_transfer_encoding_quoted_printable(self): obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_TRANSFER_ENCODING: 'quoted-printable'}, Stream(b'=D0=9F=D1=80=D0=B8=D0=B2=D0=B5=D1=82,' b' =D0=BC=D0=B8=D1=80!\r\n--:--')) result = await obj.read(decode=True) expected = (b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82,' b' \xd0\xbc\xd0\xb8\xd1\x80!') assert result == expected
async def test_reading_long_part(self, newline) -> None: size = 2 * stream_reader_default_limit protocol = mock.Mock(_reading_paused=False) stream = StreamReader(protocol, loop=asyncio.get_event_loop()) stream.feed_data(b'0' * size + b'%s--:--' % newline) stream.feed_eof() obj = aiohttp.BodyPartReader(BOUNDARY, {}, stream, _newline=newline) data = await obj.read() assert len(data) == size
async def test_reading_long_part(self, newline: Any) -> None: size = 2 * 2**16 protocol = mock.Mock(_reading_paused=False) stream = StreamReader(protocol, 2**16, loop=asyncio.get_event_loop()) stream.feed_data(b"0" * size + b"%s--:--" % newline) stream.feed_eof() obj = aiohttp.BodyPartReader(BOUNDARY, {}, stream, _newline=newline) data = await obj.read() assert len(data) == size
async def test_release_respects_content_length(self, newline) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, {'CONTENT-LENGTH': 100500}, Stream(b'.' * 100500 + b'%s--:--' % newline), _newline=newline, ) result = await obj.release() assert result is None assert obj.at_eof()
async def test_read_with_content_transfer_encoding_binary( self, encoding) -> None: data = b'\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82,' \ b' \xd0\xbc\xd0\xb8\xd1\x80!' obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_TRANSFER_ENCODING: encoding}, Stream(data + b'\r\n--:--')) result = await obj.read(decode=True) assert data == result
async def test_read_json_compressed(self, newline: Any) -> None: obj = aiohttp.BodyPartReader( BOUNDARY, {CONTENT_ENCODING: "deflate", CONTENT_TYPE: "application/json"}, Stream(b"\xabV*I-.Q\xb2RP*H,.NMQ\xaa\x05\x00" b"%s--:--" % newline), _newline=newline, ) result = await obj.json() assert {"test": "passed"} == result