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 TryFrom<HttpRequest<()>> for Request #2196

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/async_impl/request.rs
Expand Up @@ -622,6 +622,30 @@ where
}
}

impl TryFrom<HttpRequest<()>> for Request {
type Error = crate::Error;

fn try_from(req: HttpRequest<()>) -> crate::Result<Self> {
let (parts, _) = req.into_parts();
let Parts {
method,
uri,
headers,
version,
..
} = parts;
let url = Url::parse(&uri.to_string()).map_err(crate::error::builder)?;
Ok(Request {
method,
url,
headers,
body: None,
timeout: None,
version,
})
}
}

impl TryFrom<Request> for HttpRequest<Body> {
type Error = crate::Error;

Expand Down Expand Up @@ -890,6 +914,17 @@ mod tests {
assert_eq!(req.url().as_str(), "http://localhost/");
}

#[test]
fn convert_from_http_request_without_body() {
let http_request = HttpRequest::builder()
.method("GET")
.uri("http://localhost/")
.body(())
.unwrap();
let req: Request = Request::try_from(http_request).unwrap();
assert!(req.body().is_none());
}

#[test]
fn set_http_request_version() {
let http_request = HttpRequest::builder()
Expand Down
29 changes: 29 additions & 0 deletions src/blocking/request.rs
Expand Up @@ -640,6 +640,24 @@ where
}
}

impl TryFrom<HttpRequest<()>> for Request {
type Error = crate::Error;

fn try_from(req: HttpRequest<()>) -> crate::Result<Self> {
let (parts, body) = req.into_parts();
let Parts {
method,
uri,
headers,
..
} = parts;
let url = Url::parse(&uri.to_string()).map_err(crate::error::builder)?;
let mut inner = async_impl::Request::new(method, url);
crate::util::replace_headers(inner.headers_mut(), headers);
Ok(Request { body: None, inner })
}
}

impl fmt::Debug for Request {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_request_fields(&mut f.debug_struct("Request"), self).finish()
Expand Down Expand Up @@ -1006,6 +1024,17 @@ mod tests {
assert_eq!(req.url().as_str(), "http://localhost/");
}

#[test]
fn convert_from_http_request_without_body() {
let http_request = HttpRequest::builder()
.method("GET")
.uri("http://localhost/")
.body(())
.unwrap();
let req: Request = Request::try_from(http_request).unwrap();
assert!(req.body().is_none());
}

#[test]
fn set_http_request_version() {
let http_request = HttpRequest::builder()
Expand Down
23 changes: 23 additions & 0 deletions src/wasm/request.rs
Expand Up @@ -455,6 +455,29 @@ where
}
}

impl TryFrom<HttpRequest<()>> for Request {
type Error = crate::Error;

fn try_from(req: HttpRequest<()>) -> crate::Result<Self> {
let (parts, body) = req.into_parts();
let Parts {
method,
uri,
headers,
..
} = parts;
let url = Url::parse(&uri.to_string()).map_err(crate::error::builder)?;
Ok(Request {
method,
url,
headers,
body: None,
cors: true,
credentials: None,
})
}
}

impl TryFrom<Request> for HttpRequest<Body> {
type Error = crate::Error;

Expand Down