aro-web 1.0.0

HTTP/ADR layer for the Aro web framework
Documentation
#![cfg(feature = "test-utils")]
//! Tests for SSE (Server-Sent Events) support.

use std::convert::Infallible;

use aro_web::test::TestClient;
use aro_web::{App, Event, KeepAlive, Sse};
use axum::Router;
use axum::routing::get;
use tokio_stream::iter as stream_iter;

fn sse_app() -> Router {
    Router::new().route(
        "/events",
        get(|| async {
            let stream = stream_iter(vec![
                Ok::<_, Infallible>(Event::default().data("hello")),
                Ok(Event::default().data("world")),
            ]);
            Sse::new(stream).keep_alive(KeepAlive::default())
        }),
    )
}

#[tokio::test]
async fn sse_endpoint_returns_event_stream_content_type() {
    let client = TestClient::new(App::new().routes(sse_app()).build());
    let resp = client.get("/events").send().await;

    resp.assert_status(200);

    let content_type = resp
        .headers()
        .get("content-type")
        .expect("missing content-type header")
        .to_str()
        .unwrap();
    assert!(
        content_type.contains("text/event-stream"),
        "expected text/event-stream, got {content_type}"
    );
}

#[tokio::test]
async fn sse_endpoint_streams_events() {
    let client = TestClient::new(App::new().routes(sse_app()).build());
    let resp = client.get("/events").send().await;

    resp.assert_status(200);

    let body = resp.text();
    assert!(
        body.contains("data: hello"),
        "body missing 'data: hello': {body}"
    );
    assert!(
        body.contains("data: world"),
        "body missing 'data: world': {body}"
    );
}