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