|
9 | 9 | import socket |
10 | 10 | import ssl |
11 | 11 | import sys |
| 12 | +import tarfile |
12 | 13 | import time |
| 14 | +import zipfile |
13 | 15 | from typing import Any, AsyncIterator, Type |
14 | 16 | from unittest import mock |
15 | 17 |
|
@@ -511,6 +513,61 @@ async def handler(request): |
511 | 513 | assert 200 == resp.status |
512 | 514 |
|
513 | 515 |
|
| 516 | +async def test_post_data_zipfile_filelike(aiohttp_client: AiohttpClient) -> None: |
| 517 | + data = b"This is a zip file payload text file." |
| 518 | + |
| 519 | + async def handler(request: web.Request) -> web.Response: |
| 520 | + val = await request.read() |
| 521 | + assert data == val, "Transmitted zipfile member failed to match original data." |
| 522 | + return web.Response() |
| 523 | + |
| 524 | + app = web.Application() |
| 525 | + app.router.add_route("POST", "/", handler) |
| 526 | + client = await aiohttp_client(app) |
| 527 | + |
| 528 | + buf = io.BytesIO() |
| 529 | + with zipfile.ZipFile(file=buf, mode="w") as zf: |
| 530 | + with zf.open("payload1.txt", mode="w") as zip_filelike_writing: |
| 531 | + zip_filelike_writing.write(data) |
| 532 | + |
| 533 | + buf.seek(0) |
| 534 | + zf = zipfile.ZipFile(file=buf, mode="r") |
| 535 | + resp = await client.post("/", data=zf.open("payload1.txt")) |
| 536 | + assert 200 == resp.status |
| 537 | + |
| 538 | + |
| 539 | +async def test_post_data_tarfile_filelike(aiohttp_client: AiohttpClient) -> None: |
| 540 | + data = b"This is a tar file payload text file." |
| 541 | + |
| 542 | + async def handler(request: web.Request) -> web.Response: |
| 543 | + val = await request.read() |
| 544 | + assert data == val, "Transmitted tarfile member failed to match original data." |
| 545 | + return web.Response() |
| 546 | + |
| 547 | + app = web.Application() |
| 548 | + app.router.add_route("POST", "/", handler) |
| 549 | + client = await aiohttp_client(app) |
| 550 | + |
| 551 | + buf = io.BytesIO() |
| 552 | + with tarfile.open(fileobj=buf, mode="w") as tf: |
| 553 | + ti = tarfile.TarInfo(name="payload1.txt") |
| 554 | + ti.size = len(data) |
| 555 | + tf.addfile(tarinfo=ti, fileobj=io.BytesIO(data)) |
| 556 | + |
| 557 | + # Random-access tarfile. |
| 558 | + buf.seek(0) |
| 559 | + tf = tarfile.open(fileobj=buf, mode="r:") |
| 560 | + resp = await client.post("/", data=tf.extractfile("payload1.txt")) |
| 561 | + assert 200 == resp.status |
| 562 | + |
| 563 | + # Streaming tarfile. |
| 564 | + buf.seek(0) |
| 565 | + tf = tarfile.open(fileobj=buf, mode="r|") |
| 566 | + for entry in tf: |
| 567 | + resp = await client.post("/", data=tf.extractfile(entry)) |
| 568 | + assert 200 == resp.status |
| 569 | + |
| 570 | + |
514 | 571 | async def test_ssl_client( |
515 | 572 | aiohttp_server, |
516 | 573 | ssl_ctx, |
|
0 commit comments