diff --git a/src/request.rs b/src/request.rs index f47c6630..08c612e7 100644 --- a/src/request.rs +++ b/src/request.rs @@ -453,6 +453,27 @@ impl Request { Request { head: parts, body } } + /// Returns a reference to the request's component parts. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let request = Request::builder() + /// .method(Method::POST) + /// .uri("/hello") + /// .body(()) + /// .unwrap(); + /// + /// let parts = request.parts(); + /// assert_eq!(parts.method, Method::POST); + /// assert_eq!(parts.uri, "/hello"); + /// ``` + #[inline] + pub fn parts(&self) -> &Parts { + &self.head + } + /// Returns a reference to the associated HTTP method. /// /// # Examples @@ -1060,6 +1081,20 @@ impl Default for Builder { mod tests { use super::*; + #[test] + fn it_can_borrow_parts() { + let request = Request::builder() + .method(Method::POST) + .uri("/hello") + .body("body") + .unwrap(); + + let parts = request.parts(); + assert_eq!(parts.method, Method::POST); + assert_eq!(parts.uri, "/hello"); + assert_eq!(request.body(), &"body"); + } + #[test] fn it_can_map_a_body_from_one_type_to_another() { let request = Request::builder().body("some string").unwrap(); diff --git a/src/response.rs b/src/response.rs index b8fc9b2a..95e81e7f 100644 --- a/src/response.rs +++ b/src/response.rs @@ -271,6 +271,25 @@ impl Response { Response { head: parts, body } } + /// Returns a reference to the response's component parts. + /// + /// # Examples + /// + /// ``` + /// # use http::*; + /// let response = Response::builder() + /// .status(StatusCode::CREATED) + /// .body(()) + /// .unwrap(); + /// + /// let parts = response.parts(); + /// assert_eq!(parts.status, StatusCode::CREATED); + /// ``` + #[inline] + pub fn parts(&self) -> &Parts { + &self.head + } + /// Returns the `StatusCode`. /// /// # Examples @@ -769,6 +788,18 @@ impl Default for Builder { mod tests { use super::*; + #[test] + fn it_can_borrow_parts() { + let response = Response::builder() + .status(StatusCode::CREATED) + .body("body") + .unwrap(); + + let parts = response.parts(); + assert_eq!(parts.status, StatusCode::CREATED); + assert_eq!(response.body(), &"body"); + } + #[test] fn it_can_map_a_body_from_one_type_to_another() { let response = Response::builder().body("some string").unwrap();