Skip to content

Commit 8739402

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent e470e0c commit 8739402

File tree

2 files changed

+43
-68
lines changed

2 files changed

+43
-68
lines changed

integration_tests/base_routes.py

+33-51
Original file line numberDiff line numberDiff line change
@@ -1084,132 +1084,114 @@ def create_item(request, body: CreateItemBody, query: CreateItemQueryParamsParam
10841084

10851085
# --- Streaming responses ---
10861086

1087+
10871088
@app.get("/stream/sync")
10881089
def sync_stream():
10891090
def number_generator():
10901091
for i in range(5):
10911092
yield f"Chunk {i}\n".encode()
1092-
1093-
return Response(
1094-
status_code=200,
1095-
headers={"Content-Type": "text/plain"},
1096-
description=number_generator()
1097-
)
1093+
1094+
return Response(status_code=200, headers={"Content-Type": "text/plain"}, description=number_generator())
1095+
10981096

10991097
@app.get("/stream/async")
11001098
async def async_stream():
11011099
async def async_generator():
11021100
import asyncio
1101+
11031102
for i in range(5):
11041103
await asyncio.sleep(1) # Simulate async work
11051104
yield f"Async Chunk {i}\n".encode()
1106-
1107-
return Response(
1108-
status_code=200,
1109-
headers={"Content-Type": "text/plain"},
1110-
description=async_generator()
1111-
)
1105+
1106+
return Response(status_code=200, headers={"Content-Type": "text/plain"}, description=async_generator())
1107+
11121108

11131109
@app.get("/stream/mixed")
11141110
async def mixed_stream():
11151111
async def mixed_generator():
11161112
import asyncio
1113+
11171114
# Binary data
11181115
yield b"Binary chunk\n"
11191116
await asyncio.sleep(0.5)
1120-
1117+
11211118
# String data
11221119
yield "String chunk\n".encode()
11231120
await asyncio.sleep(0.5)
1124-
1121+
11251122
# Integer data
11261123
yield str(42).encode() + b"\n"
11271124
await asyncio.sleep(0.5)
1128-
1125+
11291126
# JSON data
11301127
import json
1128+
11311129
data = {"message": "JSON chunk", "number": 123}
11321130
yield json.dumps(data).encode() + b"\n"
1133-
1134-
return Response(
1135-
status_code=200,
1136-
headers={"Content-Type": "text/plain"},
1137-
description=mixed_generator()
1138-
)
1131+
1132+
return Response(status_code=200, headers={"Content-Type": "text/plain"}, description=mixed_generator())
1133+
11391134

11401135
@app.get("/stream/events")
11411136
async def server_sent_events():
11421137
async def event_generator():
11431138
import asyncio
11441139
import json
11451140
import time
1146-
1141+
11471142
# Regular event
11481143
yield f"event: message\ndata: {json.dumps({'time': time.time(), 'type': 'start'})}\n\n".encode()
11491144
await asyncio.sleep(1)
1150-
1145+
11511146
# Event with ID
11521147
yield f"id: 1\nevent: update\ndata: {json.dumps({'progress': 50})}\n\n".encode()
11531148
await asyncio.sleep(1)
1154-
1149+
11551150
# Multiple data lines
1156-
data = json.dumps({'status': 'complete', 'results': [1, 2, 3]}, indent=2)
1151+
data = json.dumps({"status": "complete", "results": [1, 2, 3]}, indent=2)
11571152
yield f"event: complete\ndata: {data}\n\n".encode()
1158-
1153+
11591154
return Response(
1160-
status_code=200,
1161-
headers={
1162-
"Content-Type": "text/event-stream",
1163-
"Cache-Control": "no-cache",
1164-
"Connection": "keep-alive"
1165-
},
1166-
description=event_generator()
1155+
status_code=200, headers={"Content-Type": "text/event-stream", "Cache-Control": "no-cache", "Connection": "keep-alive"}, description=event_generator()
11671156
)
11681157

1158+
11691159
@app.get("/stream/large-file")
11701160
async def stream_large_file():
11711161
async def file_generator():
11721162
# Simulate streaming a large file in chunks
11731163
chunk_size = 1024 # 1KB chunks
11741164
total_size = 10 * chunk_size # 10KB total
1175-
1165+
11761166
for offset in range(0, total_size, chunk_size):
11771167
# Simulate reading file chunk
11781168
chunk = b"X" * min(chunk_size, total_size - offset)
11791169
yield chunk
1180-
1170+
11811171
return Response(
11821172
status_code=200,
1183-
headers={
1184-
"Content-Type": "application/octet-stream",
1185-
"Content-Disposition": "attachment; filename=large-file.bin"
1186-
},
1187-
description=file_generator()
1173+
headers={"Content-Type": "application/octet-stream", "Content-Disposition": "attachment; filename=large-file.bin"},
1174+
description=file_generator(),
11881175
)
11891176

1177+
11901178
@app.get("/stream/csv")
11911179
async def stream_csv():
11921180
async def csv_generator():
11931181
# CSV header
11941182
yield "id,name,value\n".encode()
1195-
1183+
11961184
import asyncio
11971185
import random
1198-
1186+
11991187
# Generate rows
12001188
for i in range(5):
12011189
await asyncio.sleep(0.5) # Simulate data processing
12021190
row = f"{i},item-{i},{random.randint(1, 100)}\n"
12031191
yield row.encode()
1204-
1205-
return Response(
1206-
status_code=200,
1207-
headers={
1208-
"Content-Type": "text/csv",
1209-
"Content-Disposition": "attachment; filename=data.csv"
1210-
},
1211-
description=csv_generator()
1212-
)
1192+
1193+
return Response(status_code=200, headers={"Content-Type": "text/csv", "Content-Disposition": "attachment; filename=data.csv"}, description=csv_generator())
1194+
12131195

12141196
def main():
12151197
app.set_response_header("server", "robyn")

integration_tests/test_streaming_responses.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
import json
1616
import pytest
17-
from robyn import Robyn
18-
from robyn.robyn import Request
1917
from integration_tests.base_routes import app
2018

2119

2220
@pytest.mark.asyncio
2321
async def test_sync_stream():
2422
"""Test basic synchronous streaming response.
25-
23+
2624
Verifies that:
2725
1. Response has correct content type
2826
2. Chunks are received in correct order
@@ -45,7 +43,7 @@ async def test_sync_stream():
4543
@pytest.mark.asyncio
4644
async def test_async_stream():
4745
"""Test asynchronous streaming response.
48-
46+
4947
Verifies that:
5048
1. Response has correct content type
5149
2. Chunks are received in correct order with delays
@@ -68,7 +66,7 @@ async def test_async_stream():
6866
@pytest.mark.asyncio
6967
async def test_mixed_stream():
7068
"""Test streaming of mixed content types.
71-
69+
7270
Verifies that:
7371
1. Response handles different content types:
7472
- Binary data
@@ -82,12 +80,7 @@ async def test_mixed_stream():
8280
assert response.status_code == 200
8381
assert response.headers["Content-Type"] == "text/plain"
8482

85-
expected = [
86-
b"Binary chunk\n",
87-
b"String chunk\n",
88-
b"42\n",
89-
json.dumps({"message": "JSON chunk", "number": 123}).encode() + b"\n"
90-
]
83+
expected = [b"Binary chunk\n", b"String chunk\n", b"42\n", json.dumps({"message": "JSON chunk", "number": 123}).encode() + b"\n"]
9184

9285
chunks = []
9386
async for chunk in response.content:
@@ -101,7 +94,7 @@ async def test_mixed_stream():
10194
@pytest.mark.asyncio
10295
async def test_server_sent_events():
10396
"""Test Server-Sent Events (SSE) streaming.
104-
97+
10598
Verifies that:
10699
1. Response has correct SSE headers
107100
2. Events are properly formatted with:
@@ -143,7 +136,7 @@ async def test_server_sent_events():
143136
@pytest.mark.asyncio
144137
async def test_large_file_stream():
145138
"""Test streaming of large files in chunks.
146-
139+
147140
Verifies that:
148141
1. Response has correct headers for file download
149142
2. Content is streamed in correct chunk sizes
@@ -166,7 +159,7 @@ async def test_large_file_stream():
166159
@pytest.mark.asyncio
167160
async def test_csv_stream():
168161
"""Test streaming of CSV data.
169-
162+
170163
Verifies that:
171164
1. Response has correct CSV headers
172165
2. CSV content is properly formatted
@@ -184,11 +177,11 @@ async def test_csv_stream():
184177

185178
# Verify header
186179
assert lines[0] == "id,name,value"
187-
180+
188181
# Verify data rows
189182
assert len(lines) == 6 # Header + 5 data rows
190183
for i, line in enumerate(lines[1:], 0):
191-
id_, name, value = line.split(',')
184+
id_, name, value = line.split(",")
192185
assert int(id_) == i
193186
assert name == f"item-{i}"
194-
assert 1 <= int(value) <= 100
187+
assert 1 <= int(value) <= 100

0 commit comments

Comments
 (0)