reqres 1.0.0

A pure Rust async HTTP client library based on Tokio with HTTP/2, connection pooling, proxy, cookie, compression, benchmarks, and comprehensive tests
Documentation
use reqres::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== reqres v0.6.0 Compression Demo ===\n");

    // 测试 1: 启用压缩的客户端
    println!("Test 1: Request with compression enabled\n");
    let client_with_compression = Client::builder()
        .enable_compression()
        .build()?;

    match client_with_compression.get("https://httpbin.org/gzip").await {
        Ok(response) => {
            println!("  ✓ Status: {}", response.status);
            println!("  ✓ Content-Length: {} bytes", response.body.len());

            // 检查响应头
            if let Some(encoding) = response.header("content-encoding") {
                println!("  ✓ Server sent compressed with: {}", encoding);
            }

            println!("  ✓ Response decompressed automatically");
            println!("  ✓ First 100 chars: {}...",
                String::from_utf8_lossy(&response.body[..std::cmp::min(100, response.body.len())]));
        }
        Err(e) => {
            println!("  ✗ Error: {}", e);
        }
    }

    println!("\n==================================================\n");

    // 测试 2: 禁用压缩的客户端
    println!("Test 2: Request without compression\n");
    let client_without_compression = Client::builder()
        .disable_compression()
        .build()?;

    match client_without_compression.get("https://httpbin.org/encoding/utf8").await {
        Ok(response) => {
            println!("  ✓ Status: {}", response.status);
            println!("  ✓ Content-Length: {} bytes", response.body.len());

            // 检查响应头
            if let Some(encoding) = response.header("content-encoding") {
                println!("  ✗ Server sent compressed with: {} (should not be compressed)", encoding);
            } else {
                println!("  ✓ No compression in response (as expected)");
            }

            // 验证响应是可读的文本
            let body_text = String::from_utf8_lossy(&response.body);
            if body_text.contains("The quick brown fox") {
                println!("  ✓ Response is readable text");
            }
        }
        Err(e) => {
            println!("  ✗ Error: {}", e);
        }
    }

    println!("\n==================================================\n");

    // 测试 3: 直接使用 Decompressor
    println!("Test 3: Direct Decompressor usage\n");
    use flate2::write::GzEncoder;
    use flate2::read::GzDecoder;
    use flate2::Compression;
    use std::io::{Read, Write};

    let original = "Hello, world! This is a test string for compression.".as_bytes();
    println!("  Original size: {} bytes", original.len());

    // 压缩
    let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(original).unwrap();
    let compressed = encoder.finish().unwrap();
    println!("  Compressed size: {} bytes", compressed.len());

    // 解压缩
    let mut decoder = GzDecoder::new(&compressed[..]);
    let mut decompressed = Vec::new();
    decoder.read_to_end(&mut decompressed).unwrap();
    println!("  Decompressed size: {} bytes", decompressed.len());

    assert_eq!(original, decompressed.as_slice());
    println!("  ✓ Decompression successful (data matches)");

    println!("\n=== Demo completed ===");
    Ok(())
}