diff --git a/ylong_http_client/tests/sdv_async_dns.rs b/ylong_http_client/tests/sdv_async_dns.rs new file mode 100644 index 0000000000000000000000000000000000000000..0459a4addf6a760f1670b712e08d5f661a4a06a0 --- /dev/null +++ b/ylong_http_client/tests/sdv_async_dns.rs @@ -0,0 +1,109 @@ +// Copyright (c) 2025 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![cfg(all(feature = "async", feature = "tokio_base", feature = "http1_1"))] + +use std::net::SocketAddr; +use std::time::{Duration, Instant}; + +use ylong_http_client::async_impl::{DefaultDnsResolver, Resolver}; + +#[cfg(feature = "__c_openssl")] +mod common; + +#[tokio::test] +async fn sdv_client_request_dns_default() { + let authority = "example.com:0"; + let resolver = DefaultDnsResolver::new().set_ttl(Duration::from_secs(50)); + let start1 = Instant::now(); + let addrs1 = resolver.resolve(authority).await; + let duration1 = start1.elapsed(); + assert!(addrs1.is_ok()); + tokio::time::sleep(Duration::from_millis(10)).await; + let start2 = Instant::now(); + let addrs2 = resolver.resolve(authority).await; + let duration2 = start2.elapsed(); + assert!(duration1 > duration2); + assert!(addrs2.is_ok()); + if let (Ok(addr1), Ok(addr2)) = (addrs1, addrs2) { + let vec1: Vec = addr1.collect(); + let vec2: Vec = addr2.collect(); + assert_eq!(vec1, vec2); + } +} + +#[cfg(feature = "__c_openssl")] +#[test] +fn sdv_client_request_dns_doh() { + use ylong_http_client::async_impl::DohResolver; + async fn server_fn( + _req: hyper::Request, + ) -> Result, std::convert::Infallible> { + let body = "{\"Status\":0,\"TC\":false,\"RD\":true,\"RA\":true,\"AD\":false,\"CD\":false,\"Question\":[{\"name\":\"example.com.\",\"type\":1}],\"Answer\":[{\"name\":\"example.com.\",\"type\":1,\"TTL\":168,\"data\":\"23.215.0.138\"},{\"name\":\"example.com.\",\"type\":1,\"TTL\":168,\"data\":\"96.7.128.175\"},{\"name\":\"example.com.\",\"type\":1,\"TTL\":168,\"data\":\"96.7.128.198\"},{\"name\":\"example.com.\",\"type\":1,\"TTL\":168,\"data\":\"23.192.228.80\"},{\"name\":\"example.com.\",\"type\":1,\"TTL\":168,\"data\":\"23.192.228.84\"},{\"name\":\"example.com.\",\"type\":1,\"TTL\":168,\"data\":\"23.215.0.136\"}]}"; + let response = hyper::Response::builder() + .status(hyper::StatusCode::OK) + .body(hyper::Body::from(body)) + .expect("build hyper response failed"); + Ok(response) + } + define_service_handle!(HTTP;); + + let rt = crate::common::init_test_work_runtime(4); + + let (tx, rx) = std::sync::mpsc::channel(); + + rt.block_on(async move { + let mut handle = start_http_server!(HTTP; server_fn); + handle + .server_start + .recv() + .await + .expect("recv server start msg failed !"); + tx.send(handle) + .expect("send Handle out the server coroutine failed !"); + }); + + let mut handle = rx.recv().expect("recv Handle failed !"); + + rt.block_on(async { + let authority = "example1.com:0"; + let resolver = DohResolver::new(format!("{}:{}", "http://localhost", handle.port).as_str()) + .set_ttl(Duration::from_secs(50)); + let start1 = Instant::now(); + let addrs1 = resolver.resolve(authority).await; + let duration1 = start1.elapsed(); + assert!(addrs1.is_ok()); + tokio::time::sleep(Duration::from_millis(10)).await; + let start2 = Instant::now(); + let addrs2 = resolver.resolve(authority).await; + let duration2 = start2.elapsed(); + assert!(duration1 > duration2); + assert!(addrs2.is_ok()); + if let (Ok(addr1), Ok(addr2)) = (addrs1, addrs2) { + let vec1: Vec = addr1.collect(); + let vec2: Vec = addr2.collect(); + assert_eq!(vec1, vec2); + } + + handle + .client_shutdown + .send(()) + .await + .expect("send client shutdown"); + handle + .server_shutdown + .recv() + .await + .expect("server shutdown"); + }); +}