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