proptest_http/
response.rs

1use super::*;
2
3#[derive(Debug)]
4pub struct ArbitraryResponse(pub http::response::Response<()>);
5#[derive(Debug, Clone, Copy)]
6pub struct ResponseStrategy;
7#[derive(Debug, Clone)]
8pub struct ResponseValueTree(
9    TupleValueTree<(StatusCodeValueTree, super::header::HeaderMapValueTree)>,
10);
11
12impl Arbitrary for ArbitraryResponse {
13    type Strategy = ResponseStrategy;
14    type Parameters = ();
15
16    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
17        ResponseStrategy
18    }
19}
20impl Strategy for ResponseStrategy {
21    type Tree = ResponseValueTree;
22    type Value = ArbitraryResponse;
23
24    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
25        Ok(ResponseValueTree(
26            <(ArbitraryStatusCode, ArbitraryHeaderMap)>::arbitrary().new_tree(runner)?,
27        ))
28    }
29}
30impl ValueTree for ResponseValueTree {
31    type Value = ArbitraryResponse;
32
33    fn current(&self) -> Self::Value {
34        let mut b = http::response::Builder::default();
35        let (ArbitraryStatusCode(s), ArbitraryHeaderMap(h)) = self.0.current();
36        b.status(s);
37        *b.headers_mut().unwrap() = h;
38        ArbitraryResponse(b.body(()).unwrap())
39    }
40
41    fn simplify(&mut self) -> bool {
42        self.0.simplify()
43    }
44
45    fn complicate(&mut self) -> bool {
46        self.0.complicate()
47    }
48}
49
50//-----------------------------
51
52#[derive(Debug, Eq, PartialEq)]
53pub struct ArbitraryStatusCode(pub http::status::StatusCode);
54#[derive(Debug, Clone, Copy)]
55pub struct StatusCodeStrategy;
56#[derive(Debug, Clone)]
57pub struct StatusCodeValueTree(IndexValueTree);
58
59const STATUS_CODES: [http::status::StatusCode; 14] = [
60    http::StatusCode::OK,
61    http::StatusCode::NOT_FOUND,
62    http::StatusCode::FORBIDDEN,
63    http::StatusCode::BAD_REQUEST,
64    http::StatusCode::NO_CONTENT,
65    http::StatusCode::FOUND,
66    http::StatusCode::GONE,
67    http::StatusCode::CONTINUE,
68    http::StatusCode::UPGRADE_REQUIRED,
69    http::StatusCode::INTERNAL_SERVER_ERROR,
70    http::StatusCode::NOT_IMPLEMENTED,
71    http::StatusCode::GATEWAY_TIMEOUT,
72    http::StatusCode::LOOP_DETECTED,
73    http::StatusCode::NETWORK_AUTHENTICATION_REQUIRED,
74];
75
76impl Arbitrary for ArbitraryStatusCode {
77    type Strategy = StatusCodeStrategy;
78    type Parameters = ();
79
80    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
81        StatusCodeStrategy
82    }
83}
84impl Strategy for StatusCodeStrategy {
85    type Tree = StatusCodeValueTree;
86    type Value = ArbitraryStatusCode;
87
88    fn new_tree(&self, runner: &mut TestRunner) -> NewTree<Self> {
89        Ok(StatusCodeValueTree(Index::arbitrary().new_tree(runner)?))
90    }
91}
92impl ValueTree for StatusCodeValueTree {
93    type Value = ArbitraryStatusCode;
94
95    fn current(&self) -> Self::Value {
96        ArbitraryStatusCode(*self.0.current().get(&STATUS_CODES))
97    }
98
99    fn simplify(&mut self) -> bool {
100        self.0.simplify()
101    }
102
103    fn complicate(&mut self) -> bool {
104        self.0.complicate()
105    }
106}