Skip to content

Commit 1e48be4

Browse files
committed
qos_net: re-add proxy_connection unit test
1 parent 565b6f7 commit 1e48be4

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/qos_net/src/proxy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ impl Proxy {
8383

8484
let resp = match ProxyMsg::try_from_slice(&req_bytes) {
8585
Ok(req) => match req {
86-
// TODO: do we need this??
8786
ProxyMsg::StatusRequest => ProxyMsg::StatusResponse(0),
8887
ProxyMsg::ConnectByNameRequest {
8988
hostname,

src/qos_net/src/proxy_connection.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,61 @@ pub async fn resolve_hostname(
120120
))
121121
})
122122
}
123+
124+
#[cfg(test)]
125+
mod test {
126+
127+
use std::{io::ErrorKind, sync::Arc};
128+
129+
use rustls::RootCertStore;
130+
use tokio_rustls::TlsConnector;
131+
132+
use super::*;
133+
134+
#[tokio::test]
135+
async fn can_fetch_tls_content_with_proxy_connection() {
136+
let host = "api.turnkey.com";
137+
let path = "/health";
138+
139+
let mut remote_connection = ProxyConnection::new_from_name(
140+
host.to_string(),
141+
443,
142+
vec!["8.8.8.8".to_string()],
143+
53,
144+
)
145+
.await
146+
.unwrap();
147+
148+
let root_store =
149+
RootCertStore { roots: webpki_roots::TLS_SERVER_ROOTS.into() };
150+
151+
let server_name: rustls::pki_types::ServerName<'_> =
152+
host.try_into().unwrap();
153+
let config: rustls::ClientConfig = rustls::ClientConfig::builder()
154+
.with_root_certificates(root_store)
155+
.with_no_client_auth();
156+
let conn = TlsConnector::from(Arc::new(config));
157+
let stream = &mut remote_connection.tcp_stream;
158+
let mut tls = conn.connect(server_name, stream).await.unwrap();
159+
160+
let http_request = format!(
161+
"GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n"
162+
);
163+
164+
tls.write_all(http_request.as_bytes()).await.unwrap();
165+
166+
let mut response_bytes = Vec::new();
167+
let read_to_end_result = tls.read_to_end(&mut response_bytes).await;
168+
169+
// Ignore eof errors: https://docs.rs/rustls/latest/rustls/manual/_03_howto/index.html#unexpected-eof
170+
assert!(
171+
read_to_end_result.is_ok()
172+
|| (read_to_end_result
173+
.is_err_and(|e| e.kind() == ErrorKind::UnexpectedEof))
174+
);
175+
176+
let response_text = std::str::from_utf8(&response_bytes).unwrap();
177+
assert!(response_text.contains("HTTP/1.1 200 OK"));
178+
assert!(response_text.contains("currentTime"));
179+
}
180+
}

0 commit comments

Comments
 (0)