mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-26 20:40:29 +00:00
[test] the versioning example (#3275)
This commit is contained in:
parent
ac7732abd3
commit
15917c6dbc
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1968,7 +1968,9 @@ name = "example-versioning"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"http-body-util",
|
||||
"tokio",
|
||||
"tower 0.5.2",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
@ -9,3 +9,7 @@ axum = { path = "../../axum" }
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
[dev-dependencies]
|
||||
http-body-util = "0.1.0"
|
||||
tower = { version = "0.5.2", features = ["util"] }
|
||||
|
@ -7,7 +7,7 @@
|
||||
use axum::{
|
||||
extract::{FromRequestParts, Path},
|
||||
http::{request::Parts, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::get,
|
||||
RequestPartsExt, Router,
|
||||
};
|
||||
@ -25,7 +25,7 @@ async fn main() {
|
||||
.init();
|
||||
|
||||
// build our application with some routes
|
||||
let app = Router::new().route("/{version}/foo", get(handler));
|
||||
let app = app();
|
||||
|
||||
// run it
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||
@ -35,8 +35,12 @@ async fn main() {
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn handler(version: Version) {
|
||||
println!("received request with version {version:?}");
|
||||
fn app() -> Router {
|
||||
Router::new().route("/{version}/foo", get(handler))
|
||||
}
|
||||
|
||||
async fn handler(version: Version) -> Html<String> {
|
||||
Html(format!("received request with version {version:?}"))
|
||||
}
|
||||
|
||||
#[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");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user