From ab7d833e6997aeb77efff4e0e313f6c76493f0eb Mon Sep 17 00:00:00 2001 From: Igor Davydenko Date: Wed, 11 Mar 2020 23:39:42 +0200 Subject: [PATCH] feature: Ensure named upload URLs works as well This is common pattern to setup tus uploads to named resources, such as: - `/user/{username}/uploads` - or `/clients/{client_id:\d+}/projects/{project_id:\d+}/uploads` Previously attaching `aiohttp_tus` to given URLs result in `KeyError`, which is fixed by given commit. --- CHANGELOG.rst | 6 ++++++ aiohttp_tus/__init__.py | 2 ++ aiohttp_tus/views.py | 2 +- pyproject.toml | 2 +- tests/test_tus.py | 26 ++++++++++++++++---------- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f09b540..5c3ad00 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,9 @@ +1.0.0a1 (In Development) +======================== + +- Ensure named upload URLs (e.g. ``/user/{username}/uploads``) works as well +- Ensure package is typed by adding ``py.typed`` + 1.0.0a0 (2020-03-11) ==================== diff --git a/aiohttp_tus/__init__.py b/aiohttp_tus/__init__.py index 194dbca..d64523c 100644 --- a/aiohttp_tus/__init__.py +++ b/aiohttp_tus/__init__.py @@ -1,3 +1,5 @@ from .tus import setup_tus __all__ = ("setup_tus",) +__license__ = "BSD-3-Clause" +__version__ = "1.0.0a1" diff --git a/aiohttp_tus/views.py b/aiohttp_tus/views.py index bdb147a..5472749 100644 --- a/aiohttp_tus/views.py +++ b/aiohttp_tus/views.py @@ -98,7 +98,7 @@ async def start_upload(request: web.Request) -> web.Response: headers[constants.HEADER_LOCATION] = str( request.url.join( request.app.router[constants.ROUTE_RESOURCE].url_for( - resource_uid=resource.uid + **request.match_info, resource_uid=resource.uid ) ) ) diff --git a/pyproject.toml b/pyproject.toml index 601e197..b695c6b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ show_missing = true [tool.poetry] name = "aiohttp-tus" -version = "1.0.0a0" +version = "1.0.0a1" description = "tus.io protocol implementation for aiohttp.web applications" authors = ["Igor Davydenko "] license = "BSD-3-Clause" diff --git a/tests/test_tus.py b/tests/test_tus.py index 7775b72..3df9ec7 100644 --- a/tests/test_tus.py +++ b/tests/test_tus.py @@ -9,6 +9,7 @@ import pytest import tus from aiohttp import web +from aiohttp.test_utils import TestClient from aiohttp_tus import setup_tus from aiohttp_tus.constants import APP_TUS_CONFIG_KEY @@ -21,21 +22,26 @@ @pytest.fixture def aiohttp_test_client(aiohttp_client): @asynccontextmanager - async def factory(): + async def factory(*, upload_url: str) -> TestClient: with tempfile.TemporaryDirectory(prefix="aiohttp_tus") as temp_path: - upload_path = Path(temp_path) - yield await aiohttp_client(create_app(upload_path=upload_path)) + app = setup_tus( + web.Application(), upload_path=Path(temp_path), upload_url=upload_url + ) + yield await aiohttp_client(app) return factory -def create_app(*, upload_path: Path) -> web.Application: - return setup_tus(web.Application(), upload_url="/uploads", upload_path=upload_path) - - -async def test_upload(aiohttp_test_client, loop): - async with aiohttp_test_client() as client: - upload_url = f"http://{client.host}:{client.port}/uploads" +@pytest.mark.parametrize( + "upload_url, tus_upload_url", + ( + ("/uploads", "/uploads"), + (r"/user/{username}/uploads", "/user/playpauseandtop/uploads"), + ), +) +async def test_upload(aiohttp_test_client, loop, upload_url, tus_upload_url): + async with aiohttp_test_client(upload_url=upload_url) as client: + upload_url = f"http://{client.host}:{client.port}{tus_upload_url}" test_upload_path = TEST_DATA_PATH / "hello.txt" with open(test_upload_path, "rb") as handler: