use std::sync::Arc;
use rustls::ClientConfig;
use crate::error::{AppError, AppResult, ErrorDetail};
pub(super) fn client_config() -> AppResult<ClientConfig> {
let mut roots = rustls::RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
.with_protocol_versions(&[&rustls::version::TLS13, &rustls::version::TLS12])
.map_err(|e| AppError::of_with_source(ErrorDetail::HttpClientBuild, e))
.map(|builder| builder.with_root_certificates(roots).with_no_client_auth())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_mozilla_anchor_reaches_the_store() {
let mut roots = rustls::RootCertStore::empty();
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
assert_eq!(roots.len(), webpki_roots::TLS_SERVER_ROOTS.len());
assert!(
roots.len() > 100,
"the Mozilla set holds hundreds of anchors; got {}",
roots.len()
);
}
#[test]
fn the_client_config_builds_without_a_process_default_provider() {
assert!(client_config().is_ok());
}
}