Skip to content

Commit

Permalink
> bug fixes on tests
Browse files Browse the repository at this point in the history
> code refactor
  • Loading branch information
unique1o1 committed Sep 18, 2023
1 parent 98bdb19 commit 572e1e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion server/src/uniqx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct UniqxServer {
server_context: Arc<ServerContext>,
}
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()),
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
Expand Down
23 changes: 10 additions & 13 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,12 +17,11 @@ use tokio::time;
static GUARD: OnceLock<Mutex<()>> = 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<SocketAddr> {
async fn spawn_http_client() -> Result<(SocketAddr, SocketAddr)> {
const LOCAL_POST: u16 = 65432;
let client = UniqxClient::new(
shared::Protocol::HTTP,
Expand All @@ -40,8 +36,9 @@ async fn spawn_http_client() -> Result<SocketAddr> {
.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)> {
Expand Down Expand Up @@ -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";

Expand Down

0 comments on commit 572e1e9

Please sign in to comment.