Skip to content

Commit d364a95

Browse files
committed
add tests and forward peer and local addr
1 parent 3e2e84c commit d364a95

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/request.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ impl<State> Request<State> {
309309
pub fn is_empty(&self) -> Option<bool> {
310310
Some(self.request.len()? == 0)
311311
}
312+
313+
/// Peer address of the underlying transport
314+
#[must_use]
315+
pub fn peer_addr(&self) -> Option<&str> {
316+
self.request.peer_addr()
317+
}
318+
319+
/// Local address of the underlying transport
320+
#[must_use]
321+
pub fn local_addr(&self) -> Option<&str> {
322+
self.request.local_addr()
323+
}
312324
}
313325

314326
impl<State> AsMut<http::Request> for Request<State> {

tests/server.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ fn hello_world() -> Result<(), http_types::Error> {
1212
let port = test_utils::find_port().await;
1313
let server = task::spawn(async move {
1414
let mut app = tide::new();
15-
app.at("/").get(|mut req: Request<()>| async move {
15+
app.at("/").get(move |mut req: Request<()>| async move {
1616
assert_eq!(req.body_string().await.unwrap(), "nori".to_string());
17+
assert!(req.local_addr().unwrap().contains(&port.to_string()));
18+
assert!(req.peer_addr().is_some());
1719
let res = Response::new(StatusCode::Ok).body_string("says hello".to_string());
1820
Ok(res)
1921
});

0 commit comments

Comments
 (0)