-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use dynamic_proxy::server::{HttpsConfig, SimpleHttpServer}; | ||
use plane::{ | ||
protocol::{RouteInfoRequest, RouteInfoResponse}, | ||
proxy::proxy_server::ProxyState, | ||
}; | ||
use std::net::SocketAddr; | ||
use tokio::{net::TcpListener, sync::mpsc}; | ||
|
||
pub struct MockProxy { | ||
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
Check warning on line 9 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippystruct `MockProxy` is never constructed
|
||
proxy_state: ProxyState, | ||
route_info_request_receiver: mpsc::Receiver<RouteInfoRequest>, | ||
addr: SocketAddr, | ||
server: SimpleHttpServer, | ||
} | ||
|
||
impl MockProxy { | ||
pub async fn new() -> Self { | ||
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
Check warning on line 17 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippyassociated items `new`, `addr`, `recv_route_info_request`, `expect_no_route_info_request`, and `send_route_info_response` are never used
|
||
let proxy_state = ProxyState::new(); | ||
let (route_info_request_sender, route_info_request_receiver) = mpsc::channel(1); | ||
|
||
proxy_state.inner.route_map.set_sender(move |m| { | ||
route_info_request_sender | ||
.try_send(m) | ||
.expect("Failed to send route info request"); | ||
}); | ||
|
||
let listener = TcpListener::bind("127.0.0.1:0") | ||
.await | ||
.expect("Failed to bind listener"); | ||
let addr = listener.local_addr().expect("Failed to get local address"); | ||
|
||
let server = SimpleHttpServer::new(proxy_state.clone(), listener, HttpsConfig::http()) | ||
.expect("Failed to create server"); | ||
|
||
Self { | ||
proxy_state, | ||
route_info_request_receiver, | ||
addr, | ||
server, | ||
} | ||
} | ||
|
||
pub fn addr(&self) -> SocketAddr { | ||
self.addr | ||
} | ||
|
||
pub async fn recv_route_info_request(&mut self) -> RouteInfoRequest { | ||
Check warning on line 47 in plane/plane-tests/tests/common/proxy_mock.rs GitHub Actions / clippymethods `recv_route_info_request` and `send_route_info_response` are never used
|
||
self.route_info_request_receiver | ||
.recv() | ||
.await | ||
.expect("Failed to receive route info request") | ||
} | ||
|
||
pub async fn expect_no_route_info_request(&mut self) { | ||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; | ||
assert!( | ||
self.route_info_request_receiver.is_empty(), | ||
"Expected no route info request, but got: {}", | ||
self.route_info_request_receiver.len() | ||
); | ||
} | ||
|
||
pub async fn send_route_info_response(&mut self, response: RouteInfoResponse) { | ||
self.proxy_state.inner.route_map.receive(response); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use axum::{body::Body, extract::Request, routing::any, Json, Router}; | ||
use http::Method; | ||
use http_body_util::BodyExt; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{collections::HashMap, net::SocketAddr}; | ||
use tokio::net::TcpListener; | ||
|
||
pub struct SimpleAxumServer { | ||
handle: tokio::task::JoinHandle<()>, | ||
addr: SocketAddr, | ||
} | ||
|
||
#[allow(unused)] | ||
impl SimpleAxumServer { | ||
pub async fn new() -> Self { | ||
let app = Router::new() | ||
.route("/*path", any(return_request_info)) | ||
.route("/", any(return_request_info)); | ||
|
||
let addr = SocketAddr::from(([127, 0, 0, 1], 0)); | ||
let tcp_listener = TcpListener::bind(addr).await.unwrap(); | ||
let addr = tcp_listener.local_addr().unwrap(); | ||
|
||
let handle = tokio::spawn(async { | ||
axum::serve(tcp_listener, app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
Self { handle, addr } | ||
} | ||
|
||
pub fn addr(&self) -> SocketAddr { | ||
self.addr | ||
} | ||
} | ||
|
||
impl Drop for SimpleAxumServer { | ||
fn drop(&mut self) { | ||
self.handle.abort(); | ||
} | ||
} | ||
|
||
// Handler function for the root route | ||
async fn return_request_info(method: Method, request: Request<Body>) -> Json<RequestInfo> { | ||
let method = method.to_string(); | ||
|
||
let path = request.uri().path().to_string(); | ||
let query = request.uri().query().unwrap_or("").to_string(); | ||
|
||
let headers: HashMap<String, String> = request | ||
.headers() | ||
.iter() | ||
.map(|(k, v)| (k.to_string(), v.to_str().unwrap().to_string())) | ||
.collect(); | ||
|
||
let body = request.into_body().collect().await.unwrap().to_bytes(); | ||
let body = String::from_utf8(body.to_vec()).unwrap(); | ||
|
||
Json(RequestInfo { | ||
path, | ||
query, | ||
method, | ||
headers, | ||
body, | ||
}) | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct RequestInfo { | ||
pub path: String, | ||
pub query: String, | ||
pub method: String, | ||
pub headers: HashMap<String, String>, | ||
pub body: String, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use common::{proxy_mock::MockProxy, test_env::TestEnvironment}; | ||
use plane_test_macro::plane_test; | ||
use reqwest::StatusCode; | ||
|
||
mod common; | ||
|
||
#[plane_test] | ||
async fn proxy_no_bearer_token(env: TestEnvironment) { | ||
let mut proxy = MockProxy::new().await; | ||
let url = format!("http://{}", proxy.addr()); | ||
let handle = tokio::spawn(async { reqwest::get(url).await.expect("Failed to send request") }); | ||
|
||
proxy.expect_no_route_info_request().await; | ||
|
||
let response = handle.await.unwrap(); | ||
|
||
assert_eq!(response.status(), StatusCode::UNAUTHORIZED); | ||
} |