use crate::Cookies;
use cookie::{Cookie, Key};
pub struct SignedCookies<'a> {
cookies: Cookies,
key: &'a Key,
}
impl<'a> SignedCookies<'a> {
pub(crate) fn new(cookies: &Cookies, key: &'a Key) -> Self {
Self {
cookies: cookies.clone(),
key,
}
}
pub fn add(&self, cookie: Cookie<'static>) {
let mut inner = self.cookies.inner.lock();
inner.changed = true;
inner.jar().signed_mut(self.key).add(cookie);
}
pub fn get(&self, name: &str) -> Option<Cookie<'static>> {
let mut inner = self.cookies.inner.lock();
inner.jar().signed(self.key).get(name)
}
pub fn remove(&self, cookie: Cookie<'static>) {
self.cookies.remove(cookie);
}
}
#[cfg(all(test, feature = "signed"))]
mod tests {
use crate::Cookies;
use cookie::{Cookie, Key};
#[test]
fn get_absent() {
let key = Key::generate();
let cookies = Cookies::new(vec![]);
assert_eq!(cookies.signed(&key).get("foo"), None);
}
#[test]
fn add_get_signed() {
let key = Key::generate();
let cookies = Cookies::new(vec![]);
let cookie = Cookie::new("foo", "bar");
let signed = cookies.signed(&key);
signed.add(cookie.clone());
assert_eq!(signed.get("foo").unwrap(), cookie);
}
#[test]
fn add_signed_get_raw() {
let key = Key::generate();
let cookies = Cookies::new(vec![]);
let cookie = Cookie::new("foo", "bar");
cookies.signed(&key).add(cookie.clone());
assert_ne!(cookies.get("foo").unwrap(), cookie);
}
#[test]
fn add_raw_get_signed() {
let key = Key::generate();
let cookies = Cookies::new(vec![]);
let cookie = Cookie::new("foo", "bar");
cookies.add(cookie);
assert_eq!(cookies.signed(&key).get("foo"), None);
}
#[test]
fn messed_keys() {
let key1 = Key::generate();
let key2 = Key::generate();
let cookies = Cookies::new(vec![]);
let cookie = Cookie::new("foo", "bar");
cookies.signed(&key1).add(cookie);
assert_eq!(cookies.signed(&key2).get("foo"), None);
}
#[test]
fn remove() {
let key = Key::generate();
let cookies = Cookies::new(vec![]);
let signed = cookies.signed(&key);
signed.add(Cookie::new("foo", "bar"));
let cookie = signed.get("foo").unwrap();
signed.remove(cookie);
assert!(signed.get("foo").is_none());
}
}