1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 10:42:55 +01:00

Add with-cookie init-method for TestRequest (#647)

This commit is contained in:
Ji Qu 2019-01-02 18:24:08 +08:00 committed by Douman
parent 799c6eb719
commit 61883042c2
2 changed files with 31 additions and 0 deletions

View File

@ -1,5 +1,12 @@
# Changes
## [0.7.18] - 2019-01-02
### Added
* Add `with_cookie` for `TestRequest` to allow users to customize request cookie. #647
* Add `cookie` method for `TestRequest` to allow users to add cookie dynamically.
## [0.7.17] - 2018-12-25
### Added

View File

@ -507,6 +507,11 @@ impl TestRequest<()> {
{
TestRequest::default().header(key, value)
}
/// Create TestRequest and set request cookie
pub fn with_cookie(cookie: Cookie<'static>) -> TestRequest<()> {
TestRequest::default().cookie(cookie)
}
}
impl<S: 'static> TestRequest<S> {
@ -543,6 +548,25 @@ impl<S: 'static> TestRequest<S> {
self
}
/// set cookie of this request
pub fn cookie(mut self, cookie: Cookie<'static>) -> Self {
if self.cookies.is_none() {
let mut should_insert = true;
let old_cookies = self.cookies.as_mut().unwrap();
for old_cookie in old_cookies.iter() {
if old_cookie == &cookie {
should_insert = false
};
};
if should_insert {
old_cookies.push(cookie);
};
} else {
self.cookies = Some(vec![cookie]);
};
self
}
/// Set a header
pub fn set<H: Header>(mut self, hdr: H) -> Self {
if let Ok(value) = hdr.try_into() {