use hyper_custom_cert::HttpClient;
#[cfg(any(
all(feature = "rustls", feature = "insecure-dangerous"),
all(feature = "native-tls", feature = "insecure-dangerous"),
feature = "insecure-dangerous",
not(any(feature = "rustls", feature = "insecure-dangerous")),
all(
feature = "native-tls",
feature = "rustls",
feature = "insecure-dangerous"
)
))]
use std::collections::HashMap;
use std::time::Duration;
#[allow(dead_code)]
const TEST_CA_PEM: &[u8] = b"-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAKoK/heBjcOuMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTcwODI4MTUxMzAyWhcNMjcwODI2MTUxMzAyWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAuuExKtKjKEw91uR8gqyUZx+wW3qZjUHq3oLe+RxbEUVFWApwrKE3XxKJ
-----END CERTIFICATE-----";
#[cfg(all(feature = "rustls", feature = "insecure-dangerous"))]
#[test]
fn rustls_and_insecure_combination() {
let _client = HttpClient::builder()
.with_root_ca_pem(TEST_CA_PEM)
.insecure_accept_invalid_certs(true)
.build();
}
#[cfg(all(feature = "rustls", feature = "insecure-dangerous"))]
#[test]
fn rustls_pinning_and_insecure_combination() {
let pins = vec![[
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08,
]];
let _client = HttpClient::builder()
.with_pinned_cert_sha256(pins)
.insecure_accept_invalid_certs(true)
.with_timeout(Duration::from_secs(30))
.build();
}
#[cfg(all(feature = "rustls", feature = "insecure-dangerous"))]
#[test]
fn full_rustls_insecure_configuration() {
let mut headers = HashMap::new();
headers.insert("X-Custom".to_string(), "test".to_string());
let pins = vec![[0u8; 32]];
let _client = HttpClient::builder()
.with_timeout(Duration::from_secs(45))
.with_default_headers(headers)
.with_root_ca_pem(TEST_CA_PEM)
.with_root_ca_pem(TEST_CA_PEM) .with_pinned_cert_sha256(pins)
.insecure_accept_invalid_certs(true)
.build();
}
#[cfg(all(feature = "native-tls", feature = "insecure-dangerous"))]
#[test]
fn native_tls_and_insecure_combination() {
let _client = HttpClient::builder()
.with_timeout(Duration::from_secs(20))
.insecure_accept_invalid_certs(true)
.build();
}
#[cfg(all(feature = "native-tls", feature = "insecure-dangerous"))]
#[test]
fn native_tls_insecure_with_headers() {
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), "Bearer test".to_string());
headers.insert("Content-Type".to_string(), "application/json".to_string());
let _client = HttpClient::builder()
.with_default_headers(headers)
.insecure_accept_invalid_certs(true)
.build();
}
#[cfg(not(any(feature = "rustls", feature = "insecure-dangerous")))]
#[test]
fn minimal_feature_set() {
let _client = HttpClient::builder()
.with_timeout(Duration::from_secs(30))
.build();
}
#[cfg(not(any(feature = "rustls", feature = "insecure-dangerous")))]
#[test]
fn minimal_with_headers_only() {
let mut headers = HashMap::new();
headers.insert("User-Agent".to_string(), "minimal-client".to_string());
let _client = HttpClient::builder()
.with_default_headers(headers)
.with_timeout(Duration::from_millis(5000))
.build();
}
#[cfg(all(
feature = "native-tls",
feature = "rustls",
feature = "insecure-dangerous"
))]
#[test]
fn all_features_enabled() {
let mut headers = HashMap::new();
headers.insert("X-All-Features".to_string(), "enabled".to_string());
let pins = vec![[0x42; 32]];
let _client = HttpClient::builder()
.with_timeout(Duration::from_secs(60))
.with_default_headers(headers)
.with_root_ca_pem(TEST_CA_PEM)
.with_pinned_cert_sha256(pins)
.insecure_accept_invalid_certs(false) .build();
}
#[cfg(all(
feature = "native-tls",
feature = "rustls",
feature = "insecure-dangerous"
))]
#[test]
fn all_features_insecure_enabled() {
let _client = HttpClient::builder()
.with_root_ca_pem(TEST_CA_PEM) .insecure_accept_invalid_certs(true)
.build();
}
#[test]
fn feature_availability_check() {
let _client = HttpClient::builder();
let _default_client = HttpClient::new();
let _builder = HttpClient::builder().with_timeout(Duration::from_secs(10));
#[cfg(feature = "rustls")]
{
let _rustls_client = HttpClient::builder().with_root_ca_pem(TEST_CA_PEM);
}
#[cfg(feature = "insecure-dangerous")]
{
let _insecure_client = HttpClient::builder().insecure_accept_invalid_certs(true);
}
}
#[cfg(feature = "rustls")]
#[test]
fn rustls_method_chaining() {
let _client = HttpClient::builder()
.with_timeout(Duration::from_secs(30))
.with_root_ca_pem(TEST_CA_PEM)
.with_root_ca_pem(TEST_CA_PEM) .build();
}
#[cfg(feature = "insecure-dangerous")]
#[test]
fn insecure_method_chaining() {
let mut headers = HashMap::new();
headers.insert("Test".to_string(), "chaining".to_string());
let _client = HttpClient::builder()
.with_timeout(Duration::from_millis(1000))
.with_default_headers(headers)
.insecure_accept_invalid_certs(true)
.build();
}
#[test]
fn basic_error_handling() {
let _client = HttpClient::new();
}
#[cfg(all(feature = "rustls", feature = "insecure-dangerous"))]
#[test]
fn complex_configuration_order() {
let pins = vec![[1u8; 32], [2u8; 32]];
let mut headers = HashMap::new();
headers.insert("Order".to_string(), "test".to_string());
let _client1 = HttpClient::builder()
.insecure_accept_invalid_certs(true)
.with_root_ca_pem(TEST_CA_PEM)
.with_pinned_cert_sha256(pins.clone())
.with_timeout(Duration::from_secs(15))
.with_default_headers(headers.clone())
.build();
let _client2 = HttpClient::builder()
.with_default_headers(headers)
.with_timeout(Duration::from_secs(15))
.with_pinned_cert_sha256(pins)
.with_root_ca_pem(TEST_CA_PEM)
.insecure_accept_invalid_certs(true)
.build();
}