Skip to content

Commit

Permalink
trillium_http::Conn::response_headers → response_headers_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Aug 11, 2021
1 parent 2ef1147 commit 333ba79
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 29 deletions.
3 changes: 2 additions & 1 deletion http/examples/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ async fn handler(mut conn: Conn<TcpStream>) -> Conn<TcpStream> {
log::info!("< {}", String::from_utf8(chunk).unwrap());
}
conn.set_response_body("Hello world");
conn.response_headers().insert("Content-type", "text/plain");
conn.response_headers_mut()
.insert("Content-type", "text/plain");
conn.set_status(200);
conn
}
Expand Down
3 changes: 2 additions & 1 deletion http/examples/tokio-http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ async fn handler(mut conn: Conn<Compat<TcpStream>>) -> Conn<Compat<TcpStream>> {
log::info!("< {}", String::from_utf8(chunk).unwrap());
}
conn.set_response_body("Hello world");
conn.response_headers().insert("Content-type", "text/plain");
conn.response_headers_mut()
.insert("Content-type", "text/plain");
conn.set_status(200);
conn
}
Expand Down
19 changes: 13 additions & 6 deletions http/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,26 @@ where
&mut self.state
}

/// returns an immutable reference to the request headers. it is
/// not currently possible to mutate request headers for a conn
/// that has been read from a transport. For synthetic conns, use
/// [Conn<Synthetic>::request_headers_mut](Conn<trillium_http::Synthetic>::request_headers_mut)
/// returns a reference to the request headers
pub fn request_headers(&self) -> &Headers {
&self.request_headers
}

/// returns a mutable reference to the response [headers](Headers)
pub fn response_headers(&mut self) -> &mut Headers {
pub fn request_headers_mut(&mut self) -> &mut Headers {
&mut self.request_headers
}

/// returns a mutable reference to the response [headers](Headers)
pub fn response_headers_mut(&mut self) -> &mut Headers {
&mut self.response_headers
}

/// returns a reference to the response [headers](Headers)
pub fn response_headers(&self) -> &Headers {
&self.response_headers
}

/** sets the http status code from any `TryInto<StatusCode>`.
Note: This currently will set the s
Expand Down Expand Up @@ -405,7 +412,7 @@ where
# use trillium_http::{Conn, http_types::{Method, Body}};
let mut conn = Conn::new_synthetic(Method::Get, "/", ());
assert_eq!(conn.response_encoding(), encoding_rs::WINDOWS_1252); // the default
conn.response_headers().insert("content-type", "text/plain;charset=utf-16");
conn.response_headers_mut().insert("content-type", "text/plain;charset=utf-16");
assert_eq!(conn.response_encoding(), encoding_rs::UTF_16LE);
```
*/
Expand Down
17 changes: 0 additions & 17 deletions http/src/synthetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,6 @@ impl Conn<Synthetic> {
}
}

/**
A Conn<Synthetic> provides the ability to mutate request headers
with `request_headers_mut`. This is only provided on synthetic
requests for now, since it doesn't generally make sense to mutate
headers for a request that is read from an io transport.
```rust
# use trillium_http::{http_types::Method, Conn};
let mut conn = Conn::new_synthetic(Method::Get, "/", "hello");
conn.request_headers_mut().insert("content-type", "application/json");
assert_eq!(conn.request_headers()["content-type"], "application/json");
```
*/
pub fn request_headers_mut(&mut self) -> &mut Headers {
&mut self.request_headers
}

/**
Replaces the synthetic body. This is intended for testing use.
*/
Expand Down
6 changes: 3 additions & 3 deletions testing/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ macro_rules! assert_response {
($conn:expr, $status:expr, $body:expr, $($header_name:literal => $header_value:expr),*) => {
let mut conn = $conn;
$crate::assert_response!(&mut conn, $status, $body);
$crate::assert_headers!(&mut conn, $($header_name => $header_value),*);
$crate::assert_headers!(&conn, $($header_name => $header_value),*);
};

}
Expand Down Expand Up @@ -247,8 +247,8 @@ macro_rules! assert_headers {
};

($conn:expr, $($header_name:literal => $header_value:expr),*) => {
let mut conn = $conn;
let headers = conn.inner_mut().response_headers();
let conn = $conn;
let headers = conn.inner().response_headers();
$(
assert_eq!(
headers.get($header_name).map(|h| h.as_str()),
Expand Down
2 changes: 1 addition & 1 deletion trillium/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl Conn {
///
/// stability note: this may become `response_headers` at some point
pub fn headers_mut(&mut self) -> &mut Headers {
self.inner.response_headers()
self.inner.response_headers_mut()
}

/**
Expand Down

0 comments on commit 333ba79

Please sign in to comment.