From 46148a176c919d628754277d394a17256ce9902d Mon Sep 17 00:00:00 2001 From: Tiga Ultraman Date: Tue, 20 Feb 2024 10:08:13 +0800 Subject: [PATCH] isolate h1 tests with feature Signed-off-by: Tiga Ultraman --- ylong_http/src/response/mod.rs | 1 + .../examples/sync_https_outside.rs | 2 + ylong_http_client/src/sync_impl/client.rs | 12 +++++- .../src/util/c_openssl/adapter.rs | 42 +++++-------------- ylong_http_client/tests/common/mod.rs | 8 ++-- ylong_http_client/tests/common/sync_utils.rs | 2 - ylong_http_client/tests/tcp_server/mod.rs | 2 +- 7 files changed, 29 insertions(+), 40 deletions(-) diff --git a/ylong_http/src/response/mod.rs b/ylong_http/src/response/mod.rs index 1e4bfd8..2d0d129 100644 --- a/ylong_http/src/response/mod.rs +++ b/ylong_http/src/response/mod.rs @@ -102,6 +102,7 @@ pub struct ResponsePart { } #[cfg(test)] +#[cfg(feature = "http1_1")] mod ut_response { use crate::h1::ResponseDecoder; use crate::headers::Headers; diff --git a/ylong_http_client/examples/sync_https_outside.rs b/ylong_http_client/examples/sync_https_outside.rs index cbba111..5378135 100644 --- a/ylong_http_client/examples/sync_https_outside.rs +++ b/ylong_http_client/examples/sync_https_outside.rs @@ -11,6 +11,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! This is a simple synchronous HTTPS client example. + use ylong_http_client::sync_impl::Client; use ylong_http_client::util::Redirect; use ylong_http_client::{Certificate, HttpClientError, Request, TlsVersion}; diff --git a/ylong_http_client/src/sync_impl/client.rs b/ylong_http_client/src/sync_impl/client.rs index 83a61ba..8abd750 100644 --- a/ylong_http_client/src/sync_impl/client.rs +++ b/ylong_http_client/src/sync_impl/client.rs @@ -391,7 +391,17 @@ impl ClientBuilder { /// # } /// ``` pub fn add_root_certificate(mut self, certs: crate::util::Certificate) -> Self { - self.tls = self.tls.add_root_certificates(certs); + use crate::c_openssl::adapter::CertificateList; + + match certs.into_inner() { + CertificateList::CertList(c) => { + self.tls = self.tls.add_root_certificates(c); + } + #[cfg(feature = "c_openssl_3_0")] + CertificateList::PathList(p) => { + self.tls = self.tls.add_path_certificates(p); + } + } self } diff --git a/ylong_http_client/src/util/c_openssl/adapter.rs b/ylong_http_client/src/util/c_openssl/adapter.rs index c3e0afe..66e7afb 100644 --- a/ylong_http_client/src/util/c_openssl/adapter.rs +++ b/ylong_http_client/src/util/c_openssl/adapter.rs @@ -228,22 +228,10 @@ impl TlsConfigBuilder { /// # Examples /// /// ``` - /// use ylong_http_client::async_impl::Client; - /// use ylong_http_client::{Certificate, TlsVersion}; - /// - /// let cert1 = Certificate::from_pem(include_bytes!("../../../tests/file/root-ca.pem")).unwrap(); - /// let cert2 = Certificate::from_pem(include_bytes!("../../../tests/file/cert.pem")).unwrap(); - /// - /// // Creates a `Client` - /// let client = Client::builder() - /// .tls_built_in_root_certs(false) - /// .danger_accept_invalid_certs(false) - /// .max_tls_version(TlsVersion::TLS_1_2) - /// .min_tls_version(TlsVersion::TLS_1_2) - /// .add_root_certificate(cert1) - /// .add_root_certificate(cert2) - /// .build() - /// .unwrap(); + /// use ylong_http_client::{Cert, TlsConfigBuilder}; + /// # fn example(certs: Vec) { + /// let builder = TlsConfigBuilder::new().add_root_certificates(certs); + /// # } /// ``` pub fn add_root_certificates(mut self, mut certs: Vec) -> Self { self.certs_list.append(&mut certs); @@ -255,20 +243,10 @@ impl TlsConfigBuilder { /// # Examples /// /// ``` - /// use ylong_http_client::async_impl::Client; - /// use ylong_http_client::{Certificate, TlsVersion}; - /// - /// let path_cert = Certificate::from_path("../../../cert/file").unwrap(); - /// - /// // Creates a `Client` - /// let client = Client::builder() - /// .tls_built_in_root_certs(false) - /// .danger_accept_invalid_certs(false) - /// .max_tls_version(TlsVersion::TLS_1_2) - /// .min_tls_version(TlsVersion::TLS_1_2) - /// .add_root_certificate(path_cert) - /// .build() - /// .unwrap(); + /// use ylong_http_client::TlsConfigBuilder; + /// # fn example(path: String) { + /// let builder = TlsConfigBuilder::new().add_path_certificates(path); + /// # } /// ``` #[cfg(feature = "c_openssl_3_0")] pub fn add_path_certificates(mut self, path: String) -> Self { @@ -821,9 +799,11 @@ mod ut_openssl_adapter { fn ut_add_root_certificates() { let certificate = Certificate::from_pem(include_bytes!("../../../tests/file/root-ca.pem")) .expect("Sets certs error."); + #[cfg(feature = "c_openssl_1_1")] + let CertificateList::CertList(certs) = certificate.inner; + #[cfg(feature = "c_openssl_3_0")] let certs = match certificate.inner { CertificateList::CertList(c) => c, - #[cfg(feature = "c_openssl_3_0")] CertificateList::PathList(_) => vec![], }; diff --git a/ylong_http_client/tests/common/mod.rs b/ylong_http_client/tests/common/mod.rs index 3a0b6dc..5c96606 100644 --- a/ylong_http_client/tests/common/mod.rs +++ b/ylong_http_client/tests/common/mod.rs @@ -304,7 +304,6 @@ macro_rules! set_server_fn { $server_fn_name: ident, $(Request: { Method: $method: expr, - Host: $host: expr, $( Header: $req_n: expr, $req_v: expr, )* @@ -325,11 +324,10 @@ macro_rules! set_server_fn { $( $method => { assert_eq!($method, request.method().as_str(), "Assert request method failed"); - assert_eq!( - $host, - format!("{}://{}", request.uri().scheme().expect("assert uri scheme failed !").as_str(), request.uri().host().expect("assert uri host failed !")), - "Assert request host failed", + "/", + request.uri().to_string(), + "Assert request uri failed", ); assert_eq!( $version, diff --git a/ylong_http_client/tests/common/sync_utils.rs b/ylong_http_client/tests/common/sync_utils.rs index b9a1a6c..aad7786 100644 --- a/ylong_http_client/tests/common/sync_utils.rs +++ b/ylong_http_client/tests/common/sync_utils.rs @@ -41,7 +41,6 @@ macro_rules! sync_client_test_case { ylong_server_fn, $(Request: { Method: $method, - Host: $host, $( Header: $req_n, $req_v, )* @@ -130,7 +129,6 @@ macro_rules! sync_client_test_case { ylong_server_fn, $(Request: { Method: $method, - Host: $host, $( Header: $req_n, $req_v, )* diff --git a/ylong_http_client/tests/tcp_server/mod.rs b/ylong_http_client/tests/tcp_server/mod.rs index 41d57b8..fc899f3 100644 --- a/ylong_http_client/tests/tcp_server/mod.rs +++ b/ylong_http_client/tests/tcp_server/mod.rs @@ -184,7 +184,7 @@ macro_rules! start_tcp_server { let mut length = 0; let crlf = "\r\n"; let request_str = String::from_utf8_lossy(&buf[..size]); - let request_line = format!("{} http://{}{} {}{}", $method, addr.to_string().as_str(), $path, "HTTP/1.1", crlf); + let request_line = format!("{} {} {}{}", $method, $path, "HTTP/1.1", crlf); assert!(&buf[..size].starts_with(request_line.as_bytes()), "Incorrect Request-Line!"); length += request_line.len(); -- Gitee