From 7ad66956b237394744e8b2bfab0122d4fa8460aa Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 3 Feb 2018 08:31:32 -0800 Subject: [PATCH] add HttpRequest::uri_mut(), allows to modify request uri --- src/httprequest.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index 75050233..b19cc7d0 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -195,6 +195,15 @@ impl HttpRequest { #[inline] pub fn uri(&self) -> &Uri { &self.as_ref().uri } + #[doc(hidden)] + #[inline] + /// Modify the Request Uri. + /// + /// This might be useful for middlewares, i.e. path normalization + pub fn uri_mut(&mut self) -> &mut Uri { + &mut self.as_mut().uri + } + /// Read the Request method. #[inline] pub fn method(&self) -> &Method { &self.as_ref().method } @@ -768,7 +777,7 @@ impl Future for RequestBody { mod tests { use super::*; use mime; - use http::Uri; + use http::{Uri, HttpTryFrom}; use std::str::FromStr; use router::Pattern; use resource::Resource; @@ -807,6 +816,14 @@ mod tests { assert_eq!(mt.subtype(), mime::JSON); } + #[test] + fn test_uri_mut() { + let mut req = HttpRequest::default(); + assert_eq!(req.path(), "/"); + *req.uri_mut() = Uri::try_from("/test").unwrap(); + assert_eq!(req.path(), "/test"); + } + #[test] fn test_no_request_cookies() { let req = HttpRequest::default();