From 25a890f5f8faa1114b71588f1300a46ab634c3b3 Mon Sep 17 00:00:00 2001 From: 30061565 Date: Tue, 28 May 2024 16:35:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20response=20=E5=92=8C=20int?= =?UTF-8?q?erceptor=20=E7=9A=84=20ut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 30061565 Signed-off-by: Shenyx --- .../src/async_impl/interceptor/mod.rs | 167 ++++++++++++++++++ ylong_http_client/src/async_impl/response.rs | 107 +++++++++++ 2 files changed, 274 insertions(+) diff --git a/ylong_http_client/src/async_impl/interceptor/mod.rs b/ylong_http_client/src/async_impl/interceptor/mod.rs index da02e73..005823e 100644 --- a/ylong_http_client/src/async_impl/interceptor/mod.rs +++ b/ylong_http_client/src/async_impl/interceptor/mod.rs @@ -135,3 +135,170 @@ pub trait Interceptor { pub(crate) struct IdleInterceptor; impl Interceptor for IdleInterceptor {} + +#[cfg(test)] +mod ut_interceptor { + use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + use std::fmt::{Debug, Formatter}; + + use crate::async_impl::{ConnDetail, ConnProtocol}; + use crate::util::AlpnProtocol; + + impl PartialEq for ConnProtocol { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (ConnProtocol::Tcp, ConnProtocol::Tcp) => true, + (ConnProtocol::Udp, ConnProtocol::Udp) => true, + (ConnProtocol::Tcp, ConnProtocol::Udp) => false, + (ConnProtocol::Udp, ConnProtocol::Tcp) => false, + } + } + } + + impl Debug for ConnProtocol { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ConnProtocol::Tcp => write!(f, "Tcp"), + ConnProtocol::Udp => write!(f, "Udp"), + } + } + } + + /// UT test cases for `ConnDetail::protocol` + /// + /// # Brief + /// 1. Create a `ConnDetail` + /// 2. Gets the reference of a `Protocol` by `calling ConnDetail::protocol` + /// 3. Checks if the test result is correct + #[test] + fn ut_interceptor_protocol() { + let test_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000); + let conn_detail = ConnDetail { + protocol: ConnProtocol::Tcp, + alpn: None, + local: test_addr, + peer: test_addr, + addr: "".to_string(), + proxy: false, + }; + + let result = conn_detail.protocol(); + assert_eq!(result, &ConnProtocol::Tcp); + } + + /// UT test cases for `ConnDetail::local` + /// + /// # Brief + /// 1. Create a `ConnDetail` + /// 2. Gets the reference of a `Local` by `calling ConnDetail::local` + /// 3. Checks if the test result is correct + #[test] + fn ut_interceptor_local() { + let test_local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000); + let test_peer_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 4567); + let conn_detail = ConnDetail { + protocol: ConnProtocol::Tcp, + alpn: None, + local: test_local_addr, + peer: test_peer_addr, + addr: "".to_string(), + proxy: false, + }; + + let result_addr = conn_detail.local(); + assert_eq!(result_addr, test_local_addr); + } + + /// UT test cases for `ConnDetail::peer` + /// + /// # Brief + /// 1. Create a `ConnDetail` + /// 2. Gets the reference of a `Peer` by `calling ConnDetail::peer` + /// 3. Checks if the test result is correct + #[test] + fn ut_interceptor_peer() { + let test_local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000); + let test_peer_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 4567); + let conn_detail = ConnDetail { + protocol: ConnProtocol::Tcp, + alpn: None, + local: test_local_addr, + peer: test_peer_addr, + addr: "".to_string(), + proxy: false, + }; + + let result_addr = conn_detail.peer(); + assert_eq!(result_addr, test_peer_addr); + } + + /// UT test cases for `ConnDetail::addr` + /// + /// # Brief + /// 1. Create a `ConnDetail` + /// 2. Gets the reference of a `Addr` by `calling ConnDetail::addr` + /// 3. Checks if the test result is correct + #[test] + fn ut_interceptor_addr() { + let test_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000); + let conn_detail = ConnDetail { + protocol: ConnProtocol::Tcp, + alpn: None, + local: test_addr, + peer: test_addr, + addr: "this is an addr".to_string(), + proxy: false, + }; + + let result = conn_detail.addr(); + assert_eq!(result, "this is an addr"); + } + + /// UT test cases for `ConnDetail::proxy` + /// + /// # Brief + /// 1. Create a `ConnDetail` + /// 2. Gets the reference of a `Proxy` by `calling ConnDetail::proxy` + /// 3. Checks if the test result is correct + #[test] + fn ut_interceptor_proxy() { + let test_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000); + let conn_detail = ConnDetail { + protocol: ConnProtocol::Tcp, + alpn: None, + local: test_addr, + peer: test_addr, + addr: "".to_string(), + proxy: true, + }; + + let result = conn_detail.proxy(); + assert!(result); + } + + /// UT test cases for `ConnDetail::alpn` + /// + /// # Brief + /// 1. Create a `ConnDetail` + /// 2. Gets the reference of a `Alpn` by `calling ConnDetail::alpn` + /// 3. Checks if the test result is correct + #[test] + fn ut_interceptor_alpn() { + let alpn_http11: Option> = + Some(Vec::from(AlpnProtocol::HTTP11.wire_format_bytes())); + + let test_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000); + let conn_detail = ConnDetail { + protocol: ConnProtocol::Tcp, + alpn: alpn_http11, + local: test_addr, + peer: test_addr, + addr: "this is an addr".to_string(), + proxy: false, + }; + + let result = conn_detail.alpn(); + + assert_eq!(result.unwrap(), AlpnProtocol::HTTP11.wire_format_bytes()); + } +} diff --git a/ylong_http_client/src/async_impl/response.rs b/ylong_http_client/src/async_impl/response.rs index a611fd6..88cbbae 100644 --- a/ylong_http_client/src/async_impl/response.rs +++ b/ylong_http_client/src/async_impl/response.rs @@ -64,3 +64,110 @@ impl DerefMut for Response { &mut self.inner } } + +#[cfg(test)] +mod ut_client_response { + use std::sync::Arc; + + use ylong_http::response::status::StatusCode; + use ylong_http::response::{Response as Resp, ResponsePart}; + use ylong_http::version::Version; + + use crate::async_impl::interceptor::IdleInterceptor; + use crate::async_impl::{HttpBody, Response}; + use crate::util::normalizer::BodyLength; + + /// UT test cases for `Response::data` + /// + /// # Brief + /// 1. Creates a `Response` by calling `Response::new` + /// 2. Calls `data` method get box_stream's data + /// 3. Check if the test result is correct + #[test] + fn ut_client_response_data() { + let handle = ylong_runtime::spawn(async move { + ut_client_response_data_1().await; + }); + ylong_runtime::block_on(handle).unwrap(); + } + + async fn ut_client_response_data_1() { + let response_part = ResponsePart { + version: Version::HTTP1_1, + status: StatusCode::OK, + headers: Default::default(), + }; + let box_stream = Box::new( + "\ + 5\r\n\ + hello\r\n\ + C ; type = text ;end = !\r\n\ + hello world!\r\n\ + 0\r\n\r\n\ + " + .as_bytes(), + ); + let http_body = HttpBody::new( + Arc::new(IdleInterceptor), + BodyLength::Chunk, + box_stream, + "".as_bytes(), + ) + .unwrap(); + + let mut response = Response::new(Resp::from_raw_parts(response_part, http_body)); + + let mut buf = [0u8; 32]; + let read = response.data(&mut buf).await.unwrap(); + assert_eq!(read, 5); + assert_eq!(&buf[..read], b"hello"); + let read = response.data(&mut buf).await.unwrap(); + assert_eq!(read, 12); + assert_eq!(&buf[..read], b"hello world!"); + let read = response.data(&mut buf).await.unwrap(); + assert_eq!(read, 0); + assert_eq!(&buf[..read], b""); + } + + /// UT test cases for `Response::text` + /// + /// # Brief + /// 1. Create a `Response` by calling `Response::from_raw_parts`. + /// 2. Create a `Response` by calling `Response::new` + /// 3. Gets the reference of `text` by calling `Response::text` + /// 4. Check if the test result is correct + #[test] + fn ut_client_response_text() { + let handle = ylong_runtime::spawn(async move { + ut_client_response_text_1().await; + }); + ylong_runtime::block_on(handle).unwrap(); + } + + async fn ut_client_response_text_1() { + let box_stream = Box::new( + "\ + example:this is body" + .as_bytes(), + ); + + let part = ResponsePart { + version: Version::HTTP1_1, + status: StatusCode::OK, + headers: Default::default(), + }; + let body = HttpBody::new( + Arc::new(IdleInterceptor), + BodyLength::UntilClose, + box_stream, + "".as_bytes(), + ) + .unwrap(); + + let response_body = Resp::from_raw_parts(part, body); + let response = Response::new(response_body); + + let result_str = response.text().await.unwrap(); + assert_eq!(result_str, "example:this is body"); + } +} -- Gitee