mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 01:51:23 +02:00
Add with-cookie init-method for TestRequest (#647)
This commit is contained in:
24
src/test.rs
24
src/test.rs
@ -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() {
|
||||
|
Reference in New Issue
Block a user