-
-
Notifications
You must be signed in to change notification settings - Fork 103
feat(multipart): allow reusing types other than stream iterators #571
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
+255
−73
Merged
Changes from all commits
Commits
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
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,90 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| import wreq | ||
| from wreq import Multipart, Part | ||
|
|
||
| client = wreq.Client(tls_info=True) | ||
|
|
||
|
|
||
| def assert_form_value(data, key, expected): | ||
| value = data["form"][key] | ||
| if isinstance(value, list): | ||
| assert expected in value | ||
| else: | ||
| assert value == expected | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.flaky(reruns=3, reruns_delay=2) | ||
| async def test_reuse_multipart_with_clonable_parts(): | ||
| form = Multipart( | ||
| Part(name="a", value="1"), | ||
| Part(name="b", value=b"2"), | ||
| Part(name="c", value=Path("./README.md"), filename="README.md", mime="text/plain"), | ||
| ) | ||
|
|
||
| for _ in range(3): | ||
| resp = await client.post("https://httpbin.io/post", multipart=form) | ||
| async with resp: | ||
| assert resp.status.is_success() | ||
| data = await resp.json() | ||
| assert_form_value(data, "a", "1") | ||
| assert_form_value(data, "b", "2") | ||
| assert "c" in data["files"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.flaky(reruns=3, reruns_delay=2) | ||
| async def test_stream_part_is_one_shot_when_reusing_multipart(): | ||
| def file_stream(path): | ||
| with open(path, "rb") as f: | ||
| while chunk := f.read(1024): | ||
| yield chunk | ||
|
|
||
| form = Multipart( | ||
| Part( | ||
| name="stream", | ||
| value=file_stream("./README.md"), | ||
| filename="README.md", | ||
| mime="text/plain", | ||
| ), | ||
| ) | ||
|
|
||
| resp = await client.post("https://httpbin.io/post", multipart=form) | ||
| async with resp: | ||
| assert resp.status.is_success() | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
| resp = await client.post("https://httpbin.io/post", multipart=form) | ||
| async with resp: | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.flaky(reruns=3, reruns_delay=2) | ||
| async def test_reuse_same_part_without_copy_for_clonable_value(): | ||
| part = Part(name="a", value="1") | ||
|
|
||
| form1 = Multipart(part) | ||
| form2 = Multipart(part) | ||
|
|
||
| for form in (form1, form2): | ||
| resp = await client.post("https://httpbin.io/post", multipart=form) | ||
| async with resp: | ||
| assert resp.status.is_success() | ||
| data = await resp.json() | ||
| assert_form_value(data, "a", "1") | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.flaky(reruns=3, reruns_delay=2) | ||
| async def test_reuse_same_part_without_copy_fails_for_stream_value(): | ||
| def bytes_stream(): | ||
| yield b"hello" | ||
|
|
||
| part = Part(name="stream", value=bytes_stream()) | ||
| Multipart(part) | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
| Multipart(part) |
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.
Using
block_oninsidebuild_inneris risky and can lead to panics if this code is executed on a thread that is already part of a Tokio runtime (e.g., the event loop thread). SinceMultipart::extractis called during argument parsing for async functions likeexecute_request, it is highly likely to be called from such a context.Consider making
build_inner,into_inner, andbuild_formasynchronous. You can then defer the actual construction of themultipart::Formto theexecute_requestfunction insrc/client/req.rs, where it can be properly awaited without blocking the thread. This would also involve changingMultipart::extractto only clone the parts and leave theformfield asNoneinitially.