diff --git a/src/httpmessage.rs b/src/httpmessage.rs index 6b59413ad..2e0d426c9 100644 --- a/src/httpmessage.rs +++ b/src/httpmessage.rs @@ -82,10 +82,20 @@ impl HttpRequest { } /// Return request cookies. - pub fn cookies(&mut self) -> &Vec> { + pub fn cookies(&self) -> &Vec> { &self.cookies } + /// Return request cookie. + pub fn cookie(&self, name: &str) -> Option<&Cookie> { + for cookie in &self.cookies { + if cookie.name() == name { + return Some(&cookie) + } + } + None + } + /// Load cookies pub fn load_cookies(&mut self) -> Result<&Vec, CookieParseError> { diff --git a/tests/test_httpmessage.rs b/tests/test_httpmessage.rs index 0ed07013c..47eaf7fc1 100644 --- a/tests/test_httpmessage.rs +++ b/tests/test_httpmessage.rs @@ -31,6 +31,12 @@ fn test_request_cookies() { assert_eq!(cookies[0].value(), "value1"); assert_eq!(cookies[1].name(), "cookie2"); assert_eq!(cookies[1].value(), "value2"); + + let cookie = req.cookie("cookie1"); + assert!(cookie.is_some()); + let cookie = cookie.unwrap(); + assert_eq!(cookie.name(), "cookie1"); + assert_eq!(cookie.value(), "value1"); } #[test]