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
//! Basic tests for the hyper implementation

#[cfg(all(feature = "std_hyper", feature = "json"))]
#[tokio::test]
async fn test_hyper_basic_get() {
    use product_os_request::{Method, ProductOSClient, ProductOSHyperClient, ProductOSRequester};

    let mut requester = ProductOSRequester::new();
    requester.set_timeout(5000);

    let mut client = ProductOSHyperClient::new();
    requester.build(&mut client);

    let request = client.new_request(Method::GET, "https://httpbin.org/get");
    let response = client.request(request).await;

    assert!(response.is_ok(), "Request should succeed");
    let response = response.unwrap();
    assert_eq!(response.status_code(), 200, "Status code should be 200");
}

#[cfg(all(feature = "std_hyper", feature = "json"))]
#[tokio::test]
async fn test_hyper_json_post() {
    use product_os_request::{Method, ProductOSClient, ProductOSHyperClient, ProductOSRequester};
    use serde_json::json;

    let mut requester = ProductOSRequester::new();
    requester.set_timeout(5000);

    let mut client = ProductOSHyperClient::new();
    requester.build(&mut client);

    let mut request = client.new_request(Method::POST, "https://httpbin.org/post");

    let json_data = json!({
        "test": "data",
        "number": 42
    });

    client.set_body_json(&mut request, json_data).await;

    let response = client.request(request).await;

    assert!(response.is_ok(), "Request should succeed");
    let response = response.unwrap();
    assert_eq!(response.status_code(), 200, "Status code should be 200");

    let json_response = client.json(response).await;
    assert!(json_response.is_ok(), "Should parse JSON response");
}

#[cfg(feature = "std_hyper")]
#[tokio::test]
async fn test_hyper_headers() {
    use product_os_request::{Method, ProductOSClient, ProductOSHyperClient, ProductOSRequester};

    let mut requester = ProductOSRequester::new();
    requester.set_timeout(5000);
    requester.add_header("X-Custom-Header", "test-value", false);

    let mut client = ProductOSHyperClient::new();
    requester.build(&mut client);

    let mut request = client.new_request(Method::GET, "https://httpbin.org/headers");
    request.add_header("X-Request-Header", "request-value", false);

    let response = client.request(request).await;

    assert!(response.is_ok(), "Request should succeed");
    let response = response.unwrap();
    assert_eq!(response.status_code(), 200, "Status code should be 200");
}

#[cfg(feature = "std_hyper")]
#[tokio::test]
async fn test_hyper_redirect() {
    use product_os_request::{
        Method, ProductOSClient, ProductOSHyperClient, ProductOSRequester, RedirectPolicy,
    };

    let mut requester = ProductOSRequester::new();
    requester.set_timeout(5000);
    requester.set_redirect_policy(RedirectPolicy::Limit(5));

    let mut client = ProductOSHyperClient::new();
    requester.build(&mut client);

    let request = client.new_request(Method::GET, "https://httpbin.org/redirect/2");
    let response = client.request(request).await;

    assert!(response.is_ok(), "Request should succeed after redirects");
    let response = response.unwrap();
    assert_eq!(
        response.status_code(),
        200,
        "Should reach final destination"
    );
}