unb-client 2.0.3

unb outbound client: WebSocket + in-process dialing (native) over the sans-IO core; wasm-safe
Documentation
//! Browser WebTransport integration test (EXPERIMENTAL — excluded from the CI
//! completion gate; the native quinn↔wtransport smoke test is the normative gate).
//!
//! Run locally:
//! 1. `cargo run -p unb-server --example wt_echo` (prints URL + cert hash)
//! 2. `UNB_WT_URL=... UNB_WT_CERT_HASH=... wasm-pack test --chrome --headless crates/unb-client --features webtransport`
//!
//! Without the env vars the test is a no-op, so plain wasm test runs stay green.

#![cfg(all(target_arch = "wasm32", feature = "webtransport"))]

use bytes::Bytes;
use unb_client::{dial_endpoints, Endpoint, TransportKind};
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

fn hash_from_hex(hex: &str) -> [u8; 32] {
    let mut out = [0u8; 32];
    for (index, byte) in out.iter_mut().enumerate() {
        *byte = u8::from_str_radix(&hex[index * 2..index * 2 + 2], 16).unwrap();
    }
    out
}

#[wasm_bindgen_test]
async fn a_browser_dials_a_wtransport_server_and_echoes() {
    let (Some(url), Some(hash)) = (option_env!("UNB_WT_URL"), option_env!("UNB_WT_CERT_HASH"))
    else {
        return;
    };

    let set = vec![Endpoint {
        kind: TransportKind::WebTransport,
        address: url.to_string(),
        cert_hash: Some(hash_from_hex(hash)),
    }];
    let session = dial_endpoints(&set).await.unwrap();
    let response = session
        .fetch(
            http::Request::builder()
                .uri("/wt-echo/echo")
                .body(Bytes::from_static(br#"{"hello":"browser"}"#))
                .unwrap(),
            std::time::Duration::from_secs(5),
        )
        .await
        .unwrap();
    let body: serde_json::Value = serde_json::from_slice(response.body()).unwrap();
    assert_eq!(body["echo"]["hello"], "browser");
}