diff --git a/aries/agents/mediator/src/http_routes/mod.rs b/aries/agents/mediator/src/http_routes/mod.rs index 0b54bc0b6f..e88cbd611a 100644 --- a/aries/agents/mediator/src/http_routes/mod.rs +++ b/aries/agents/mediator/src/http_routes/mod.rs @@ -9,6 +9,7 @@ use axum::{ routing::get, Json, Router, }; +use serde::{Deserialize, Serialize}; use serde_json::Value; use crate::{ @@ -22,11 +23,7 @@ pub async fn oob_invite_qr( State(agent): State>, ) -> Response { let Json(oob_json) = oob_invite_json(State(agent)).await; - let preferred_mimetype = headers - .get(ACCEPT) - .map(|s| s.to_str().unwrap_or_default()) - .unwrap_or_default(); - match preferred_mimetype { + match detect_mime_type(&headers) { "application/json" => Json(oob_json).into_response(), _ => { let oob_string = serde_json::to_string_pretty(&oob_json).unwrap(); @@ -47,6 +44,13 @@ pub async fn oob_invite_qr( } } +fn detect_mime_type(headers: &HeaderMap) -> &str { + headers + .get(ACCEPT) + .map(|s| s.to_str().unwrap_or_default()) + .unwrap_or_default() +} + pub async fn oob_invite_json( State(agent): State>, ) -> Json { @@ -61,8 +65,23 @@ pub async fn handle_didcomm( didcomm_handlers::handle_aries(State(agent), didcomm_msg).await } -pub async fn readme() -> Html { - Html("

Please refer to the API section of readme for usage. Thanks.

".into()) +#[derive(Serialize, Deserialize)] +pub struct ReadmeInfo { + message: String, +} + +pub async fn readme(headers: HeaderMap) -> Response { + match detect_mime_type(&headers) { + "application/json" => Json(ReadmeInfo { + message: "Please refer to the API section of a readme for usage. Thanks.".into(), + }) + .into_response(), + _ => Html( + "

Please refer to the API section of readme for usage. Thanks.

" + .to_string(), + ) + .into_response(), + } } pub async fn build_router( diff --git a/aries/agents/mediator/tests/mediator-readme.rs b/aries/agents/mediator/tests/mediator-readme.rs new file mode 100644 index 0000000000..9d571be32e --- /dev/null +++ b/aries/agents/mediator/tests/mediator-readme.rs @@ -0,0 +1,32 @@ +mod common; + +use anyhow::Result; +use log::info; +use mediator::http_routes::ReadmeInfo; +use reqwest::header::ACCEPT; +use url::Url; + +use crate::common::test_setup::setup_env_logging; + +static LOGGING_INIT: std::sync::Once = std::sync::Once::new(); + +const ENDPOINT_ROOT: &str = "http://localhost:8005"; + +#[test] +fn base_path_returns_readme() -> Result<()> { + LOGGING_INIT.call_once(setup_env_logging); + + let client = reqwest::blocking::Client::new(); + let endpoint: Url = ENDPOINT_ROOT.parse().unwrap(); + + let res = client + .get(endpoint) + .header(ACCEPT, "application/json") + .send()? + .error_for_status()?; + info!("{:?}", res); + + let _: ReadmeInfo = res.json()?; + + Ok(()) +}