MCPcopy
hub / github.com/encode/starlette / test_file_response

Function test_file_response

tests/test_responses.py:213–249  ·  view source on GitHub ↗
(tmp_path: Path, test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

211
212
213def test_file_response(tmp_path: Path, test_client_factory: TestClientFactory) -> None:
214 path = tmp_path / "xyz"
215 content = b"<file content>" * 1000
216 path.write_bytes(content)
217
218 filled_by_bg_task = ""
219
220 async def numbers(minimum: int, maximum: int) -> AsyncIterator[str]:
221 for i in range(minimum, maximum + 1):
222 yield str(i)
223 if i != maximum:
224 yield ", "
225 await anyio.sleep(0)
226
227 async def numbers_for_cleanup(start: int = 1, stop: int = 5) -> None:
228 nonlocal filled_by_bg_task
229 async for thing in numbers(start, stop):
230 filled_by_bg_task = filled_by_bg_task + thing
231
232 cleanup_task = BackgroundTask(numbers_for_cleanup, start=6, stop=9)
233
234 async def app(scope: Scope, receive: Receive, send: Send) -> None:
235 response = FileResponse(path=path, filename="example.png", background=cleanup_task)
236 await response(scope, receive, send)
237
238 assert filled_by_bg_task == ""
239 client = test_client_factory(app)
240 response = client.get("/")
241 expected_disposition = 'attachment; filename="example.png"'
242 assert response.status_code == status.HTTP_200_OK
243 assert response.content == content
244 assert response.headers["content-type"] == "image/png"
245 assert response.headers["content-disposition"] == expected_disposition
246 assert "content-length" in response.headers
247 assert "last-modified" in response.headers
248 assert "etag" in response.headers
249 assert filled_by_bg_task == "6, 7, 8, 9"
250
251
252@pytest.mark.anyio

Callers

nothing calls this directly

Calls 3

BackgroundTaskClass · 0.90
test_client_factoryFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected