qubit-http 0.5.2

General-purpose HTTP infrastructure for Rust with unified client semantics, secure logging, and built-in SSE decoding
Documentation
/*******************************************************************************
 *
 *    Copyright (c) 2025 - 2026 Haixing Hu.
 *
 *    SPDX-License-Identifier: Apache-2.0
 *
 *    Licensed under the Apache License, Version 2.0.
 *
 ******************************************************************************/

use bytes::Bytes;
use futures_util::StreamExt;
use http::{
    HeaderMap,
    Method,
    StatusCode,
};
use qubit_http::HttpResponse;
use url::Url;

#[tokio::test]
async fn test_event_decoder_decodes_multiple_sse_events_from_response_body() {
    let response = HttpResponse::new(
        StatusCode::OK,
        HeaderMap::new(),
        Bytes::from_static(b"id: 1\nevent: add\ndata: one\n\nid: 2\ndata: two\n\n"),
        Url::parse("https://example.com/events").expect("valid URL"),
        Method::GET,
    );

    let events = response
        .sse_events()
        .map(|item| item.expect("event should decode"))
        .collect::<Vec<_>>()
        .await;

    assert_eq!(events.len(), 2);
    assert_eq!(events[0].id.as_deref(), Some("1"));
    assert_eq!(events[0].event.as_deref(), Some("add"));
    assert_eq!(events[0].data, "one");
    assert_eq!(events[1].id.as_deref(), Some("2"));
    assert_eq!(events[1].data, "two");
}