use kekse::{Cookie, CookieAttributes, Path, SameSite, SetCookie, ValueEncoding};
fn main() {
let header = SetCookie::new("SID", "deadbeef")
.http_only()
.secure()
.same_site(SameSite::Strict)
.path(Path::new("/").expect("`/` is av-octet-clean"))
.max_age(3600)
.to_set_cookie();
println!("1. built: {header}");
assert_eq!(
header,
"SID=deadbeef; HttpOnly; SameSite=Strict; Secure; Path=/; Max-Age=3600"
);
let cookie = SetCookie::new("SID", "deadbeef").http_only().secure();
let header_value = http::HeaderValue::try_from(&cookie)
.expect("a managed encoding is always a valid HeaderValue");
println!("2. HeaderValue: {header_value:?}");
let hardened = CookieAttributes::default()
.http_only()
.secure()
.same_site(SameSite::Lax)
.path(Path::new("/").expect("`/` is av-octet-clean"));
for (name, value) in [("SID", "deadbeef"), ("csrf", "tok-123")] {
let line = Cookie::new(name, value)
.with_attributes(hardened.clone())
.to_set_cookie();
println!("3. policy: {line}");
}
let attrs = CookieAttributes {
http_only: true,
secure: true,
same_site: Some(SameSite::Strict),
path: Path::new("/").ok(),
max_age: Some(3600),
..Default::default()
};
let literal = SetCookie::new("SID", "deadbeef")
.with_attributes(attrs)
.to_set_cookie();
println!("4. literal: {literal}");
assert_eq!(
literal,
"SID=deadbeef; HttpOnly; SameSite=Strict; Secure; Path=/; Max-Age=3600"
);
let wire = SetCookie::new("pref", "a b")
.with_encoding(ValueEncoding::Percent)
.max_age(60)
.to_set_cookie();
let parsed = SetCookie::parse(&wire)
.expect("our own output round-trips")
.into_value();
println!(
"5. round-trip: {wire:?} -> name={:?} value={:?} max_age={:?}",
parsed.name(),
parsed.value(),
parsed.attributes().max_age
);
assert_eq!(parsed.name(), "pref");
assert_eq!(parsed.value(), "a b");
assert_eq!(parsed.attributes().max_age, Some(60));
}