mirror of https://github.com/fafhrd91/actix-web
Feature: Add with-cookie init-method for TestRequest
This commit is contained in:
parent
799c6eb719
commit
8bd8ee7b18
25
src/test.rs
25
src/test.rs
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue