[test] the versioning example (#3275)

This commit is contained in:
Gábor Szabó 2025-03-18 11:54:02 +02:00 committed by GitHub
parent ac7732abd3
commit 15917c6dbc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 4 deletions

2
Cargo.lock generated
View File

@ -1968,7 +1968,9 @@ name = "example-versioning"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"http-body-util",
"tokio", "tokio",
"tower 0.5.2",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]

View File

@ -9,3 +9,7 @@ axum = { path = "../../axum" }
tokio = { version = "1.0", features = ["full"] } tokio = { version = "1.0", features = ["full"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[dev-dependencies]
http-body-util = "0.1.0"
tower = { version = "0.5.2", features = ["util"] }

View File

@ -7,7 +7,7 @@
use axum::{ use axum::{
extract::{FromRequestParts, Path}, extract::{FromRequestParts, Path},
http::{request::Parts, StatusCode}, http::{request::Parts, StatusCode},
response::{IntoResponse, Response}, response::{Html, IntoResponse, Response},
routing::get, routing::get,
RequestPartsExt, Router, RequestPartsExt, Router,
}; };
@ -25,7 +25,7 @@ async fn main() {
.init(); .init();
// build our application with some routes // build our application with some routes
let app = Router::new().route("/{version}/foo", get(handler)); let app = app();
// run it // run it
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
@ -35,8 +35,12 @@ async fn main() {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
async fn handler(version: Version) { fn app() -> Router {
println!("received request with version {version:?}"); Router::new().route("/{version}/foo", get(handler))
}
async fn handler(version: Version) -> Html<String> {
Html(format!("received request with version {version:?}"))
} }
#[derive(Debug)] #[derive(Debug)]
@ -68,3 +72,51 @@ where
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use axum::{body::Body, http::Request, http::StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
#[tokio::test]
async fn test_v1() {
let response = app()
.oneshot(
Request::builder()
.uri("/v1/foo")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body();
let bytes = body.collect().await.unwrap().to_bytes();
let html = String::from_utf8(bytes.to_vec()).unwrap();
assert_eq!(html, "received request with version V1");
}
#[tokio::test]
async fn test_v4() {
let response = app()
.oneshot(
Request::builder()
.uri("/v4/foo")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = response.into_body();
let bytes = body.collect().await.unwrap().to_bytes();
let html = String::from_utf8(bytes.to_vec()).unwrap();
assert_eq!(html, "unknown version");
}
}