Generate typed cluster service clients and Axum routes from one contract

This commit is contained in:
2026-12-02 21:26:01 +08:00
parent 83a30fad4a
commit 46b85a9d47
9 changed files with 539 additions and 27 deletions
+45
View File
@@ -0,0 +1,45 @@
use std::{sync::Arc, time::Duration};
use admin_backend::rpc::{Client, StatusRequest};
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use bedrock_rpc::{Error, HttpTransport, Transport};
use clusters::DeploymentConfig;
#[tokio::test]
async fn status_rpc_uses_the_admin_listener_and_its_authentication() {
let config = DeploymentConfig::default().resolve().unwrap();
let app =
admin_backend::router(Arc::new(config), Some("release-7".into()), "test".into()).unwrap();
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
.await
.unwrap();
let origin = format!("http://{}", listener.local_addr().unwrap());
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let peer = |headers| HttpTransport::new(&origin, Duration::from_secs(3), headers).unwrap();
let anonymous = Client(peer(HeaderMap::new()));
assert!(matches!(
anonymous.status(&StatusRequest {}).await,
Err(Error::Status(StatusCode::UNAUTHORIZED))
));
let mut headers = HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
HeaderValue::from_static("Bearer test"),
);
let client = Client(peer(headers));
let status = client.status(&StatusRequest {}).await.unwrap();
assert_eq!(status.config_version.as_deref(), Some("release-7"));
assert_eq!(status.deployment, "default");
let malformed = client
.0
.post("/admin/v1/deployment/status", b"{".to_vec())
.await;
assert!(matches!(
malformed,
Err(Error::Status(StatusCode::BAD_REQUEST))
));
server.abort();
}