Browse Source

refactor: added some tests

pull/22/head
KaranGauswami 10 months ago
parent
commit
46eda3018d
No known key found for this signature in database GPG Key ID: A4D57BC9D0772045
  1. 13
      .github/workflows/rust.yml
  2. 11
      tests/proxy.rs

13
.github/workflows/rust.yml

@ -2,21 +2,18 @@ name: Rust
on: on:
push: push:
branches: [ main ] branches: [main]
pull_request: pull_request:
branches: [ main ] branches: [main]
env: env:
CARGO_TERM_COLOR: always CARGO_TERM_COLOR: always
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Build - name: Run tests
run: cargo build --verbose run: cargo test --verbose -- --nocapture
- name: Run tests
run: cargo test --verbose

11
tests/proxy.rs

@ -1,4 +1,4 @@
use std::{net::SocketAddr, time::Duration}; use std::net::SocketAddr;
use http::StatusCode; use http::StatusCode;
use sthp::proxy_request; use sthp::proxy_request;
@ -9,11 +9,13 @@ use socksprox::Socks5Server;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
async fn start_socks_server() -> Result<(JoinHandle<()>, SocketAddr)> { async fn start_socks_server() -> Result<(JoinHandle<()>, SocketAddr)> {
// TODO: currently Socks5Server doesnt return what port it binded // TODO: Currently, Socks5Server does not return the port it is bound to.
// so we will use TcpListener to get the random port and release it immediatly // To work around this, we will use TcpListener to obtain a random available port.
// After retrieving the port, we will immediately release it.
let listener = TcpListener::bind("localhost:0").await?; let listener = TcpListener::bind("localhost:0").await?;
let addr = listener.local_addr()?; let addr = listener.local_addr()?;
let port = addr.port(); let port = addr.port();
eprintln!("socks proxy will listen on port {}", port);
// release port // release port
drop(listener); drop(listener);
@ -37,7 +39,7 @@ async fn simple_test() -> Result<()> {
eprintln!("new connection from: {:?}", proxy_addr); eprintln!("new connection from: {:?}", proxy_addr);
Ok::<_, color_eyre::eyre::Error>(()) Ok::<_, color_eyre::eyre::Error>(())
}); });
assert_eq!("hello", "hello"); eprintln!("http proxy will listen on {}", addr);
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
.proxy(reqwest::Proxy::http(format!( .proxy(reqwest::Proxy::http(format!(
@ -51,6 +53,5 @@ async fn simple_test() -> Result<()> {
client.get("http://example.org").send().await?.status(), client.get("http://example.org").send().await?.status(),
StatusCode::OK StatusCode::OK
); );
eprintln!("http proxy handle dropped");
Ok(()) Ok(())
} }

Loading…
Cancel
Save