| 82 | @pytest.mark.parametrize("case", CASES, ids=lambda case: case.id) |
| 83 | @pytest.mark.benchmark(max_time=0.5, max_rounds=1) |
| 84 | def test_file_response( |
| 85 | tmp_path: Path, |
| 86 | asgi_runner: ASGIRunner, |
| 87 | benchmark: BenchmarkFixture, |
| 88 | case: BenchmarkCase, |
| 89 | ) -> None: |
| 90 | path = tmp_path / "file.bin" |
| 91 | path.write_bytes(b"x" * case.file_size) |
| 92 | messages = benchmark.pedantic(dispatch, args=(asgi_runner, FileApp(path), case), rounds=1) |
| 93 | |
| 94 | expected_status = 206 if case.range_header is not None else 200 |
| 95 | assert messages[0]["type"] == "http.response.start" |
| 96 | assert messages[0]["status"] == expected_status |
| 97 | headers = Headers(raw=messages[0]["headers"]) |
| 98 | expected_size = case.expected_size or case.file_size |
| 99 | assert headers["content-length"] == str(expected_size) |
| 100 | |
| 101 | if case.pathsend: |
| 102 | assert messages[1:] == [{"type": "http.response.pathsend", "path": str(path)}] |
| 103 | else: |
| 104 | body_messages = messages[1:] |
| 105 | assert sum(len(message.get("body", b"")) for message in body_messages) == expected_size |
| 106 | assert body_messages[-1].get("more_body") is False |
| 107 | |
| 108 | if case.range_header is not None: |
| 109 | assert headers["content-range"] == f"bytes 262144-327679/{case.file_size}" |
| 110 | |
| 111 | benchmark.extra_info["file_bytes"] = case.file_size |
| 112 | benchmark.extra_info["response_bytes"] = expected_size |
| 113 | benchmark.extra_info["pathsend"] = case.pathsend |