Skip to content

Commit

Permalink
valid response has CORS headers
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 24, 2024
1 parent 3103312 commit 6da1708
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
82 changes: 79 additions & 3 deletions plane/plane-tests/tests/proxy_cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ use common::{
localhost_resolver::localhost_client, proxy_mock::MockProxy,
simple_axum_server::SimpleAxumServer, test_env::TestEnvironment,
};
use plane::{protocol::RouteInfoResponse, types::BearerToken};
use plane::{
log_types::BackendAddr,
names::{BackendName, Name},
protocol::{RouteInfo, RouteInfoResponse},
types::{BearerToken, ClusterName, SecretToken},
};
use plane_test_macro::plane_test;
use reqwest::StatusCode;
use std::str::FromStr;

mod common;

#[plane_test]
async fn proxy_forbidden_request_has_cors_headers(env: TestEnvironment) {
let _server = SimpleAxumServer::new().await;

let mut proxy = MockProxy::new().await;
let port = proxy.port();
let url = format!("http://plane.test:{port}/abc123/");
Expand Down Expand Up @@ -71,3 +75,75 @@ async fn proxy_forbidden_request_has_cors_headers(env: TestEnvironment) {
"true"
);
}

#[plane_test]
async fn proxy_valid_request_has_cors_headers(env: TestEnvironment) {
let server = SimpleAxumServer::new().await;
let mut proxy = MockProxy::new().await;
let cluster = ClusterName::from_str(&format!("plane.test:{}", proxy.port())).unwrap();
let port = proxy.port();
let url = format!("http://plane.test:{port}/abc123/");
let client = localhost_client();

let handle = tokio::spawn(client.get(url).send());

let route_info_request = proxy.recv_route_info_request().await;
assert_eq!(
route_info_request.token,
BearerToken::from("abc123".to_string())
);

proxy
.send_route_info_response(RouteInfoResponse {
token: BearerToken::from("abc123".to_string()),
route_info: Some(RouteInfo {
backend_id: BackendName::new_random(),
address: BackendAddr(server.addr()),
secret_token: SecretToken::from("secret".to_string()),
cluster,
user: None,
user_data: None,
subdomain: None,
}),
})
.await;

let response = handle.await.unwrap().unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response
.headers()
.get("access-control-allow-origin")
.unwrap()
.to_str()
.unwrap(),
"*"
);
assert_eq!(
response
.headers()
.get("access-control-allow-methods")
.unwrap()
.to_str()
.unwrap(),
"*"
);
assert_eq!(
response
.headers()
.get("access-control-allow-headers")
.unwrap()
.to_str()
.unwrap(),
"*"
);
assert_eq!(
response
.headers()
.get("access-control-allow-credentials")
.unwrap()
.to_str()
.unwrap(),
"true"
);
}
4 changes: 3 additions & 1 deletion plane/src/proxy/proxy_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Service<Request<Incoming>> for ProxyState {

let request = request.into_request_with_simple_body();

let (res, upgrade_handler) = inner.proxy_client.request(request).await.unwrap();
let (mut res, upgrade_handler) = inner.proxy_client.request(request).await.unwrap();

Check warning on line 94 in plane/src/proxy/proxy_server.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

warning: used `unwrap()` on a `Result` value --> plane/src/proxy/proxy_server.rs:94:46 | 94 | let (mut res, upgrade_handler) = inner.proxy_client.request(request).await.unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used

if let Some(upgrade_handler) = upgrade_handler {
let monitor = inner.monitor.monitor();
Expand All @@ -111,6 +111,8 @@ impl Service<Request<Incoming>> for ProxyState {
inner.monitor.touch_backend(&route_info.backend_id);
}

apply_cors_headers(&mut res);

Ok(res)
})
}
Expand Down

0 comments on commit 6da1708

Please sign in to comment.