From 3beae1f36f519c5fdbcf8878b6a8ac71ac84938d Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 11 Jul 2022 20:54:56 +0200 Subject: [PATCH] impl Service for MethodRouter<()> --- axum/src/routing/method_routing.rs | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index 5a7236accc4..16cbe18f93b 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -1048,6 +1048,24 @@ fn append_allow_header(allow_header: &mut AllowHeader, method: &'static str) { } } +impl Service> for MethodRouter<(), B, E> +where + B: HttpBody, +{ + type Response = Response; + type Error = E; + type Future = RouteFuture; + + #[inline] + fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request) -> Self::Future { + self.clone().with_state(()).call(req) + } +} + impl Clone for MethodRouter { fn clone(&self) -> Self { Self { @@ -1207,6 +1225,19 @@ mod tests { assert!(body.is_empty()); } + #[tokio::test] + async fn get_service_fn() { + async fn handle(_req: Request) -> Result, Infallible> { + Ok(Response::new(Body::from("ok"))) + } + + let mut svc = get_service(service_fn(handle)); + + let (status, _, body) = call(Method::GET, &mut svc).await; + assert_eq!(status, StatusCode::OK); + assert_eq!(body, "ok"); + } + #[tokio::test] async fn get_handler() { let mut svc = MethodRouter::new().get(ok).with_state(());