logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Test utilities to test your endpoints.
//!
//! # Basic usage
//!
//! ```
//! use poem::{handler, test::TestClient, Route};
//!
//! #[handler]
//! fn index() -> &'static str {
//!     "hello"
//! }
//!
//! let app = Route::new().at("/", index);
//! let cli = TestClient::new(app);
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! // send request
//! let resp = cli.get("/").send().await;
//! // check the status code
//! resp.assert_status_is_ok();
//! // check the body string
//! resp.assert_text("hello").await;
//! # });
//! ```
//!
//! # Check the JSON response
//!
//! ```
//! use poem::{handler, test::TestClient, web::Json, Route};
//! use serde::Serialize;
//!
//! #[derive(Serialize)]
//! struct MyResponse {
//!     a: i32,
//!     b: String,
//! }
//!
//! #[handler]
//! fn index() -> Json<MyResponse> {
//!     Json(MyResponse {
//!         a: 100,
//!         b: "hello".to_string(),
//!     })
//! }
//!
//! let app = Route::new().at("/", index);
//! let cli = TestClient::new(app);
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! // send request
//! let resp = cli.get("/").send().await;
//! // check the status code
//! resp.assert_status_is_ok();
//! // check the json
//! let json = resp.json().await;
//! let json_value = json.value();
//! json_value.object().get("a").assert_i64(100);
//! json_value.object().get("b").assert_string("hello");
//! # });
//! ```
//!
//! # Post multipart data
//!
//! ```
//! use poem::{
//!     error::{BadRequest, Error},
//!     handler,
//!     http::StatusCode,
//!     test::{TestClient, TestForm},
//!     web::{Form, Multipart},
//!     Result, Route,
//! };
//!
//! #[handler]
//! async fn index(mut multipart: Multipart) -> Result<String> {
//!     let mut name = None;
//!     let mut value = None;
//!
//!     while let Some(field) = multipart.next_field().await? {
//!         match field.name() {
//!             Some("name") => name = Some(field.text().await?),
//!             Some("value") => {
//!                 value = Some(field.text().await?.parse::<i32>().map_err(BadRequest)?)
//!             }
//!             _ => {}
//!         }
//!     }
//!
//!     match (name, value) {
//!         (Some(name), Some(value)) => Ok(format!("{}={}", name, value)),
//!         _ => Err(Error::from_status(StatusCode::BAD_REQUEST)),
//!     }
//! }
//!
//! let app = Route::new().at("/", index);
//! let cli = TestClient::new(app);
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! // send request
//! let resp = cli
//!     .post("/")
//!     .multipart(TestForm::new().text("name", "a").text("value", "10"))
//!     .send()
//!     .await;
//! // check the status code
//! resp.assert_status_is_ok();
//! // check the body string
//! resp.assert_text("a=10").await;
//! # });
//! ```

mod client;
mod form;
mod json;
mod request_builder;
mod response;

pub use client::TestClient;
pub use form::{TestForm, TestFormField};
pub use json::{TestJson, TestJsonArray, TestJsonObject, TestJsonValue};
pub use request_builder::TestRequestBuilder;
pub use response::TestResponse;