Skip to content

Commit 6fa7cce

Browse files
authored
[test] the validator example (#3276)
1 parent c997137 commit 6fa7cce

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/validator/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ tokio = { version = "1.0", features = ["full"] }
1212
tracing = "0.1"
1313
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1414
validator = { version = "0.18.1", features = ["derive"] }
15+
16+
[dev-dependencies]
17+
http-body-util = "0.1.0"
18+
tower = { version = "0.5.2", features = ["util"] }

examples/validator/src/main.rs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ async fn main() {
3434
.init();
3535

3636
// build our application with a route
37-
let app = Router::new().route("/", get(handler));
37+
let app = app();
3838

3939
// run it
4040
let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
4141
tracing::debug!("listening on {}", listener.local_addr().unwrap());
4242
axum::serve(listener, app).await.unwrap();
4343
}
4444

45+
fn app() -> Router {
46+
Router::new().route("/", get(handler))
47+
}
48+
4549
#[derive(Debug, Deserialize, Validate)]
4650
pub struct NameInput {
47-
#[validate(length(min = 1, message = "Can not be empty"))]
51+
#[validate(length(min = 2, message = "Can not be empty"))]
4852
pub name: String,
4953
}
5054

@@ -91,3 +95,83 @@ impl IntoResponse for ServerError {
9195
.into_response()
9296
}
9397
}
98+
99+
#[cfg(test)]
100+
mod tests {
101+
use super::*;
102+
use axum::{
103+
body::Body,
104+
http::{Request, StatusCode},
105+
};
106+
use http_body_util::BodyExt;
107+
use tower::ServiceExt;
108+
109+
async fn get_html(response: Response<Body>) -> String {
110+
let body = response.into_body();
111+
let bytes = body.collect().await.unwrap().to_bytes();
112+
String::from_utf8(bytes.to_vec()).unwrap()
113+
}
114+
115+
#[tokio::test]
116+
async fn test_no_param() {
117+
let response = app()
118+
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
119+
.await
120+
.unwrap();
121+
122+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
123+
let html = get_html(response).await;
124+
assert_eq!(html, "Failed to deserialize form: missing field `name`");
125+
}
126+
127+
#[tokio::test]
128+
async fn test_with_param_without_value() {
129+
let response = app()
130+
.oneshot(
131+
Request::builder()
132+
.uri("/?name=")
133+
.body(Body::empty())
134+
.unwrap(),
135+
)
136+
.await
137+
.unwrap();
138+
139+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
140+
let html = get_html(response).await;
141+
assert_eq!(html, "Input validation error: [name: Can not be empty]");
142+
}
143+
144+
#[tokio::test]
145+
async fn test_with_param_with_short_value() {
146+
let response = app()
147+
.oneshot(
148+
Request::builder()
149+
.uri("/?name=X")
150+
.body(Body::empty())
151+
.unwrap(),
152+
)
153+
.await
154+
.unwrap();
155+
156+
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
157+
let html = get_html(response).await;
158+
assert_eq!(html, "Input validation error: [name: Can not be empty]");
159+
}
160+
161+
#[tokio::test]
162+
async fn test_with_param_and_value() {
163+
let response = app()
164+
.oneshot(
165+
Request::builder()
166+
.uri("/?name=LT")
167+
.body(Body::empty())
168+
.unwrap(),
169+
)
170+
.await
171+
.unwrap();
172+
173+
assert_eq!(response.status(), StatusCode::OK);
174+
let html = get_html(response).await;
175+
assert_eq!(html, "<h1>Hello, LT!</h1>");
176+
}
177+
}

0 commit comments

Comments
 (0)