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
Add file function to async_impl::multipart #2106
Open
NaokiM03
wants to merge
6
commits into
seanmonstar:master
Choose a base branch
from
NaokiM03:async-file-for-multipart
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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.
+117
−1
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
56c1e49
Add file function to async_impl::multipart
NaokiM03 a472eef
Add test for asynchronous file function in multipart
NaokiM03 d40a765
Fix doc of file function in blocking::multipart
NaokiM03 7175547
Merge branch 'master' into async-file-for-multipart
NaokiM03 8f63824
Merge branch 'master' into async-file-for-multipart
NaokiM03 47da118
Merge branch 'master' into async-file-for-multipart
NaokiM03 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -181,3 +181,61 @@ fn blocking_file_part() { | |
assert_eq!(res.url().as_str(), &url); | ||
assert_eq!(res.status(), reqwest::StatusCode::OK); | ||
} | ||
|
||
#[cfg(feature = "stream")] | ||
#[tokio::test] | ||
async fn async_impl_file_part() { | ||
let _ = env_logger::try_init(); | ||
|
||
let form = reqwest::multipart::Form::new() | ||
.file("foo", "Cargo.lock") | ||
.await | ||
.unwrap(); | ||
|
||
let fcontents = std::fs::read_to_string("Cargo.lock").unwrap(); | ||
|
||
let expected_body = format!( | ||
"\ | ||
--{0}\r\n\ | ||
Content-Disposition: form-data; name=\"foo\"; filename=\"Cargo.lock\"\r\n\ | ||
Content-Type: application/octet-stream\r\n\r\n\ | ||
{1}\r\n\ | ||
--{0}--\r\n\ | ||
", | ||
form.boundary(), | ||
fcontents | ||
); | ||
|
||
let ct = format!("multipart/form-data; boundary={}", form.boundary()); | ||
|
||
let server = server::http(move |mut req| { | ||
let ct = ct.clone(); | ||
let expected_body = expected_body.clone(); | ||
async move { | ||
assert_eq!(req.method(), "POST"); | ||
assert_eq!(req.headers()["content-type"], ct); | ||
assert_eq!(req.headers()["transfer-encoding"], "chunked"); | ||
|
||
let mut full: Vec<u8> = Vec::new(); | ||
while let Some(item) = req.body_mut().next().await { | ||
full.extend(&*item.unwrap()); | ||
} | ||
|
||
assert_eq!(full, expected_body.as_bytes()); | ||
|
||
http::Response::default() | ||
} | ||
}); | ||
|
||
let url = format!("http://{}/multipart/3", server.addr()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not know what these numbers mean. |
||
|
||
let res = reqwest::Client::new() | ||
.post(&url) | ||
.multipart(form) | ||
.send() | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(res.url().as_str(), &url); | ||
assert_eq!(res.status(), reqwest::StatusCode::OK); | ||
} |
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.
Variable names were different from the entities and have been corrected.