mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-28 00:00:20 +02:00
Clearly document applying DefaultBodyLimit to individual routes (#2157)
This commit is contained in:
@@ -64,6 +64,28 @@ use tower_layer::Layer;
|
|||||||
/// extractors and want to sure a limit is also applied there then [`RequestBodyLimit`] should be
|
/// extractors and want to sure a limit is also applied there then [`RequestBodyLimit`] should be
|
||||||
/// used.
|
/// used.
|
||||||
///
|
///
|
||||||
|
/// # Different limits for different routes
|
||||||
|
///
|
||||||
|
/// `DefaultBodyLimit` can also be selectively applied to have different limits for different
|
||||||
|
/// routes:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use axum::{
|
||||||
|
/// Router,
|
||||||
|
/// routing::post,
|
||||||
|
/// body::Body,
|
||||||
|
/// http::Request,
|
||||||
|
/// extract::DefaultBodyLimit,
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// let app = Router::new()
|
||||||
|
/// // this route has a different limit
|
||||||
|
/// .route("/", post(|request: Request<_>| async {}).layer(DefaultBodyLimit::max(1024)))
|
||||||
|
/// // this route still has the default limit
|
||||||
|
/// .route("/foo", post(|request: Request<_>| async {}));
|
||||||
|
/// # let _: Router = app;
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
/// [`Body::data`]: http_body::Body::data
|
/// [`Body::data`]: http_body::Body::data
|
||||||
/// [`Bytes`]: bytes::Bytes
|
/// [`Bytes`]: bytes::Bytes
|
||||||
/// [`Json`]: https://docs.rs/axum/0.6.0/axum/struct.Json.html
|
/// [`Json`]: https://docs.rs/axum/0.6.0/axum/struct.Json.html
|
||||||
|
|||||||
@@ -743,6 +743,75 @@ async fn changing_the_default_limit() {
|
|||||||
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[crate::test]
|
||||||
|
async fn changing_the_default_limit_differently_on_different_routes() {
|
||||||
|
let limit1 = 2;
|
||||||
|
let limit2 = 10;
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route(
|
||||||
|
"/limit1",
|
||||||
|
post(|_: Bytes| async {}).layer(DefaultBodyLimit::max(limit1)),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/limit2",
|
||||||
|
post(|_: Bytes| async {}).layer(DefaultBodyLimit::max(limit2)),
|
||||||
|
)
|
||||||
|
.route("/default", post(|_: Bytes| async {}));
|
||||||
|
|
||||||
|
let client = TestClient::new(app);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/limit1")
|
||||||
|
.body(reqwest::Body::from("a".repeat(limit1)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/limit1")
|
||||||
|
.body(reqwest::Body::from("a".repeat(limit2)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/limit2")
|
||||||
|
.body(reqwest::Body::from("a".repeat(limit1)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/limit2")
|
||||||
|
.body(reqwest::Body::from("a".repeat(limit2)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/limit2")
|
||||||
|
.body(reqwest::Body::from("a".repeat(limit1 + limit2)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/default")
|
||||||
|
.body(reqwest::Body::from("a".repeat(limit1 + limit2)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.post("/default")
|
||||||
|
// `DEFAULT_LIMIT` is 2mb so make a body larger than that
|
||||||
|
.body(reqwest::Body::from("a".repeat(3_000_000)))
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
|
||||||
|
}
|
||||||
|
|
||||||
#[crate::test]
|
#[crate::test]
|
||||||
async fn limited_body_with_streaming_body() {
|
async fn limited_body_with_streaming_body() {
|
||||||
const LIMIT: usize = 3;
|
const LIMIT: usize = 3;
|
||||||
|
|||||||
Reference in New Issue
Block a user