mirror of
https://github.com/tokio-rs/axum.git
synced 2025-10-02 15:24:54 +00:00
Add test for routing matching multiple methods
I don't believe we had a test for this
This commit is contained in:
parent
387de8b426
commit
8ef96f2199
@ -1,26 +1,24 @@
|
|||||||
//! Run with
|
|
||||||
//!
|
|
||||||
//! ```not_rust
|
|
||||||
//! cargo run --example hello_world
|
|
||||||
//! ```
|
|
||||||
|
|
||||||
use axum::prelude::*;
|
use axum::prelude::*;
|
||||||
use std::net::SocketAddr;
|
use std::{convert::Infallible, net::SocketAddr, time::Duration};
|
||||||
|
use tower::{limit::RateLimitLayer, BoxError, ServiceBuilder};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Set the RUST_LOG, if it hasn't been explicitly defined
|
let handler_layer = ServiceBuilder::new()
|
||||||
if std::env::var("RUST_LOG").is_err() {
|
.buffer(1024)
|
||||||
std::env::set_var("RUST_LOG", "hello_world=debug")
|
.layer(RateLimitLayer::new(10, Duration::from_secs(10)))
|
||||||
}
|
.into_inner();
|
||||||
tracing_subscriber::fmt::init();
|
|
||||||
|
|
||||||
// build our application with a route
|
let app = route(
|
||||||
let app = route("/", get(handler));
|
"/",
|
||||||
|
get(handler
|
||||||
|
.layer(handler_layer)
|
||||||
|
.handle_error(|error: BoxError| {
|
||||||
|
Ok::<_, Infallible>(format!("Unhandled error: {}", error))
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
|
||||||
// run it
|
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
tracing::debug!("listening on {}", addr);
|
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
.await
|
.await
|
||||||
|
@ -604,6 +604,29 @@ async fn wrong_method_service() {
|
|||||||
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn multiple_methods_for_one_handler() {
|
||||||
|
async fn root(_: Request<Body>) -> &'static str {
|
||||||
|
"Hello, World!"
|
||||||
|
}
|
||||||
|
|
||||||
|
let app = route("/", on(MethodFilter::GET | MethodFilter::POST, root));
|
||||||
|
|
||||||
|
let addr = run_in_background(app).await;
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post(format!("http://{}", addr))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
/// Run a `tower::Service` in the background and get a URI for it.
|
/// 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
|
pub(crate) async fn run_in_background<S, ResBody>(svc: S) -> SocketAddr
|
||||||
where
|
where
|
||||||
|
Loading…
x
Reference in New Issue
Block a user