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

Add empty body conversion from unit #2436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/async_impl/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ impl From<hyper::Body> for Body {
}
*/

impl From<()> for Body {
#[inline]
fn from(_: ()) -> Body {
Body::empty()
}
}

impl From<Bytes> for Body {
#[inline]
fn from(bytes: Bytes) -> Body {
Expand Down Expand Up @@ -467,6 +474,22 @@ mod tests {

use super::Body;

#[test]
fn empty() {
let body = Body::empty();
assert_eq!(body.as_bytes(), Some(&[] as &[u8]));
assert_eq!(body.size_hint().exact(), Some(0));
assert!(body.is_end_stream());
}

#[test]
fn from_unit() {
let body = Body::from(());
assert_eq!(body.as_bytes(), Some(&[] as &[u8]));
assert_eq!(body.size_hint().exact(), Some(0));
assert!(body.is_end_stream());
}

#[test]
fn test_as_bytes() {
let test_data = b"Test body";
Expand Down
14 changes: 14 additions & 0 deletions src/async_impl/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ mod tests {

use super::{Client, HttpRequest, Request, RequestBuilder, Version};
use crate::Method;
use http::HeaderMap;
use serde::Serialize;
use std::collections::BTreeMap;
use std::convert::TryFrom;
Expand Down Expand Up @@ -877,6 +878,19 @@ mod tests {
assert!(req.headers()["hiding"].is_sensitive());
}

#[test]
fn from_empty_http_request() {
let http_request = HttpRequest::builder()
.uri("http://localhost/")
.body(())
.unwrap();
let req = Request::try_from(http_request).unwrap();
assert_eq!(req.body().unwrap().as_bytes(), Some(&[] as &[u8]));
assert_eq!(req.headers(), &HeaderMap::new());
assert_eq!(req.method(), Method::GET);
assert_eq!(req.url().as_str(), "http://localhost/");
}

#[test]
fn convert_from_http_request() {
let http_request = HttpRequest::builder()
Expand Down
43 changes: 43 additions & 0 deletions src/blocking/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ impl Body {
}
}

pub(crate) fn empty() -> Body {
Body {
kind: Kind::Bytes(Bytes::new()),
}
}

#[cfg(feature = "multipart")]
pub(crate) fn len(&self) -> Option<u64> {
match self.kind {
Expand Down Expand Up @@ -167,6 +173,20 @@ impl Kind {
}
}

impl Default for Body {
#[inline]
fn default() -> Body {
Body::empty()
}
}

impl From<()> for Body {
#[inline]
fn from(_: ()) -> Body {
Body::empty()
}
}

impl From<Vec<u8>> for Body {
#[inline]
fn from(v: Vec<u8>) -> Body {
Expand Down Expand Up @@ -367,3 +387,26 @@ pub(crate) fn read_to_string(mut body: Body) -> io::Result<String> {
}
.map(|_| s)
}

#[cfg(test)]
mod tests {
use super::Body;

#[test]
fn empty() {
let body = Body::empty();
assert_eq!(body.as_bytes(), Some(&[] as &[u8]));
}

#[test]
fn from_unit() {
let body = Body::from(());
assert_eq!(body.as_bytes(), Some(&[] as &[u8]));
}

#[test]
fn from_vec() {
let body = Body::from(vec![1, 2, 3]);
assert_eq!(body.as_bytes(), Some(&[1, 2, 3][..]));
}
}
14 changes: 14 additions & 0 deletions src/blocking/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,20 @@ mod tests {
);
}

#[test]
fn from_empty_http_request() {
let http_request = HttpRequest::builder()
.method("GET")
.uri("http://localhost/")
.body(())
.unwrap();
let req = Request::try_from(http_request).unwrap();
assert_eq!(req.body().unwrap().as_bytes(), Some(&[] as &[u8]));
assert_eq!(req.headers(), &HeaderMap::new());
assert_eq!(req.method(), Method::GET);
assert_eq!(req.url().as_str(), "http://localhost/");
}

#[test]
fn convert_from_http_request() {
let http_request = HttpRequest::builder()
Expand Down
20 changes: 20 additions & 0 deletions src/wasm/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ impl Body {
Inner::MultipartForm(_) => None,
}
}

#[inline]
pub(crate) fn empty() -> Body {
Body {
inner: Inner::Single(Single::Bytes(Bytes::new())),
}
}
}

impl From<()> for Body {
#[inline]
fn from(_: ()) -> Body {
Body::empty()
}
}

impl From<Bytes> for Body {
Expand Down Expand Up @@ -203,6 +217,12 @@ mod tests {
fn log(s: String);
}

#[wasm_bindgen_test]
async fn test_body_empty() {
let body = Body::empty();
assert_eq!(body.as_bytes(), Some(&[] as &[u8]));
}

#[wasm_bindgen_test]
async fn test_body() {
let body = Body::from("TEST");
Expand Down