Bring back Handler::into_service (#212)

It was removed as part of https://github.com/tokio-rs/axum/pull/184 but
I do actually think it has some utility. So makes sense to keep even if
axum doesn't use it directly for routing.
This commit is contained in:
David Pedersen
2021-08-19 21:16:44 +02:00
committed by GitHub
parent 421faceae3
commit 0fd17d4181
4 changed files with 139 additions and 1 deletions
+21
View File
@@ -3,6 +3,7 @@
use crate::{
extract,
handler::{any, delete, get, on, patch, post, Handler},
response::IntoResponse,
route,
routing::nest,
routing::{MethodFilter, RoutingDsl},
@@ -604,6 +605,26 @@ async fn multiple_methods_for_one_handler() {
assert_eq!(res.status(), StatusCode::OK);
}
#[tokio::test]
async fn handler_into_service() {
async fn handle(body: String) -> impl IntoResponse {
format!("you said: {}", body)
}
let addr = run_in_background(handle.into_service()).await;
let client = reqwest::Client::new();
let res = client
.post(format!("http://{}", addr))
.body("hi there!")
.send()
.await
.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await.unwrap(), "you said: hi there!");
}
/// Run a `tower::Service` in the background and get a URI for it.
pub(crate) async fn run_in_background<S, ResBody>(svc: S) -> SocketAddr
where