Skip to content

Commit

Permalink
Add TryFrom<HttpRequest<()>> for Request
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerhut committed Mar 21, 2024
1 parent 7a5df21 commit de4409a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/async_impl/request.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit de4409a

Please sign in to comment.