diff --git a/src/connector/builder.rs b/src/connector/builder.rs index fdec7a8..a505e44 100644 --- a/src/connector/builder.rs +++ b/src/connector/builder.rs @@ -174,16 +174,22 @@ impl ConnectorBuilder { }) } - /// Enable all HTTP versions + /// Enable all HTTP versions built into this library (enabled with Cargo features) /// - /// For now, this enables both HTTP 1 and 2. In the future, other supported versions - /// will be enabled as well. - #[cfg(all(feature = "http1", feature = "http2"))] + /// For now, this could enable both HTTP 1 and 2, depending on active features. + /// In the future, other supported versions will be enabled as well. + #[cfg(feature = "http2")] + #[cfg_attr(docsrs, doc(cfg(feature = "http2")))] pub fn enable_all_versions(mut self) -> ConnectorBuilder { - self.0.tls_config.alpn_protocols = vec![b"h2".to_vec()]; + #[cfg(feature = "http1")] + let alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; + #[cfg(not(feature = "http1"))] + let alpn_protocols = vec![b"h2".to_vec()]; + + self.0.tls_config.alpn_protocols = alpn_protocols; ConnectorBuilder(WantsProtocols3 { inner: self.0, - enable_http1: true, + enable_http1: cfg!(feature = "http1"), }) } @@ -326,7 +332,7 @@ mod tests { .build(); assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]); let connector = super::ConnectorBuilder::new() - .with_tls_config(tls_config) + .with_tls_config(tls_config.clone()) .https_only() .enable_http1() .enable_http2() @@ -335,5 +341,36 @@ mod tests { &connector.tls_config.alpn_protocols, &[b"h2".to_vec(), b"http/1.1".to_vec()] ); + let connector = super::ConnectorBuilder::new() + .with_tls_config(tls_config) + .https_only() + .enable_all_versions() + .build(); + assert_eq!( + &connector.tls_config.alpn_protocols, + &[b"h2".to_vec(), b"http/1.1".to_vec()] + ); + } + + #[test] + #[cfg(all(not(feature = "http1"), feature = "http2"))] + fn test_alpn_http2() { + let roots = rustls::RootCertStore::empty(); + let tls_config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(roots) + .with_no_client_auth(); + let connector = super::ConnectorBuilder::new() + .with_tls_config(tls_config.clone()) + .https_only() + .enable_http2() + .build(); + assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]); + let connector = super::ConnectorBuilder::new() + .with_tls_config(tls_config) + .https_only() + .enable_all_versions() + .build(); + assert_eq!(&connector.tls_config.alpn_protocols, &[b"h2".to_vec()]); } }