1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
#[derive(Debug)]
pub struct CHR<O> {
pub(crate) cookies: Vec<http::HeaderValue>,
pub(crate) headers: Vec<(http::header::HeaderName, http::HeaderValue)>,
pub(crate) response: O,
}
impl<O> CHR<O> {
pub(crate) fn new(response: O) -> Self {
Self {
cookies: Vec::new(),
headers: Vec::new(),
response,
}
}
}
pub(crate) fn chr(
cookies: Vec<http::HeaderValue>,
headers: Vec<(http::header::HeaderName, http::HeaderValue)>,
mut response: http::Response<bytes::Bytes>,
) -> Result<http::Response<bytes::Bytes>, ft_sdk::Error> {
for cookie in cookies.into_iter() {
response
.headers_mut()
.try_append(http::header::SET_COOKIE, cookie)?;
}
for (k, v) in headers.into_iter() {
response.headers_mut().insert(k, v);
}
Ok(response)
}
impl<O> CHR<O> {
pub fn with_cookie(mut self, c: http::HeaderValue) -> Self {
self.cookies.push(c);
self
}
pub fn with_header(mut self, key: http::HeaderName, value: http::HeaderValue) -> Self {
self.headers.push((key, value));
self
}
}
#[cfg(test)]
mod test {
#[test]
fn header() {
let r = ft_sdk::json(()).unwrap();
let chr = super::CHR::new(()).with_header(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("text/html"),
);
let r = super::chr(chr.cookies, chr.headers, r).unwrap();
assert_eq!(
r.headers().get(http::header::CONTENT_TYPE),
Some(&http::HeaderValue::from_static("text/html"))
);
}
// #[test]
// fn cookie() {
// let r = ft_sdk::json(()).unwrap();
// let chr = super::CHR::new(()).with_cookie(("name", "value"));
// let r = super::chr(chr.cookies, chr.headers, r).unwrap();
//
// let cookies = r.headers().get_all(http::header::SET_COOKIE);
// let mut iter = cookies.iter();
// assert_eq!(
// iter.next(),
// Some(&http::HeaderValue::from_static(
// "name=value; Secure; HttpOnly; SameSite=Strict; Max-Age=34560000"
// ))
// );
// assert_eq!(iter.next(), None);
//
// let r = ft_sdk::json(()).unwrap();
// let chr = super::CHR::new(()).with_cookie(("name", "value", 200));
// let r = super::chr(chr.cookies, chr.headers, r).unwrap();
//
// let cookies = r.headers().get_all(http::header::SET_COOKIE);
// let mut iter = cookies.iter();
// assert_eq!(
// iter.next(),
// Some(&http::HeaderValue::from_static(
// "name=value; Secure; HttpOnly; SameSite=Strict; Max-Age=200"
// ))
// );
// assert_eq!(iter.next(), None);
//
// let r = ft_sdk::json(()).unwrap();
// let chr = super::CHR::new(())
// .with_cookie(("name", "value"))
// .with_cookie(("n2", "v2"));
// let r = super::chr(chr.cookies, chr.headers, r).unwrap();
//
// let cookies = r.headers().get_all(http::header::SET_COOKIE);
// let mut iter = cookies.iter();
// assert_eq!(
// iter.next(),
// Some(&http::HeaderValue::from_static(
// "name=value; Secure; HttpOnly; SameSite=Strict; Max-Age=34560000"
// ))
// );
// assert_eq!(
// iter.next(),
// Some(&http::HeaderValue::from_static(
// "n2=v2; Secure; HttpOnly; SameSite=Strict; Max-Age=34560000"
// ))
// );
// }
//
// #[test]
// fn raw_cookie() {
// let r = ft_sdk::json(()).unwrap();
// let chr = super::CHR::new(()).with_cookie(http::HeaderValue::from_static("hello"));
// let r = super::chr(chr.cookies, chr.headers, r).unwrap();
//
// let cookies = r.headers().get_all(http::header::SET_COOKIE);
// let mut iter = cookies.iter();
// assert_eq!(iter.next(), Some(&http::HeaderValue::from_static("hello")));
// assert_eq!(iter.next(), None);
//
// let r = ft_sdk::json(()).unwrap();
// let chr = super::CHR::new(())
// .with_cookie(http::HeaderValue::from_static("hello"))
// .with_cookie(http::HeaderValue::from_static("hello"));
// let r = super::chr(chr.cookies, chr.headers, r).unwrap();
//
// let cookies = r.headers().get_all(http::header::SET_COOKIE);
// let mut iter = cookies.iter();
// assert_eq!(
// iter.next(),
// Some(&http::HeaderValue::from_static(
// "name=value; Secure; HttpOnly; SameSite=Strict; Max-Age=34560000"
// ))
// );
// assert_eq!(iter.next(), Some(&http::HeaderValue::from_static("hello")));
// }
}