aro-web 1.0.0

HTTP/ADR layer for the Aro web framework
Documentation
//! Integration tests for the compression middleware.

#![cfg(feature = "compression")]

use aro_web::middleware::compression;
use axum::Router;
use axum::body::Body;
use axum::http::header::{ACCEPT_ENCODING, CONTENT_ENCODING};
use axum::http::{Request, StatusCode};
use axum::routing::get;
use tower::ServiceExt;

/// Returns a large-enough JSON body to trigger compression.
async fn json_handler() -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "message": "hello world from aro compression test",
        "data": "abcdefghijklmnopqrstuvwxyz".repeat(100),
    }))
}

#[tokio::test]
async fn gzip_compression_applied_when_accepted() {
    let app = Router::new()
        .route("/data", get(json_handler))
        .layer(compression());

    let req = Request::builder()
        .uri("/data")
        .header(ACCEPT_ENCODING, "gzip")
        .body(Body::empty())
        .unwrap();

    let resp = app.oneshot(req).await.unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert_eq!(
        resp.headers()
            .get(CONTENT_ENCODING)
            .map(|v| v.to_str().unwrap()),
        Some("gzip"),
    );
}

#[tokio::test]
async fn no_compression_without_accept_encoding() {
    let app = Router::new()
        .route("/data", get(json_handler))
        .layer(compression());

    let req = Request::builder().uri("/data").body(Body::empty()).unwrap();

    let resp = app.oneshot(req).await.unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert!(resp.headers().get(CONTENT_ENCODING).is_none());
}