mockito_declarative_server/
lib.rs

1pub use mockito;
2pub use serde::Deserialize;
3pub use serde_yaml;
4
5#[derive(Deserialize)]
6pub struct MockDefinition {
7    pub request: MockRequest,
8    pub response: MockResponse,
9}
10
11#[derive(Deserialize)]
12pub struct MockRequest {
13    pub method: String,
14    pub path: Option<String>,
15    pub query: Option<Vec<MockQuery>>,
16    pub headers: Option<Vec<MockHeader>>,
17}
18
19#[derive(Deserialize)]
20pub struct MockQuery {
21    pub parameter: String,
22    pub value: String,
23    pub regex: Option<bool>,
24}
25
26#[derive(Deserialize)]
27pub struct MockHeader {
28    pub header: String,
29    pub value: String,
30}
31
32#[derive(Deserialize)]
33pub struct MockResponse {
34    pub headers: Option<Vec<MockHeader>>,
35    pub body: Option<String>,
36}
37
38/// Generate mockito mocks using declarative (yml) definition.
39#[macro_export]
40macro_rules! mock_server {
41    ($file:expr) => {{
42        use std::path::Path;
43        use $crate::mockito::{mock, Matcher};
44        use $crate::serde_yaml;
45        use $crate::*;
46
47        let mocks_path = Path::new(env!("CARGO_MANIFEST_DIR")).join($file);
48
49        let file = std::fs::read(&mocks_path).expect("definition file's not found!");
50
51        let definitions: Vec<MockDefinition> =
52            serde_yaml::from_slice(&file).expect("failed to parse definition file");
53
54        let mocks = definitions
55            .into_iter()
56            .map(|MockDefinition { request, response }| {
57                let path = request.path.map(Matcher::Regex).unwrap_or(Matcher::Any);
58
59                let mut mock = mock(&request.method, path);
60
61                if let Some(headers) = request.headers {
62                    for MockHeader { header, value } in headers {
63                        mock = mock.match_header(&header, &value[..]);
64                    }
65                }
66
67                if let Some(query_params) = request.query {
68                    for MockQuery {
69                        parameter,
70                        value,
71                        regex,
72                    } in query_params
73                    {
74                        if let Some(true) = regex {
75                            mock = mock
76                                .match_query(Matcher::Regex(format!("{}={}", parameter, value)));
77                        } else {
78                            mock = mock
79                                .match_query(Matcher::UrlEncoded(parameter.into(), value.into()));
80                        }
81                    }
82                }
83
84                if let Some(body) = response.body {
85                    mock = mock.with_body_from_file(
86                        mocks_path
87                            .parent()
88                            .expect("couldn't extract file parent")
89                            .join(body),
90                    );
91                }
92
93                if let Some(headers) = response.headers {
94                    for MockHeader { header, value } in headers {
95                        let value = value.replace("SERVER_URL", &mockito::server_url());
96
97                        mock = mock.with_header(&header, &value[..]);
98                    }
99                }
100
101                mock.create()
102            })
103            .collect::<Vec<_>>();
104
105        (mockito::server_url(), mocks)
106    }};
107}
108
109#[cfg(test)]
110mod test {
111    #[tokio::test]
112    async fn test_regex_matching() {
113        let (server, _mocks) = super::mock_server!("test/basic.yml");
114
115        let client = reqwest::Client::new();
116
117        let resp = client
118            .get(format!("{}/{}", server, "v2/hash/manifests/hash/"))
119            .header(
120                "Accept",
121                "application/vnd.docker.distribution.manifest.v2+json",
122            )
123            .send()
124            .await
125            .expect("failed to make a request")
126            .json::<serde_json::Value>()
127            .await
128            .expect("failed to parse the response");
129
130        assert_eq!(resp["config"]["size"], 6668);
131    }
132
133    #[tokio::test]
134    async fn test_query_regex_matching() {
135        let (server, _mocks) = super::mock_server!("test/basic.yml");
136
137        let client = reqwest::Client::new();
138
139        let resp = client
140            .get(format!("{}/{}", server, "somesite"))
141            .query(&[("arguments", "lorem, ipsum, dolor")])
142            .header(
143                "Accept",
144                "application/vnd.docker.distribution.manifest.v2+json",
145            )
146            .send()
147            .await
148            .expect("failed to make a request")
149            .text()
150            .await
151            .expect("failed to parse the response");
152
153        assert_eq!(resp, "Ich bin nicht gut fuer dich");
154    }
155}