Skip to content

Commit

Permalink
feature: Ensure named upload URLs works as well
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
playpauseandstop committed Mar 11, 2020
1 parent eabb409 commit ab7d833
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
====================

Expand Down
2 changes: 2 additions & 0 deletions aiohttp_tus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .tus import setup_tus

__all__ = ("setup_tus",)
__license__ = "BSD-3-Clause"
__version__ = "1.0.0a1"
2 changes: 1 addition & 1 deletion aiohttp_tus/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "BSD-3-Clause"
Expand Down
26 changes: 16 additions & 10 deletions tests/test_tus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit ab7d833

Please sign in to comment.