trillium-http 1.1.0

the http implementation for the trillium toolkit
Documentation
use indoc::{formatdoc, indoc};
use pretty_assertions::assert_eq;
use std::sync::Arc;
use test_harness::test;
use trillium_http::{Conn, HttpConfig, HttpContext, KnownHeaderName, SERVER_HEADER};
use trillium_testing::{RuntimeTrait, TestResult, TestTransport, harness};

const TEST_DATE: &str = "Tue, 21 Nov 2023 21:27:21 GMT";

async fn handler(mut conn: Conn<TestTransport>) -> Conn<TestTransport> {
    conn.set_status(200);
    conn.set_response_body("response: 0123456789");
    conn.response_headers_mut()
        .insert(KnownHeaderName::Date, TEST_DATE)
        .insert(
            KnownHeaderName::Connection,
            "close\r\nGET / HTTP/1.1\r\nHost: example.com\r\n\r\n",
        )
        .insert(KnownHeaderName::Server, SERVER_HEADER)
        .insert("Bad\r\nHeader", "true");
    conn
}

#[test(harness)]
async fn bad_headers() -> TestResult {
    let (client, server) = TestTransport::new();
    let runtime = trillium_testing::runtime();
    let context = Arc::new(
        HttpContext::default()
            .with_config(HttpConfig::default().without_panic_on_invalid_response_headers()),
    );
    let handle = runtime.spawn(context.run(server, handler));

    client.write_all(indoc! {"
        GET / HTTP/1.1\r
        Host: example.com\r
        Connection: close\r
        \r
    "});

    let expected_response = formatdoc! {"
        HTTP/1.1 200 OK\r
        Date: {TEST_DATE}\r
        Content-Length: 20\r
        Server: {SERVER_HEADER}\r
        \r
        response: 0123456789\
    "};

    assert_eq!(client.read_available_string().await, expected_response);

    handle.await.unwrap().unwrap();

    Ok(())
}