diff --git a/src/test.rs b/src/test.rs index d543937c0..ee9ef8a7a 100644 --- a/src/test.rs +++ b/src/test.rs @@ -507,6 +507,10 @@ 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 TestRequest { @@ -543,6 +547,27 @@ impl TestRequest { 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::::new() } + }; + if should_insert { + cookies.push(cookie) + }; + self.cookies = Some(cookies); + self + } + /// Set a header pub fn set(mut self, hdr: H) -> Self { if let Ok(value) = hdr.try_into() {