zenwave 0.4.0

Ergonomic HTTP client framework.
Documentation
//! Tests for backend implementations

#[cfg(any(feature = "hyper-backend", feature = "curl-backend"))]
use http_kit::{Endpoint, Method};
#[cfg(feature = "hyper-backend")]
use zenwave::backend::HyperBackend;

#[cfg(any(feature = "hyper-backend", feature = "curl-backend"))]
mod common;
#[cfg(any(feature = "hyper-backend", feature = "curl-backend"))]
use common::httpbin_uri;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg(feature = "hyper-backend")]
fn test_hyper_backend_creation() {
    let backend = HyperBackend::new();
    // Just ensure it can be created
    assert!(!format!("{backend:?}").is_empty());
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg(feature = "hyper-backend")]
fn test_hyper_backend_default() {
    let backend = HyperBackend::default();
    assert!(!format!("{backend:?}").is_empty());
}

#[test_executors::async_test]
#[cfg(feature = "hyper-backend")]
async fn test_hyper_backend_get_request() {
    let mut backend = HyperBackend::new();
    let mut request = http::Request::builder()
        .method(Method::GET)
        .uri(httpbin_uri("/get"))
        .body(http_kit::Body::empty())
        .unwrap();
    let response = backend.respond(&mut request).await;
    assert!(response.is_ok());
    let response = response.unwrap();
    assert!(response.status().is_success());
}

#[test_executors::async_test]
#[cfg(feature = "hyper-backend")]
async fn test_hyper_backend_post_request() {
    let mut backend = HyperBackend::new();
    let mut request = http::Request::builder()
        .method(Method::POST)
        .uri(httpbin_uri("/post"))
        .body(http_kit::Body::empty())
        .unwrap();
    let response = backend.respond(&mut request).await;
    assert!(response.is_ok());
    let response = response.unwrap();
    assert!(response.status().is_success());
}

#[test_executors::async_test]
#[cfg(feature = "hyper-backend")]
async fn test_hyper_backend_https_request() {
    let mut backend = HyperBackend::new();
    let mut request = http::Request::builder()
        .method(Method::GET)
        .uri(httpbin_uri("/get"))
        .body(http_kit::Body::empty())
        .unwrap();
    let response = backend.respond(&mut request).await;
    assert!(response.is_ok());
    let response = response.unwrap();
    assert!(response.status().is_success());
}

#[test_executors::async_test]
#[cfg(feature = "hyper-backend")]
async fn test_hyper_backend_invalid_uri() {
    let mut backend = HyperBackend::new();
    let mut request = http::Request::builder()
        .method(Method::GET)
        .uri("invalid-uri")
        .body(http_kit::Body::empty())
        .unwrap();
    let response = backend.respond(&mut request).await;
    assert!(response.is_err());
}

#[test_executors::async_test]
#[cfg(feature = "hyper-backend")]
async fn test_hyper_backend_http_error_returns_err() {
    let mut backend = HyperBackend::new();
    let mut request = http::Request::builder()
        .method(Method::GET)
        .uri(httpbin_uri("/status/404"))
        .body(http_kit::Body::empty())
        .unwrap();

    let response = backend.respond(&mut request).await;
    assert!(
        response.is_err(),
        "expected non-success status to surface as Err"
    );
}

#[test_executors::async_test]
#[cfg(all(not(target_arch = "wasm32"), feature = "curl-backend"))]
async fn test_curl_backend_http_error_returns_err() {
    use zenwave::backend::CurlBackend;

    let mut backend = CurlBackend::new();
    let mut request = http::Request::builder()
        .method(Method::GET)
        .uri(httpbin_uri("/status/500"))
        .body(http_kit::Body::empty())
        .unwrap();

    let response = backend.respond(&mut request).await;
    assert!(
        response.is_err(),
        "expected non-success status to surface as Err"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg(feature = "hyper-backend")]
#[cfg(not(target_arch = "wasm32"))]
fn test_hyper_backend_request_cancellation() {}

// Note: WebBackend tests are more challenging to write without a browser environment
// These would typically require wasm-pack test or a specialized test runner
#[cfg(target_arch = "wasm32")]
mod web_backend_tests {
    use super::*;
    use zenwave::backend::WebBackend;

    #[test_executors::async_test]
    async fn test_web_backend_creation() {
        let backend = WebBackend::new();
        // Basic creation test - we can't test much without a browser context
    }

    #[test_executors::async_test]
    async fn test_web_backend_default() {
        let backend = WebBackend::default();
        // Basic default test
    }
}