Skip to content
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
35 changes: 35 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,27 @@ impl<T> Request<T> {
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
Expand Down Expand Up @@ -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();
Expand Down
31 changes: 31 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ impl<T> Response<T> {
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
Expand Down Expand Up @@ -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();
Expand Down