From 61883042c2ff103b9fcf2aa94e66c0f3a50ef45c Mon Sep 17 00:00:00 2001 From: Ji Qu Date: Wed, 2 Jan 2019 18:24:08 +0800 Subject: [PATCH] Add with-cookie init-method for TestRequest (#647) --- CHANGES.md | 7 +++++++ src/test.rs | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 1c55f2ac..a07388d3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/src/test.rs b/src/test.rs index d543937c..7039c4fc 100644 --- a/src/test.rs +++ b/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 TestRequest { @@ -543,6 +548,25 @@ impl TestRequest { 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(mut self, hdr: H) -> Self { if let Ok(value) = hdr.try_into() {