product-os-request 0.0.55

Product OS : Request provides a fully featured HTTP request library combining elements of reqwest and hyper for async requests with a series of helper methods to allow for easier usage depending upon your needs for one-time or repeat usage.
Documentation
// API stability tests - ensures public API surface remains intact
#![cfg(any(feature = "method", feature = "method_std"))]

use product_os_request::Method;

// Test that Method enum variants exist and are accessible
#[test]
fn test_method_variants_exist() {
    let _ = Method::GET;
    let _ = Method::POST;
    let _ = Method::PUT;
    let _ = Method::PATCH;
    let _ = Method::DELETE;
    let _ = Method::HEAD;
    let _ = Method::OPTIONS;
    let _ = Method::TRACE;
    let _ = Method::CONNECT;
    let _ = Method::ANY;
}

// Test that Method implements required traits
#[test]
fn test_method_traits() {
    let method = Method::GET;

    // Clone
    let cloned = method.clone();
    assert_eq!(method, cloned);

    // Debug
    let debug = format!("{:?}", method);
    assert!(debug.contains("GET"));

    // PartialEq
    assert_eq!(method, Method::GET);
    assert_ne!(method, Method::POST);
}

// Test that Method conversion methods exist
#[test]
fn test_method_conversion_api() {
    let method = Method::GET;

    // to_http_method should exist
    let http_method = method.to_http_method();
    assert_eq!(http_method, product_os_http::Method::GET);

    // from_str should exist
    let parsed = Method::from_str("GET");
    assert_eq!(parsed, Method::GET);

    // from_http_method should exist
    let from_http = Method::from_http_method(&product_os_http::Method::POST);
    assert_eq!(from_http, Method::POST);
}

#[cfg(any(feature = "method", feature = "method_std"))]
#[test]
fn test_method_serde() {
    use serde::{Deserialize, Serialize};

    // Method should implement Serialize and Deserialize
    fn assert_serialize<T: Serialize>() {}
    fn assert_deserialize<'de, T: Deserialize<'de>>() {}

    assert_serialize::<Method>();
    assert_deserialize::<Method>();
}

#[cfg(any(feature = "request", feature = "request_std"))]
mod request_api_tests {
    use product_os_request::{ProductOSRequestError, Protocol, Proxy, RedirectPolicy};

    // Test Protocol variants exist
    #[test]
    fn test_protocol_variants() {
        let _ = Protocol::SOCKS5;
        let _ = Protocol::HTTP;
        let _ = Protocol::HTTPS;
        let _ = Protocol::ALL;
    }

    // Test Protocol traits
    #[test]
    fn test_protocol_traits() {
        let protocol = Protocol::HTTP;
        let cloned = protocol.clone();
        assert_eq!(protocol, cloned);
        let debug = format!("{:?}", protocol);
        assert_eq!(debug, "HTTP");
    }

    // Test Proxy struct fields are accessible
    #[test]
    fn test_proxy_fields() {
        let proxy = Proxy {
            protocol: Protocol::HTTP,
            address: String::from("localhost:8080"),
        };

        assert_eq!(proxy.protocol, Protocol::HTTP);
        assert_eq!(proxy.address, "localhost:8080");
    }

    // Test RedirectPolicy variants
    #[test]
    fn test_redirect_policy_variants() {
        let none = RedirectPolicy::None;
        let default = RedirectPolicy::Default;
        let limit = RedirectPolicy::Limit(5);

        assert_eq!(none, RedirectPolicy::None);
        assert_eq!(default, RedirectPolicy::Default);
        assert_eq!(limit, RedirectPolicy::Limit(5));
    }

    // Test ProductOSRequestError variants
    #[test]
    fn test_error_variants() {
        let err = ProductOSRequestError::Error(String::from("test"));
        let display = format!("{}", err);
        assert_eq!(display, "test");

        let uri_err = ProductOSRequestError::InvalidUri(String::from("bad"));
        let display = format!("{}", uri_err);
        assert!(display.contains("Invalid URI"));
    }

    // Test ProductOSRequestError Display
    #[test]
    fn test_error_display() {
        use std::fmt::Display;

        fn assert_display<T: Display>() {}
        assert_display::<ProductOSRequestError>();
    }
}

#[cfg(all(
    feature = "request_std",
    any(feature = "std", feature = "std_reqwest", feature = "std_hyper")
))]
mod request_builder_api_tests {
    use product_os_request::{Method, ProductOSClient, ProductOSRequestClient};
    use std::collections::BTreeMap;

    // Test ProductOSRequest methods exist
    #[test]
    fn test_request_builder_methods() {
        let client = ProductOSRequestClient::new();
        let mut request = client.new_request(Method::GET, "http://example.com");

        // All these methods should exist and be callable
        request.add_header("key", "value", false);
        request.add_headers(BTreeMap::new(), false);
        request.bearer_auth(String::from("token"));
        request.add_param(String::from("k"), String::from("v"));
        request.add_params(BTreeMap::new());
        request.set_query(BTreeMap::new());
    }

    #[test]
    fn test_request_body_methods() {
        use bytes::Bytes;
        use product_os_http_body::BodyBytes;

        let client = ProductOSRequestClient::new();
        let mut request = client.new_request(Method::POST, "http://example.com");
        let body = BodyBytes::new(Bytes::new());

        request.set_body(body);
        let extracted = request.into_body();
        assert!(extracted.is_some());
    }
}

#[cfg(any(feature = "response", feature = "response_std"))]
mod response_api_tests {
    use bytes::Bytes;
    use product_os_http_body::BodyBytes;
    use product_os_request::{ProductOSResponse, StatusCode};

    // Test ProductOSResponse methods exist (new non-deprecated APIs)
    #[test]
    fn test_response_methods() {
        let body = BodyBytes::new(Bytes::new());
        let response = ProductOSResponse::from_body(body, String::from("http://example.com"));

        let status = response.try_status();
        assert_eq!(status, Some(StatusCode::OK));
        assert_eq!(response.status_code(), 200);
        let headers = response.try_get_headers();
        assert!(headers.is_some());
        let _ = response.response_url();
    }

    #[test]
    fn test_response_consuming_methods() {
        let body = BodyBytes::new(Bytes::new());
        let response = ProductOSResponse::from_body(body, String::from("http://example.com"));

        let result = response.into_body();
        assert!(result.is_some());
    }

    #[test]
    fn test_response_parts() {
        let body = BodyBytes::new(Bytes::new());
        let response = ProductOSResponse::from_body(body, String::from("http://example.com"));

        let result = response.try_parts();
        assert!(result.is_some());
    }

    #[test]
    fn test_response_from_response() {
        use product_os_http::Response;

        let http_response = Response::builder()
            .status(201)
            .body(BodyBytes::new(Bytes::from("created")))
            .unwrap();

        let response =
            ProductOSResponse::from_response(http_response, String::from("http://example.com"));
        assert_eq!(response.status_code(), 201);
    }
}

#[cfg(any(feature = "request", feature = "request_std"))]
mod requester_api_tests {
    use product_os_request::{ProductOSRequester, Protocol, RedirectPolicy};
    use std::collections::BTreeMap;

    // Test ProductOSRequester methods exist
    #[test]
    fn test_requester_construction() {
        let requester = ProductOSRequester::new();
        assert_eq!(requester.trusted_certificates().len(), 0);
    }

    #[test]
    fn test_requester_configuration_methods() {
        let mut requester = ProductOSRequester::new();

        // All these methods should exist
        requester.add_header("key", "value", false);
        requester.set_headers(BTreeMap::new());
        requester.force_secure(true);
        requester.trust_all_certificates(false);
        requester.trust_any_certificate_for_hostname(false);
        requester.set_timeout(1000);
        requester.set_connect_timeout(1000);
        requester.add_trusted_certificate_der(vec![]);
        let certs = requester.trusted_certificates();
        assert_eq!(certs.len(), 1);
        requester.set_redirect_policy(RedirectPolicy::Default);
        requester.set_proxy(None);
        requester.set_proxy(Some((Protocol::HTTP, String::from("localhost:8080"))));
    }

    #[test]
    fn test_requester_clone() {
        let requester = ProductOSRequester::new();
        let cloned = requester.clone();
        assert_eq!(
            requester.trusted_certificates().len(),
            cloned.trusted_certificates().len()
        );
    }
}

#[cfg(all(
    feature = "request_std",
    any(feature = "std", feature = "std_reqwest", feature = "std_hyper")
))]
mod client_api_tests {
    use product_os_request::{Method, ProductOSClient, ProductOSRequestClient, ProductOSRequester};

    // Test ProductOSRequestClient exists and can be created
    #[test]
    fn test_client_construction() {
        let _ = ProductOSRequestClient::new();
    }

    #[test]
    fn test_client_clone() {
        let client = ProductOSRequestClient::new();
        let _ = client.clone();
    }

    // Test ProductOSClient trait methods exist
    #[test]
    fn test_client_trait_methods() {
        let requester = ProductOSRequester::new();
        let mut client = ProductOSRequestClient::new();

        // build method
        client.build(&requester);

        // new_request method
        let _ = client.new_request(Method::GET, "http://example.com");
    }
}

// Re-exports test - ensure all public items are accessible from crate root
#[test]
fn test_public_reexports() {
    // Method types
    #[cfg(any(feature = "method", feature = "method_std"))]
    {
        use product_os_request::Method;
        let m = Method::GET;
        assert_eq!(m, Method::GET);
    }

    // Request types
    #[cfg(any(feature = "request", feature = "request_std"))]
    {
        use product_os_request::{
            ProductOSRequestError, ProductOSRequester, Protocol, RedirectPolicy,
        };
        assert_eq!(Protocol::HTTP, Protocol::HTTP);
        assert_eq!(RedirectPolicy::None, RedirectPolicy::None);
        let err = ProductOSRequestError::Error(String::from("test"));
        assert_eq!(format!("{}", err), "test");
        let _ = ProductOSRequester::new();
    }

    // Response types - StatusCode is re-exported
    #[cfg(any(feature = "response", feature = "response_std"))]
    {
        use product_os_request::StatusCode;
        assert_eq!(StatusCode::OK, StatusCode::OK);
    }
}