proptest_http/
header.rs

1use super::*;
2
3#[derive(Debug, Clone, Eq, PartialEq)]
4pub struct ArbitraryHeaderMap(pub http::header::HeaderMap);
5#[derive(Debug, Clone, Copy)]
6pub struct HeaderMapStrategy;
7#[derive(Debug, Clone)]
8pub struct HeaderMapValueTree(
9    VecValueTree<TupleValueTree<(HeaderNameValueTree, HeaderValueValueTree)>>,
10);
11
12impl Arbitrary for ArbitraryHeaderMap {
13    type Strategy = HeaderMapStrategy;
14    type Parameters = ();
15
16    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
17        HeaderMapStrategy
18    }
19}
20impl Strategy for HeaderMapStrategy {
21    type Tree = HeaderMapValueTree;
22    type Value = ArbitraryHeaderMap;
23
24    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
25        Ok(HeaderMapValueTree(
26            Vec::<(ArbitraryHeaderName, ArbitraryHeaderValue)>::arbitrary().new_tree(runner)?,
27        ))
28    }
29}
30impl ValueTree for HeaderMapValueTree {
31    type Value = ArbitraryHeaderMap;
32
33    fn current(&self) -> Self::Value {
34        let q = self.0.current();
35        let mut hm = http::header::HeaderMap::with_capacity(q.len());
36        for (n, v) in q {
37            hm.insert(n.0, v.0);
38        }
39        ArbitraryHeaderMap(hm)
40    }
41
42    fn simplify(&mut self) -> bool {
43        self.0.simplify()
44    }
45
46    fn complicate(&mut self) -> bool {
47        self.0.complicate()
48    }
49}
50
51// ----------------------------
52
53static HEADER_NAMES: [http::header::HeaderName; 15] = [
54    http::header::HOST,
55    http::header::CONTENT_LENGTH,
56    http::header::CONTENT_TYPE,
57    http::header::AUTHORIZATION,
58    http::header::USER_AGENT,
59    http::header::ACCEPT,
60    http::header::ACCEPT_LANGUAGE,
61    http::header::ACCEPT_ENCODING,
62    http::header::UPGRADE,
63    http::header::CONNECTION,
64    http::header::DNT,
65    http::header::CACHE_CONTROL,
66    http::header::DATE,
67    http::header::EXPIRES,
68    http::header::SERVER,
69];
70
71#[derive(Debug, Clone, Eq, PartialEq)]
72pub struct ArbitraryHeaderName(pub http::header::HeaderName);
73#[derive(Debug, Clone, Copy)]
74pub struct HeaderNameStrategy;
75#[derive(Debug, Clone)]
76pub struct HeaderNameValueTree(IndexValueTree);
77
78impl Arbitrary for ArbitraryHeaderName {
79    type Strategy = HeaderNameStrategy;
80    type Parameters = ();
81
82    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
83        HeaderNameStrategy
84    }
85}
86impl Strategy for HeaderNameStrategy {
87    type Tree = HeaderNameValueTree;
88    type Value = ArbitraryHeaderName;
89
90    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
91        Ok(HeaderNameValueTree(Index::arbitrary().new_tree(runner)?))
92    }
93}
94impl ValueTree for HeaderNameValueTree {
95    type Value = ArbitraryHeaderName;
96
97    fn current(&self) -> Self::Value {
98        ArbitraryHeaderName(self.0.current().get(&HEADER_NAMES).clone())
99    }
100
101    fn simplify(&mut self) -> bool {
102        self.0.simplify()
103    }
104
105    fn complicate(&mut self) -> bool {
106        self.0.complicate()
107    }
108}
109
110// -------------------
111
112#[derive(Debug, Clone, Eq, PartialEq)]
113pub struct ArbitraryHeaderValue(pub http::header::HeaderValue);
114#[derive(Debug, Clone, Copy)]
115pub struct HeaderValueStrategy;
116#[derive(Debug, Clone)]
117pub struct HeaderValueValueTree(IndexValueTree);
118
119const HEADER_VALUES: [&str; 30] = [
120    "",
121    "0",
122    "1",
123    ":",
124    "\t",
125    "%",
126    "\"\"",
127    "%D1%89",
128    "100000",
129    "\"%D1%89\"",
130    "\\r\\n",
131    "deflate",
132    "localhost",
133    "close",
134    "en-GB",
135    "text/html",
136    "keep-alive",
137    "websocket",
138    "example.com",
139    "example.com:1234",
140    "max-age=604800",
141    "en-GB,en;q=0.5",
142    "Accept-Encoding",
143    "EOS (vny006/0453)",
144    "Thu, 20 Jun 2019 21:06:20 GMT",
145    "text/html; charset=UTF-8",
146    "999999999999999999999999999999999999999999999999999999",
147    "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
148    "_xsrf=2|8bea5404|5ef47a59a0516e67bbd5f86849e28a1c|1553532280",
149    "Mozilla/5.0 (X11; Linux i686 on x86_64; rv:52.1) Gecko/20100101 Firefox/52.1",
150];
151
152impl Arbitrary for ArbitraryHeaderValue {
153    type Strategy = HeaderValueStrategy;
154    type Parameters = ();
155
156    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
157        HeaderValueStrategy
158    }
159}
160impl Strategy for HeaderValueStrategy {
161    type Tree = HeaderValueValueTree;
162    type Value = ArbitraryHeaderValue;
163
164    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
165        Ok(HeaderValueValueTree(Index::arbitrary().new_tree(runner)?))
166    }
167}
168impl ValueTree for HeaderValueValueTree {
169    type Value = ArbitraryHeaderValue;
170
171    fn current(&self) -> Self::Value {
172        ArbitraryHeaderValue(http::header::HeaderValue::from_static(
173            &self.0.current().get(&HEADER_VALUES),
174        ))
175    }
176
177    fn simplify(&mut self) -> bool {
178        self.0.simplify()
179    }
180
181    fn complicate(&mut self) -> bool {
182        self.0.complicate()
183    }
184}