Repository: tokio-rs/axum
File: axum-extra/src/extract/multipart.rs (Rust)

Code before:
        const BYTES: &[u8] = "<!doctype html><title>🦀</title>".as_bytes();

        async fn handle(mut multipart: Multipart) -> Result<(), MultipartError> {
            while let Some(field) = multipart.next_field().await? {
                field.bytes().await?;
            }
            Ok(())
        }

        let app = Router::new()
            .route("/", post(handle))
            .layer(DefaultBodyLimit::max(BYTES.len() - 1));

        let client = TestClient::new(app);

        let form =
            reqwest::multipart::Form::new().part("file", reqwest::multipart::Part::bytes(BYTES));

        let res = client.post("/").multipart(form).await;
        assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
    }

    #[tokio::test]
    async fn body_too_large_with_tracing() {
        tracing::subscriber::set_global_default(
            tracing_subscriber::FmtSubscriber::builder()
                .with_max_level(tracing::level_filters::LevelFilter::TRACE)
                .with_writer(std::io::sink)
                .finish(),
        )
        .unwrap();

        const BYTES: &[u8] = "<!doctype html><title>🦀</title>".as_bytes();

        async fn handle(mut multipart: Multipart) -> Result<(), MultipartError> {
            while let Some(field) = multipart.next_field().await? {
                field.bytes().await?;
            }
            Ok(())
        }

        let app = Router::new()
            .route("/", post(handle))
            .layer(DefaultBodyLimit::max(BYTES.len() - 1));

        let client = TestClient::new(app);

        let form =
            reqwest::multipart::Form::new().part("file", reqwest::multipart::Part::bytes(BYTES));

        let res = client.post("/").multipart(form).await;

Review comment (suggestion):
You might want to use something like [this](https://docs.rs/tracing/latest/tracing/subscriber/fn.with_default.html) instead. If someone added a second test like this, the initialization would panic.

Also, I don't think we need `tracing` dev dependency just `tracing-subscriber`.

Code after:
        const BYTES: &[u8] = "<!doctype html><title>🦀</title>".as_bytes();

        async fn handle(mut multipart: Multipart) -> Result<(), MultipartError> {
            while let Some(field) = multipart.next_field().await? {
                field.bytes().await?;
            }
            Ok(())
        }

        let app = Router::new()
            .route("/", post(handle))
            .layer(DefaultBodyLimit::max(BYTES.len() - 1));

        let client = TestClient::new(app);

        let form =
            reqwest::multipart::Form::new().part("file", reqwest::multipart::Part::bytes(BYTES));

        let res = client.post("/").multipart(form).await;
        assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
    }

    #[tokio::test]
    #[cfg(feature = "tracing")]
    async fn body_too_large_with_tracing() {
        const BYTES: &[u8] = "<!doctype html><title>🦀</title>".as_bytes();

        async fn handle(mut multipart: Multipart) -> impl IntoResponse {
            let result: Result<(), MultipartError> = async {
                while let Some(field) = multipart.next_field().await? {
                    field.bytes().await?;
                }
                Ok(())
            }
            .await;

            let subscriber = tracing_subscriber::FmtSubscriber::builder()
                .with_max_level(tracing::level_filters::LevelFilter::TRACE)
                .with_writer(std::io::sink)
                .finish();

            let guard = tracing::subscriber::set_default(subscriber);
            let response = result.into_response();
            drop(guard);

            response
        }

        let app = Router::new()
            .route("/", post(handle))
            .layer(DefaultBodyLimit::max(BYTES.len() - 1));