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