expect_json/expect/mod.rs
1pub mod ops;
2
3use crate::expect::ops::ExpectArray;
4use crate::expect::ops::ExpectEmail;
5use crate::expect::ops::ExpectFloat;
6use crate::expect::ops::ExpectInteger;
7use crate::expect::ops::ExpectIsoDateTime;
8use crate::expect::ops::ExpectObject;
9use crate::expect::ops::ExpectString;
10use crate::expect::ops::ExpectUuid;
11
12///
13/// Expect a JSON object. See [`ExpectObject`] for further methods to
14/// define what is expected. Such as the range it is expected to be within,
15/// or if it should be positive or negative.
16///
17/// ```rust
18/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
19/// #
20/// # use axum::Router;
21/// # use axum::extract::Json;
22/// # use axum::routing::get;
23/// # use axum_test::TestServer;
24/// # use serde_json::json;
25/// #
26/// # let server = TestServer::new(Router::new())?;
27/// #
28/// use axum_test::expect_json;
29///
30/// let server = TestServer::new(Router::new())?;
31///
32/// server.get(&"/user/barrington")
33/// .await
34/// .assert_json(&json!({
35/// "name": "Barrington",
36/// "metadata": expect_json::object(),
37/// }));
38/// #
39/// # Ok(()) }
40/// ```
41pub fn object() -> ExpectObject {
42 ExpectObject::new()
43}
44
45///
46/// Expect a floating point number. See [`ExpectFloat`] for further methods to
47/// define what is expected. Such as the range it is expected to be within,
48/// or if it should be positive or negative.
49///
50/// ```rust
51/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
52/// #
53/// # use axum::Router;
54/// # use axum::extract::Json;
55/// # use axum::routing::get;
56/// # use axum_test::TestServer;
57/// # use serde_json::json;
58/// #
59/// # let server = TestServer::new(Router::new())?;
60/// #
61/// use axum_test::expect_json;
62///
63/// let server = TestServer::new(Router::new())?;
64///
65/// server.get(&"/user/barrington")
66/// .await
67/// .assert_json(&json!({
68/// "name": "Barrington",
69/// "height_in_meters": expect_json::float(),
70/// }));
71/// #
72/// # Ok(()) }
73/// ```
74pub fn float() -> ExpectFloat {
75 ExpectFloat::new()
76}
77
78///
79/// Expects an integer. See [`ExpectInteger`] for further methods to
80/// define what is expected. Such as the range it is expected to be within,
81/// or if it should be positive or negative.
82///
83/// ```rust
84/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
85/// #
86/// # use axum::Router;
87/// # use axum::extract::Json;
88/// # use axum::routing::get;
89/// # use axum_test::TestServer;
90/// # use serde_json::json;
91/// #
92/// # let server = TestServer::new(Router::new())?;
93/// #
94/// use axum_test::expect_json;
95///
96/// let server = TestServer::new(Router::new())?;
97///
98/// server.get(&"/user/barrington")
99/// .await
100/// .assert_json(&json!({
101/// "name": "Barrington",
102/// "age": expect_json::integer(),
103/// }));
104/// #
105/// # Ok(()) }
106/// ```
107pub fn integer() -> ExpectInteger {
108 ExpectInteger::new()
109}
110
111///
112/// Expect a string. See [`ExpectString`] for further methods to defined
113/// the length, and partial contents, that is expected.
114///
115/// ```rust
116/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
117/// #
118/// # use axum::Router;
119/// # use axum::extract::Json;
120/// # use axum::routing::get;
121/// # use axum_test::TestServer;
122/// # use serde_json::json;
123/// #
124/// # let server = TestServer::new(Router::new())?;
125/// #
126/// use axum_test::expect_json;
127///
128/// let server = TestServer::new(Router::new())?;
129///
130/// server.get(&"/user/barrington")
131/// .await
132/// .assert_json(&json!({
133/// "name": expect_json::string(),
134/// }));
135/// #
136/// # Ok(()) }
137/// ```
138pub fn string() -> ExpectString {
139 ExpectString::new()
140}
141
142///
143/// Expects a JSON array. The returned [`ExpectArray`] has methods to
144/// defined the length, uniqueness, all values meet a condition, etc,
145/// that is expected to be returned.
146///
147/// ```rust
148/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
149/// #
150/// # use axum::Router;
151/// # use axum::extract::Json;
152/// # use axum::routing::get;
153/// # use axum_test::TestServer;
154/// # use serde_json::json;
155/// #
156/// # let server = TestServer::new(Router::new())?;
157/// #
158/// use axum_test::expect_json;
159///
160/// let server = TestServer::new(Router::new())?;
161///
162/// server.get(&"/user/barrington")
163/// .await
164/// .assert_json(&json!({
165/// "name": "Barrington",
166/// "tags": expect_json::array(),
167/// }));
168/// #
169/// # Ok(()) }
170/// ```
171pub fn array() -> ExpectArray {
172 ExpectArray::new()
173}
174
175///
176/// Expect a valid-looking ISO date time.
177///
178/// Further methods are available on the returned [`ExpectIsoDateTime`]
179/// to check if the time is within certain durations, the time zone, etc.
180///
181/// ```rust
182/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
183/// #
184/// # use axum::Router;
185/// # use axum::extract::Json;
186/// # use axum::routing::get;
187/// # use axum_test::TestServer;
188/// # use serde_json::json;
189/// #
190/// # let server = TestServer::new(Router::new())?;
191/// #
192/// use axum_test::expect_json;
193/// use std::time::Duration;
194///
195/// let server = TestServer::new(Router::new())?;
196///
197/// server.get(&"/user/barrington")
198/// .await
199/// .assert_json(&json!({
200/// "name": "Barrington",
201/// "created_at": expect_json::iso_date_time(),
202/// }));
203/// #
204/// # Ok(()) }
205/// ```
206pub fn iso_date_time() -> ExpectIsoDateTime {
207 ExpectIsoDateTime::new()
208}
209
210///
211/// Expect a valid UUID.
212///
213/// ```rust
214/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
215/// #
216/// # use axum::Router;
217/// # use axum::extract::Json;
218/// # use axum::routing::get;
219/// # use axum_test::TestServer;
220/// # use serde_json::json;
221/// #
222/// # let server = TestServer::new(Router::new())?;
223/// #
224/// use std::time::Duration;
225/// use axum_test::expect_json;
226///
227/// let server = TestServer::new(Router::new())?;
228///
229/// server.get(&"/user/alice")
230/// .await
231/// .assert_json(&json!({
232/// "name": "Alice",
233/// "id": expect_json::uuid(),
234/// }));
235/// #
236/// # Ok(()) }
237/// ```
238///
239pub fn uuid() -> ExpectUuid {
240 ExpectUuid::new()
241}
242
243///
244/// Expect a valid-looking email address.
245///
246/// It makes no guarantees if the address is actually registered or in use.
247///
248/// ```rust
249/// # async fn test() -> Result<(), Box<dyn ::std::error::Error>> {
250/// #
251/// # use axum::Router;
252/// # use axum::extract::Json;
253/// # use axum::routing::get;
254/// # use axum_test::TestServer;
255/// # use serde_json::json;
256/// #
257/// # let server = TestServer::new(Router::new())?;
258/// #
259/// use axum_test::expect_json;
260///
261/// let server = TestServer::new(Router::new())?;
262///
263/// server.get(&"/user")
264/// .await
265/// .assert_json(&json!({
266/// "name": "John Doe",
267/// "email": expect_json::email(),
268/// }));
269/// #
270/// # Ok(()) }
271/// ```
272pub fn email() -> ExpectEmail {
273 ExpectEmail::new()
274}