Skip to content

Commit

Permalink
impl Service for MethodRouter<()>
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Jul 11, 2022
1 parent 18bb70b commit 3beae1f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,24 @@ fn append_allow_header(allow_header: &mut AllowHeader, method: &'static str) {
}
}

impl<B, E> Service<Request<B>> for MethodRouter<(), B, E>
where
B: HttpBody,
{
type Response = Response;
type Error = E;
type Future = RouteFuture<B, E>;

#[inline]
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<B>) -> Self::Future {
self.clone().with_state(()).call(req)
}
}

impl<S, B, E> Clone for MethodRouter<S, B, E> {
fn clone(&self) -> Self {
Self {
Expand Down Expand Up @@ -1207,6 +1225,19 @@ mod tests {
assert!(body.is_empty());
}

#[tokio::test]
async fn get_service_fn() {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, 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(());
Expand Down

0 comments on commit 3beae1f

Please sign in to comment.