Skip to content

Commit 15917c6

Browse files
authored
[test] the versioning example (#3275)
1 parent ac7732a commit 15917c6

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
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/versioning/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ axum = { path = "../../axum" }
99
tokio = { version = "1.0", features = ["full"] }
1010
tracing = "0.1"
1111
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
12+
13+
[dev-dependencies]
14+
http-body-util = "0.1.0"
15+
tower = { version = "0.5.2", features = ["util"] }

examples/versioning/src/main.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use axum::{
88
extract::{FromRequestParts, Path},
99
http::{request::Parts, StatusCode},
10-
response::{IntoResponse, Response},
10+
response::{Html, IntoResponse, Response},
1111
routing::get,
1212
RequestPartsExt, Router,
1313
};
@@ -25,7 +25,7 @@ async fn main() {
2525
.init();
2626

2727
// build our application with some routes
28-
let app = Router::new().route("/{version}/foo", get(handler));
28+
let app = app();
2929

3030
// run it
3131
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
@@ -35,8 +35,12 @@ async fn main() {
3535
axum::serve(listener, app).await.unwrap();
3636
}
3737

38-
async fn handler(version: Version) {
39-
println!("received request with version {version:?}");
38+
fn app() -> Router {
39+
Router::new().route("/{version}/foo", get(handler))
40+
}
41+
42+
async fn handler(version: Version) -> Html<String> {
43+
Html(format!("received request with version {version:?}"))
4044
}
4145

4246
#[derive(Debug)]
@@ -68,3 +72,51 @@ where
6872
}
6973
}
7074
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::*;
79+
use axum::{body::Body, http::Request, http::StatusCode};
80+
use http_body_util::BodyExt;
81+
use tower::ServiceExt;
82+
83+
#[tokio::test]
84+
async fn test_v1() {
85+
let response = app()
86+
.oneshot(
87+
Request::builder()
88+
.uri("/v1/foo")
89+
.body(Body::empty())
90+
.unwrap(),
91+
)
92+
.await
93+
.unwrap();
94+
95+
assert_eq!(response.status(), StatusCode::OK);
96+
let body = response.into_body();
97+
let bytes = body.collect().await.unwrap().to_bytes();
98+
let html = String::from_utf8(bytes.to_vec()).unwrap();
99+
100+
assert_eq!(html, "received request with version V1");
101+
}
102+
103+
#[tokio::test]
104+
async fn test_v4() {
105+
let response = app()
106+
.oneshot(
107+
Request::builder()
108+
.uri("/v4/foo")
109+
.body(Body::empty())
110+
.unwrap(),
111+
)
112+
.await
113+
.unwrap();
114+
115+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
116+
let body = response.into_body();
117+
let bytes = body.collect().await.unwrap().to_bytes();
118+
let html = String::from_utf8(bytes.to_vec()).unwrap();
119+
120+
assert_eq!(html, "unknown version");
121+
}
122+
}

0 commit comments

Comments
 (0)