Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_multipart_body_with_real_pre_opened_test_file(dummy_file_path):
async with await aopen(dummy_file_path, "rb") as f:
assert (
await build_multipart_body(
values=OrderedDict({"file": f,}),
encoding="utf8",
boundary_data="8banana133744910kmmr13a56!102!5649",
)
== b'--8banana133744910kmmr13a56!102!5649\r\nContent-Disposition: form-data; name="file"; filename="test.txt"\r\nContent-Type: text/plain\r\n\r\ndummyfile\n\r\n--8banana133744910kmmr13a56!102!5649--\r\n'
)
async def test_multipart_send_single_already_open_async(server):
async with await aopen(TEST_FILE1, "rb") as f:
r = await asks.post(server.http_test_url, multipart={"file_1": f})
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
file_data = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data["file"] == "Compooper"
async def test_upload_anyio_async_files(self, backend, anyio_backend):
"""Uploading a file opened via 'anyio.aopen()' should be possible"""
with open(__file__, mode="rb") as f:
data = f.read()
content_length = len(data)
headers = {
"Content-Length": str(content_length),
}
url = "%s/echo" % self.base_url
with PoolManager(backend=backend) as http:
async with await anyio.aopen(__file__, mode="rb") as f:
resp = await http.urlopen("PUT", url, headers=headers, body=f)
assert resp.status == 200
assert resp.data == data
async def to_bytes(self):
binary_source = self.binary_source
if isinstance(binary_source, Path):
async with await aopen(binary_source, "rb") as f:
return await f.read()
if isinstance(binary_source, bytes):
return binary_source
# else we should have a BinaryIO or an async equivalent,
# which we can't really type-check at runtime
result = binary_source.read()
if isinstance(result, bytes):
return result
# We must then assume it is a coroutine.
return await result
async def _file_manager(self, path):
async with await aopen(path, "rb") as f:
return b"".join(await f.readlines()) + b"\r\n"