Feature: Add with-cookie init-method for TestRequest

This commit is contained in:
winkidney 2018-12-27 13:10:25 +08:00
parent 799c6eb719
commit 8bd8ee7b18
1 changed files with 25 additions and 0 deletions

View File

@ -507,6 +507,10 @@ impl TestRequest<()> {
{ {
TestRequest::default().header(key, value) 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> { impl<S: 'static> TestRequest<S> {
@ -543,6 +547,27 @@ impl<S: 'static> TestRequest<S> {
self self
} }
/// set cookie of this request
pub fn cookie(mut self, cookie: Cookie<'static>) -> Self {
let mut should_insert = true;
let mut cookies = match self.cookies {
Some(old_cookies) => {
for old_cookie in &old_cookies {
if old_cookie == &cookie {
should_insert = false
};
}
old_cookies
}
None => { Vec::<Cookie>::new() }
};
if should_insert {
cookies.push(cookie)
};
self.cookies = Some(cookies);
self
}
/// Set a header /// Set a header
pub fn set<H: Header>(mut self, hdr: H) -> Self { pub fn set<H: Header>(mut self, hdr: H) -> Self {
if let Ok(value) = hdr.try_into() { if let Ok(value) = hdr.try_into() {