-
-
Notifications
You must be signed in to change notification settings - Fork 328
fix: support bytes in StreamingResponse generators #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sansyrox
wants to merge
4
commits into
main
Choose a base branch
from
fix/sse-downloads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
| from integration_tests.helpers.http_methods_helpers import BASE_URL | ||
|
|
||
|
|
||
| @pytest.mark.benchmark | ||
| def test_stream_bytes_basic(session): | ||
| """Test that binary bytes can be streamed without error""" | ||
| response = requests.get(f"{BASE_URL}/stream/bytes", stream=True, timeout=5) | ||
| assert response.status_code == 200 | ||
| assert response.headers.get("Content-Type") == "application/octet-stream" | ||
|
|
||
| # Collect all streamed data | ||
| data = b"" | ||
| for chunk in response.iter_content(chunk_size=None): | ||
| if chunk: | ||
| data += chunk | ||
|
|
||
| # We expect 3 chunks of 1024 bytes each | ||
| assert len(data) == 3 * 1024 | ||
|
|
||
| # Verify chunk contents: chunk i is filled with byte value i | ||
| for i in range(3): | ||
| chunk = data[i * 1024 : (i + 1) * 1024] | ||
| assert chunk == bytes([i] * 1024), f"Chunk {i} has unexpected content" | ||
|
|
||
|
|
||
| @pytest.mark.benchmark | ||
| def test_stream_bytes_no_sse_headers(session): | ||
| """Test that binary streaming responses do NOT include SSE-specific headers""" | ||
| response = requests.get(f"{BASE_URL}/stream/bytes", stream=True, timeout=5) | ||
| assert response.status_code == 200 | ||
|
|
||
| # SSE-specific headers should NOT be present for binary streams | ||
| assert response.headers.get("X-Accel-Buffering") is None | ||
| assert response.headers.get("Pragma") is None | ||
| assert response.headers.get("Expires") is None | ||
|
|
||
|
|
||
| @pytest.mark.benchmark | ||
| def test_stream_bytes_file(session): | ||
| """Test streaming a file in binary mode""" | ||
| response = requests.get(f"{BASE_URL}/stream/bytes_file", stream=True, timeout=5) | ||
| assert response.status_code == 200 | ||
| assert response.headers.get("Content-Type") == "application/octet-stream" | ||
| assert "attachment" in response.headers.get("Content-Disposition", "") | ||
|
|
||
| # Collect all streamed data | ||
| streamed_data = b"" | ||
| for chunk in response.iter_content(chunk_size=None): | ||
| if chunk: | ||
| streamed_data += chunk | ||
|
|
||
| # Read the original file to compare | ||
| test_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "build", "index.html") | ||
| with open(test_file, "rb") as f: | ||
| original_data = f.read() | ||
|
|
||
| assert streamed_data == original_data, "Streamed file content does not match original" | ||
|
|
||
|
|
||
| @pytest.mark.benchmark | ||
| def test_stream_text_still_works(session): | ||
| """Test that string-based streaming still works after the bytes change""" | ||
| response = requests.get(f"{BASE_URL}/stream/mixed_text", stream=True, timeout=5) | ||
| assert response.status_code == 200 | ||
| assert response.headers.get("Content-Type") == "text/plain" | ||
|
|
||
| content = b"" | ||
| for chunk in response.iter_content(chunk_size=None): | ||
| if chunk: | ||
| content += chunk | ||
|
|
||
| text = content.decode("utf-8") | ||
| for i in range(3): | ||
| assert f"text chunk {i}" in text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Malformed code structure will cause compilation failure.
Lines 166-170 contain extra closing braces and an orphan
Nonestatement with incorrect indentation. This appears to be leftover from a merge or edit error and will prevent the Rust code from compiling.🐛 Proposed fix
} Err(e) => { if !e.is_instance_of::<pyo3::exceptions::PyStopIteration>(py) { log::error!("Generator error: {}", e); } None } } - } - None - } }) }) .await📝 Committable suggestion
🤖 Prompt for AI Agents