use axum::Json;
use super::auth::HttpCaller;
use crate::build_identity::BuildIdentity;
pub(crate) async fn build_identity(HttpCaller(_caller): HttpCaller) -> Json<BuildIdentity> {
Json(BuildIdentity::current())
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use aion::EngineBuilder;
use aion_store::{EventStore, InMemoryStore};
use axum::{body, http::Request, http::StatusCode};
use serde_json::Value;
use tower::ServiceExt;
use super::super::router::workflow_router;
use super::super::test_support::{runtime_config, server_state};
use crate::{
NamespaceResolver, StaticScheduleNamespaces, StaticWorkflowNamespaces,
config::NamespaceMode,
};
async fn router() -> Result<axum::Router, Box<dyn std::error::Error>> {
let store: Arc<dyn EventStore> = Arc::new(InMemoryStore::default());
let engine = Arc::new(
EngineBuilder::new()
.store_arc(store)
.in_memory_visibility()
.scheduler_threads(1)
.build()
.await?,
);
let resolver = NamespaceResolver::from_parts(
NamespaceMode::SharedEngine,
Some(engine),
Arc::new(StaticWorkflowNamespaces::default()),
Arc::new(StaticScheduleNamespaces::default()),
);
let mut config = runtime_config();
config.auth.enabled = false;
Ok(workflow_router(server_state(resolver, config).await?))
}
#[tokio::test]
async fn the_server_can_say_what_code_it_is() -> Result<(), Box<dyn std::error::Error>> {
let response = router()
.await?
.oneshot(Request::builder().uri("/build").body(body::Body::empty())?)
.await?;
assert_eq!(response.status(), StatusCode::OK);
let content_type = response
.headers()
.get(axum::http::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_owned();
assert!(
content_type.starts_with("application/json"),
"the SPA catch-all answers text/html for an absent route, so a probe that \
cannot see the content-type cannot tell this endpoint from its absence; got `{content_type}`"
);
let bytes = body::to_bytes(response.into_body(), usize::MAX).await?;
let body: Value = serde_json::from_slice(&bytes)?;
let identity = crate::build_identity::BuildIdentity::current();
assert_eq!(body["version"], serde_json::json!(identity.version));
assert_eq!(body["commit"], serde_json::json!(identity.commit));
assert_eq!(body["dirty"], serde_json::json!(identity.dirty));
assert_eq!(body["built_at"], serde_json::json!(identity.built_at));
Ok(())
}
#[tokio::test]
async fn a_path_this_router_does_not_serve_is_a_404() -> Result<(), Box<dyn std::error::Error>>
{
let response = router()
.await?
.oneshot(
Request::builder()
.uri("/build-that-does-not-exist")
.body(body::Body::empty())?,
)
.await?;
assert_eq!(
response.status(),
StatusCode::NOT_FOUND,
"if this router answered every path, the sibling test would prove nothing"
);
Ok(())
}
}