From 572e1e9d89ae64937bc9c0493eed68af96078831 Mon Sep 17 00:00:00 2001 From: unique Date: Mon, 18 Sep 2023 16:16:13 +0545 Subject: [PATCH] > bug fixes on tests > code refactor --- server/src/uniqx.rs | 2 +- src/main.rs | 2 +- tests/test.rs | 23 ++++++++++------------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/server/src/uniqx.rs b/server/src/uniqx.rs index f21144b..4fc6f27 100644 --- a/server/src/uniqx.rs +++ b/server/src/uniqx.rs @@ -18,7 +18,7 @@ pub struct UniqxServer { server_context: Arc, } impl UniqxServer { - pub async fn new(domain: String, http_port: u16) -> UniqxServer { + pub fn new(domain: String, http_port: u16) -> UniqxServer { UniqxServer { domain, server_context: Arc::new(ServerContext::default()), diff --git a/src/main.rs b/src/main.rs index 271576a..0cd9c44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ async fn run(command: Command) -> Result<()> { client.start().await?; } Command::Server { domain, http_port } => { - let tunnel = server::uniqx::UniqxServer::new(domain, http_port).await; + let tunnel = server::uniqx::UniqxServer::new(domain, http_port); tunnel.start().await?; let (tx, rx) = channel(); ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel.")) diff --git a/tests/test.rs b/tests/test.rs index 4e87b90..5573310 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,11 +1,8 @@ -use std::cell::OnceCell; -use std::process::exit; use std::sync::OnceLock; use std::time::Duration; use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; use client::uniqx::UniqxClient; -use rstest::rstest; use server::uniqx::UniqxServer; use shared::utils::validate_subdomain; use shared::SERVER_PORT; @@ -20,12 +17,11 @@ use tokio::time; static GUARD: OnceLock> = OnceLock::new(); /// Spawn the server, giving some time for the control port TcpListener to start. async fn spawn_server() { - tokio::spawn(UniqxServer::new("localhost".to_owned(), 8001).await.start()); - // time::sleep(Duration::from_millis(900)).await; + tokio::spawn(UniqxServer::new("localhost".to_owned(), 65454).start()); } /// Spawns a client with randomly assigned ports, returning the listener and remote address. -async fn spawn_http_client() -> Result { +async fn spawn_http_client() -> Result<(SocketAddr, SocketAddr)> { const LOCAL_POST: u16 = 65432; let client = UniqxClient::new( shared::Protocol::HTTP, @@ -40,8 +36,9 @@ async fn spawn_http_client() -> Result { .unwrap(); tokio::spawn(client.start()); let local_http_addr: SocketAddr = ([127, 0, 0, 1], LOCAL_POST).into(); + let remote_addr: SocketAddr = ([127, 0, 0, 1], 65454).into(); tokio::time::sleep(Duration::from_millis(100)).await; - Ok(local_http_addr) + Ok((remote_addr, local_http_addr)) } /// Spawns a client with randomly assigned ports, returning the listener and remote address. async fn spawn_tcp_client() -> Result<(TcpListener, SocketAddr)> { @@ -140,25 +137,25 @@ async fn http_proxy() -> Result<()> { spawn_server().await; - let addr = spawn_http_client().await?; + let (remote_addr, local_addr) = spawn_http_client().await?; let listner = HttpServer::new(move || { App::new().route( "/test", - web::get().to(|req: HttpRequest| async { - // assert_eq!(req.query_string(), "foo=bar"); + web::get().to(|req: HttpRequest| async move { + assert_eq!(req.query_string(), "foo=bar"); HttpResponse::Ok().body("Hello world!") }), ) }) - .bind(addr) + .bind(local_addr) .unwrap(); tokio::spawn(listner.disable_signals().run()); - let mut stream = TcpStream::connect(addr).await?; + let mut stream = TcpStream::connect(remote_addr).await?; let request = "GET /test?foo=bar HTTP/1.1\r\n\ - Host: localhost:8080\r\n\ + Host: test.localhost:65454\r\n\ Connection: close\r\n\ \r\n";