interface_tests_helpers/
lib.rs

1//! Tests helpers to test HTTP interfaces.
2
3extern crate reqwest;
4
5use reqwest::{
6    Client,
7    Response,
8    StatusCode,
9};
10
11use std::collections::HashMap;
12
13pub trait HasBaseUrl {
14
15    fn get_base_url(&self) -> &str;
16}
17
18pub trait ClientHandler {
19
20    fn get_url(&self, url: &str) -> Response;
21
22    fn post_json(&self, url: &str, json: &HashMap<&str, &str>) -> Response;
23
24    fn post_body(&self, url: &str, body: &str) -> Response;
25
26    fn put_xml(&self, url: &str, body: &str) -> Response;
27
28    fn put_text(&self, url: &str, text: &str) -> Response;
29}
30
31pub trait ResponseHandler {
32
33    fn assert_200(&self);
34
35    fn assert_201(&self);
36
37    fn assert_204(&self);
38
39    fn assert_400(&self);
40
41    fn assert_401(&self);
42
43    fn assert_403(&self);
44
45    fn assert_404(&self);
46
47    fn assert_409(&self);
48
49    fn assert_500(&self);
50}
51
52impl ClientHandler for Client {
53
54    /// Perform a GET request without parameter
55    ///
56    /// # Args:
57    ///
58    /// `url` - the suffix of the URL
59    fn get_url(
60        &self,
61        url: &str,
62    ) -> Response {
63
64        self.get(url)
65            .send()
66            .unwrap()
67    }
68
69    /// Perform a POST request to send JSON and stores its result
70    ///
71    /// # Args:
72    ///
73    /// `url` - the suffix of the URL
74    /// `json` - the json data to send
75    fn post_json(
76        &self,
77        url: &str,
78        json: &HashMap<&str, &str>,
79    ) -> Response {
80
81        self.post(url)
82            .json(json)
83            .send()
84            .unwrap()
85    }
86
87    /// Perform a POST request to send a raw body and stores its result
88    ///
89    /// # Args:
90    ///
91    /// `url` - the suffix of the URL
92    /// `body` - the body data to send (String format)
93    fn post_body(
94        &self,
95        url: &str,
96        body: &str,
97    ) -> Response {
98
99        self.post(url)
100            .body(body.to_string())
101            .header(
102                reqwest::header::CONTENT_TYPE,
103                "text/plain"
104            )
105            .send()
106            .unwrap()
107    }
108
109    /// Perform a PUT request to send a XML body and returns its result
110    ///
111    /// # Args:
112    ///
113    /// `url` - the suffix of the URL
114    /// `body` - the body data to send (XML format)
115    fn put_xml(
116        &self,
117        url: &str,
118        body: &str,
119    ) -> Response {
120
121        self.put(url)
122            .body(body.to_string())
123            .header(
124                reqwest::header::CONTENT_TYPE,
125                "application/xml"
126            )
127            .send()
128            .unwrap()
129    }
130
131    /// Perform a PUT request to send a text body and returns its result
132    ///
133    /// # Args:
134    ///
135    /// `url` - the suffix of the URL
136    /// `text` - the text to upload
137    fn put_text(
138        &self,
139        url: &str,
140        text: &str,
141    ) -> Response {
142
143        self.put(url)
144            .body(text.to_string())
145            .header(
146                reqwest::header::CONTENT_TYPE,
147                "text/plain"
148            )
149            .send()
150            .unwrap()
151    }
152}
153
154impl ResponseHandler for Response {
155
156    /// Assertion that checks the response status code is 200
157    fn assert_200(&self) {
158
159        assert_eq!(
160            self.status(),
161            StatusCode::OK,
162        );
163    }
164
165    /// Assertion that checks the response status code is 201
166    fn assert_201(&self) {
167
168        assert_eq!(
169            self.status(),
170            StatusCode::CREATED,
171        );
172    }
173
174    /// Assertion that checks the response status code is 204
175    fn assert_204(&self) {
176
177        assert_eq!(
178            self.status(),
179            StatusCode::NO_CONTENT,
180        );
181    }
182
183    /// Assertion that checks the response status code is 400
184    fn assert_400(&self) {
185
186        assert_eq!(
187            self.status(),
188            StatusCode::BAD_REQUEST,
189        );
190    }
191
192    /// Assertion that checks the response status code is 401
193    fn assert_401(&self) {
194
195        assert_eq!(
196            self.status(),
197            StatusCode::UNAUTHORIZED,
198        );
199    }
200
201    /// Assertion that checks the response status code is 403
202    fn assert_403(&self) {
203
204        assert_eq!(
205            self.status(),
206            StatusCode::FORBIDDEN,
207        );
208    }
209
210    /// Assertion that checks the response status code is 404
211    fn assert_404(&self) {
212
213        assert_eq!(
214            self.status(),
215            StatusCode::NOT_FOUND,
216        );
217    }
218
219    /// Assertion that checks the response status code is 409
220    fn assert_409(&self) {
221
222        assert_eq!(
223            self.status(),
224            StatusCode::CONFLICT,
225        );
226    }
227
228    /// Assertion that checks the response status code is 500
229    fn assert_500(&self) {
230
231        assert_eq!(
232            self.status(),
233            StatusCode::INTERNAL_SERVER_ERROR,
234        );
235    }
236}
237
238#[cfg(test)]
239mod tests;