Skip to content

Commit 2eda47a

Browse files
committed
Minor changes
1 parent d5180f7 commit 2eda47a

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

lib/callbacks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def raise_http_exception(request: Request) -> Callable[[Exception | str], Awaita
2424
"""Callback to raise an HTTPException with a specific status code."""
2525

2626
async def _raise_http_exception(error: Exception | str) -> None:
27-
message = str(error) if isinstance(error, Exception) else error
28-
code = error.status_code if isinstance(error, HTTPException) else 400
29-
raise StreamTerminated(f"{code}: {message}") from error
27+
code = error.status_code if isinstance(error, HTTPException) else 405
28+
raise StreamTerminated(f"{code}: {str(error)}") from error
3029

3130
return _raise_http_exception

tests/test_endpoints.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async def sender():
112112
for chunk in chunks:
113113
await ws.send_bytes(chunk)
114114
await anyio.sleep(0.05)
115-
115+
116116
# Send completion marker
117117
await ws.send_bytes(b'')
118118
await anyio.sleep(2.0)
@@ -126,7 +126,7 @@ async def receiver():
126126
async with test_client.stream("GET", f"/{uid}?download=true", headers=headers) as response:
127127
await anyio.sleep(0.1)
128128
response.raise_for_status()
129-
129+
130130
i = 0
131131
async for chunk in response.aiter_bytes(4096):
132132
if not chunk:
@@ -139,12 +139,12 @@ async def receiver():
139139

140140
# Wait a bit before resuming
141141
await anyio.sleep(0.5)
142-
142+
143143
# Resume the download
144144
async with test_client.stream("GET", f"/{uid}?download=true", headers=headers) as response:
145145
response.raise_for_status()
146146
assert response.status_code in (200, 206) # 206 for partial content on resume
147-
147+
148148
async for chunk in response.aiter_bytes(4096):
149149
if not chunk:
150150
break
@@ -153,9 +153,10 @@ async def receiver():
153153
async with anyio.create_task_group() as tg:
154154
tg.start_soon(sender)
155155
tg.start_soon(receiver)
156-
156+
157157
# Verify that the full file was received
158158
assert len(received_bytes) == len(file_content)
159+
assert received_bytes == file_content
159160

160161

161162
@pytest.mark.anyio

tests/test_resumable.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async def test_http_resumable_download(test_client: httpx.AsyncClient, websocket
1010
"""Test that HTTP downloads can be resumed after disconnection."""
1111
uid = "test-resume-http"
1212
file_content, file_metadata = generate_test_file(size_in_kb=256)
13-
13+
1414
# Start the sender
1515
async def sender():
1616
async with websocket_client.websocket_connect(f"/send/{uid}") as ws:
@@ -19,57 +19,57 @@ async def sender():
1919
'file_size': file_metadata.size,
2020
'file_type': file_metadata.type
2121
})
22-
22+
2323
response = await ws.recv()
2424
assert response == "Go for file chunks"
25-
25+
2626
# Send file in chunks slowly to allow resume
2727
chunk_size = 8192
2828
for i in range(0, len(file_content), chunk_size):
2929
chunk = file_content[i:i + chunk_size]
3030
await ws.send_bytes(chunk)
3131
await anyio.sleep(0.05) # Slower sending to allow resume
32-
32+
3333
# Send completion marker
3434
await ws.send_bytes(b'')
3535
await anyio.sleep(1) # Keep connection open
36-
36+
3737
# Start receiver that will disconnect and resume
3838
async def receiver():
3939
await anyio.sleep(0.5) # Let sender start
40-
40+
4141
received_bytes = b""
42-
42+
4343
# First download - disconnect after 25% of the file
4444
async with test_client.stream("GET", f"/{uid}?download=true") as response:
4545
assert response.status_code == 200
4646
bytes_to_receive = file_metadata.size // 4
47-
47+
4848
async for chunk in response.aiter_bytes(4096):
4949
received_bytes += chunk
5050
if len(received_bytes) >= bytes_to_receive:
5151
break
52-
52+
5353
first_download_size = len(received_bytes)
5454
assert first_download_size >= bytes_to_receive
55-
55+
5656
await anyio.sleep(0.2) # Small pause before resuming
57-
57+
5858
# Resume download
5959
async with test_client.stream("GET", f"/{uid}?download=true") as response:
6060
# Should get 206 Partial Content for resume
6161
assert response.status_code == 206
62-
62+
6363
# Check Content-Range header
6464
assert 'content-range' in response.headers
65-
65+
6666
async for chunk in response.aiter_bytes(4096):
6767
received_bytes += chunk
68-
68+
6969
# Verify we received the complete file
7070
assert len(received_bytes) == file_metadata.size
7171
assert received_bytes == file_content
72-
72+
7373
async with anyio.create_task_group() as tg:
7474
tg.start_soon(sender)
7575
tg.start_soon(receiver)
@@ -80,7 +80,7 @@ async def test_multiple_resume_attempts(test_client: httpx.AsyncClient, websocke
8080
"""Test that transfers can be resumed multiple times."""
8181
uid = "test-multi-resume"
8282
file_content, file_metadata = generate_test_file(size_in_kb=128)
83-
83+
8484
# Start the sender
8585
async def sender():
8686
async with websocket_client.websocket_connect(f"/send/{uid}") as ws:
@@ -89,51 +89,51 @@ async def sender():
8989
'file_size': file_metadata.size,
9090
'file_type': file_metadata.type
9191
})
92-
92+
9393
response = await ws.recv()
9494
assert response == "Go for file chunks"
95-
95+
9696
# Send file slowly to allow multiple resume attempts
9797
chunk_size = 4096
9898
for i in range(0, len(file_content), chunk_size):
9999
chunk = file_content[i:i + chunk_size]
100100
await ws.send_bytes(chunk)
101101
await anyio.sleep(0.02)
102-
102+
103103
await ws.send_bytes(b'')
104104
await anyio.sleep(2)
105-
105+
106106
# Receiver with multiple disconnects
107107
async def receiver():
108108
await anyio.sleep(0.5)
109-
109+
110110
received_bytes = b""
111111
download_attempts = [0.2, 0.4, 0.6, 1.0] # Download percentages
112-
112+
113113
for attempt_idx, target_percentage in enumerate(download_attempts):
114114
target_bytes = int(file_metadata.size * target_percentage)
115-
115+
116116
async with test_client.stream("GET", f"/{uid}?download=true") as response:
117117
# First attempt gets 200, resumes get 206
118118
if attempt_idx == 0:
119119
assert response.status_code == 200
120120
else:
121121
assert response.status_code == 206
122-
122+
123123
async for chunk in response.aiter_bytes(4096):
124124
received_bytes += chunk
125-
125+
126126
# Stop at target percentage (except last attempt)
127127
if target_percentage < 1.0 and len(received_bytes) >= target_bytes:
128128
break
129-
129+
130130
if target_percentage < 1.0:
131131
await anyio.sleep(0.1) # Pause between attempts
132-
132+
133133
# Verify complete file received
134134
assert len(received_bytes) == file_metadata.size
135135
assert received_bytes == file_content
136-
136+
137137
async with anyio.create_task_group() as tg:
138138
tg.start_soon(sender)
139139
tg.start_soon(receiver)

0 commit comments

Comments
 (0)