proptest_http/
request.rs

1use super::*;
2
3#[derive(Debug)]
4pub struct ArbitraryRequest(pub http::request::Request<()>);
5#[derive(Debug, Clone, Copy)]
6pub struct RequestStrategy;
7#[derive(Debug, Clone)]
8pub struct RequestValueTree(
9    TupleValueTree<(
10        super::uri::UriValueTree,
11        MethodValueTree,
12        super::header::HeaderMapValueTree,
13    )>,
14);
15
16impl Arbitrary for ArbitraryRequest {
17    type Strategy = RequestStrategy;
18    type Parameters = ();
19
20    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
21        RequestStrategy
22    }
23}
24impl Strategy for RequestStrategy {
25    type Tree = RequestValueTree;
26    type Value = ArbitraryRequest;
27
28    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
29        Ok(RequestValueTree(
30            <(ArbitraryUri, ArbitraryMethod, ArbitraryHeaderMap)>::arbitrary().new_tree(runner)?,
31        ))
32    }
33}
34impl ValueTree for RequestValueTree {
35    type Value = ArbitraryRequest;
36
37    fn current(&self) -> Self::Value {
38        let mut b = http::request::Builder::default();
39        let (ArbitraryUri(u), ArbitraryMethod(m), ArbitraryHeaderMap(h)) = self.0.current();
40        b.uri(u);
41        b.method(m);
42        *b.headers_mut().unwrap() = h;
43        ArbitraryRequest(b.body(()).unwrap())
44    }
45
46    fn simplify(&mut self) -> bool {
47        self.0.simplify()
48    }
49
50    fn complicate(&mut self) -> bool {
51        self.0.complicate()
52    }
53}
54
55//-----------------------------
56
57#[derive(Debug, Eq, PartialEq)]
58pub struct ArbitraryMethod(pub http::method::Method);
59#[derive(Debug, Clone, Copy)]
60pub struct MethodStrategy;
61#[derive(Debug, Clone)]
62pub struct MethodValueTree(IndexValueTree);
63
64const METHODS: [http::Method; 5] = [
65    http::method::Method::GET,
66    http::method::Method::POST,
67    http::method::Method::PUT,
68    http::method::Method::DELETE,
69    http::method::Method::TRACE,
70];
71
72impl Arbitrary for ArbitraryMethod {
73    type Strategy = MethodStrategy;
74    type Parameters = ();
75
76    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
77        MethodStrategy
78    }
79}
80impl Strategy for MethodStrategy {
81    type Tree = MethodValueTree;
82    type Value = ArbitraryMethod;
83
84    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
85        Ok(MethodValueTree(Index::arbitrary().new_tree(runner)?))
86    }
87}
88impl ValueTree for MethodValueTree {
89    type Value = ArbitraryMethod;
90
91    fn current(&self) -> Self::Value {
92        ArbitraryMethod(self.0.current().get(&METHODS).clone())
93    }
94
95    fn simplify(&mut self) -> bool {
96        self.0.simplify()
97    }
98
99    fn complicate(&mut self) -> bool {
100        self.0.complicate()
101    }
102}