laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Tests of `http::sse` that need an application.

use futures_util::stream::StreamExt;

use std::time::Duration;

use futures_util::stream;

use crate::Application;
use crate::http::sse::*;
use crate::routing::Router;
use crate::testing::MakesHttpRequests;

#[tokio::test]
async fn event_streams_end_when_the_server_begins_to_stop() {
    let app = Application::configure()
        .replace_env(crate::config::Env::from_iter([("APP_KEY", "")]))
        .with_routing(|routing, _| {
            routing.api(|route: &mut Router| {
                route.get("/forever", || async {
                    EventStream::new(stream::iter([Event::new("first", "1")]).chain(stream::pending()))
                });
            });
        })
        .create();

    let stop = async {
        tokio::time::sleep(Duration::from_millis(50)).await;
        app.begin_shutdown();
    };
    let streamed = tokio::time::timeout(Duration::from_secs(5), app.get("/api/forever"));
    let (response, ()) = tokio::join!(streamed, stop);

    let response = response.expect("the stream ends once the server begins to stop");
    assert_eq!(response.text(), "event: first\ndata: 1\n\n");
}