Skip to content

Commit 3beae1f

Browse files
committed
impl Service for MethodRouter<()>
1 parent 18bb70b commit 3beae1f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

axum/src/routing/method_routing.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,24 @@ fn append_allow_header(allow_header: &mut AllowHeader, method: &'static str) {
10481048
}
10491049
}
10501050

1051+
impl<B, E> Service<Request<B>> for MethodRouter<(), B, E>
1052+
where
1053+
B: HttpBody,
1054+
{
1055+
type Response = Response;
1056+
type Error = E;
1057+
type Future = RouteFuture<B, E>;
1058+
1059+
#[inline]
1060+
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
1061+
Poll::Ready(Ok(()))
1062+
}
1063+
1064+
fn call(&mut self, req: Request<B>) -> Self::Future {
1065+
self.clone().with_state(()).call(req)
1066+
}
1067+
}
1068+
10511069
impl<S, B, E> Clone for MethodRouter<S, B, E> {
10521070
fn clone(&self) -> Self {
10531071
Self {
@@ -1207,6 +1225,19 @@ mod tests {
12071225
assert!(body.is_empty());
12081226
}
12091227

1228+
#[tokio::test]
1229+
async fn get_service_fn() {
1230+
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
1231+
Ok(Response::new(Body::from("ok")))
1232+
}
1233+
1234+
let mut svc = get_service(service_fn(handle));
1235+
1236+
let (status, _, body) = call(Method::GET, &mut svc).await;
1237+
assert_eq!(status, StatusCode::OK);
1238+
assert_eq!(body, "ok");
1239+
}
1240+
12101241
#[tokio::test]
12111242
async fn get_handler() {
12121243
let mut svc = MethodRouter::new().get(ok).with_state(());

0 commit comments

Comments
 (0)