Skip to content
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

Build a blocking body from bytes::Bytes to allow copy-free chained network calls. #1114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/blocking/body.rs
Expand Up @@ -203,6 +203,14 @@ impl From<File> for Body {
}
}
}
impl From<Bytes> for Body {
#[inline]
fn from(b: Bytes) -> Body {
Body {
kind: Kind::Bytes(b),
}
}
}

impl fmt::Debug for Kind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
16 changes: 16 additions & 0 deletions tests/blocking.rs
Expand Up @@ -310,3 +310,19 @@ fn test_allowed_methods_blocking() {

assert_eq!(resp.is_err(), true);
}

/// Test that a [`reqwest::blocking::Body`] can be created from [`bytes::Bytes`].
#[test]
fn test_body_from_bytes() {
let body = "abc";
// No external calls are needed. Only the request building is tested.
let request = reqwest::blocking::Client::builder()
.build()
.expect("Could not build the client")
.put("https://google.com")
.body(bytes::Bytes::from(body))
.build()
.expect("Invalid body");

assert_eq!(request.body().unwrap().as_bytes(), Some(body.as_bytes()));
}