Skip to content

Commit

Permalink
Fix calls to upload_chunk (#92)
Browse files Browse the repository at this point in the history
Closes #82
  • Loading branch information
Acconut authored Dec 13, 2023
1 parent fd8db8f commit 43b9b57
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/test_async_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ def test_upload_chunk(self):
request_length = self.async_uploader.get_request_length()
self.loop.run_until_complete(self.async_uploader.upload_chunk())
self.assertEqual(self.async_uploader.offset, request_length)

def test_upload_chunk_with_creation(self):
with aioresponses() as resps:
resps.post(
self.client.url, status=201,
headers={
"location": f"{self.client.url}hello"
}
)
resps.patch(
f"{self.client.url}hello",
headers={
"upload-offset": "5"
}
)

uploader = self.client.async_uploader(
file_stream=io.BytesIO(b"hello")
)
self.loop.run_until_complete(uploader.upload_chunk())

self.assertEqual(uploader.url, f"{self.client.url}hello")

def test_upload(self):
with aioresponses() as resps:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ def test_upload_chunk(self, request_mock):
self.uploader.upload_chunk()
self.assertEqual(self.uploader.offset, request_length)

@responses.activate
def test_upload_chunk_with_creation(self):
responses.add(
responses.POST, self.client.url,
adding_headers={
"location": f"{self.client.url}hello"
}
)
responses.add(
responses.PATCH,
f"{self.client.url}hello",
adding_headers={
"upload-offset": "5"
}
)

uploader = self.client.uploader(
file_stream=io.BytesIO(b"hello")
)
uploader.upload_chunk()

self.assertEqual(uploader.url, f"{self.client.url}hello")

@mock.patch('tusclient.uploader.uploader.TusRequest')
def test_upload(self, request_mock):
self.mock_request(request_mock)
Expand Down
12 changes: 12 additions & 0 deletions tusclient/uploader/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def upload_chunk(self):
"""
self._retried = 0

# Ensure that we have a URL, as this is behavior we allowed previously.
# See https://github.com/tus/tus-py-client/issues/82.
if not self.url:
self.set_url(self.create_url())
self.offset = 0

self._do_request()
self.offset = int(self.request.response_headers.get("upload-offset"))

Expand Down Expand Up @@ -127,6 +133,12 @@ async def upload_chunk(self):
"""
self._retried = 0

# Ensure that we have a URL, as this is behavior we allowed previously.
# See https://github.com/tus/tus-py-client/issues/82.
if not self.url:
self.set_url(await self.create_url())
self.offset = 0

await self._do_request()
self.offset = int(self.request.response_headers.get("upload-offset"))

Expand Down

0 comments on commit 43b9b57

Please sign in to comment.