From 73990a7a42267548a87945f22f08c951656191b5 Mon Sep 17 00:00:00 2001 From: varoonp123 Date: Tue, 15 Dec 2020 10:22:44 -0500 Subject: [PATCH] Add From for blocking::Body (#1114) --- src/blocking/body.rs | 8 ++++++++ tests/blocking.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/blocking/body.rs b/src/blocking/body.rs index 9171ac7..3cb0d1c 100644 --- a/src/blocking/body.rs +++ b/src/blocking/body.rs @@ -203,6 +203,14 @@ impl From for Body { } } } +impl From 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 { diff --git a/tests/blocking.rs b/tests/blocking.rs index aa1c2a2..c98a2c3 100644 --- a/tests/blocking.rs +++ b/tests/blocking.rs @@ -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())); +}