1
0
mirror of https://github.com/pcvolkmer/mv64e-rest-to-kafka-gateway synced 2025-09-13 09:12:51 +00:00

feat: accept custom media type application/vnd.dnpm.v2.mtb+json

This commit is contained in:
2025-07-12 10:29:31 +02:00
parent 523ad89783
commit ea72454a79
3 changed files with 69 additions and 8 deletions

View File

@@ -68,9 +68,7 @@ async fn main() -> Result<(), ()> {
&CONFIG.bootstrap_server,
)?);
let routes = routes(sender)
.layer(from_fn(check_content_type_header))
.layer(from_fn(check_basic_auth));
let routes = routes(sender).layer(from_fn(check_basic_auth));
#[cfg(debug_assertions)]
let routes = routes.layer(TraceLayer::new_for_http());
@@ -94,7 +92,11 @@ async fn check_content_type_header(request: Request<Body>, next: Next) -> Respon
.get(CONTENT_TYPE)
.map(HeaderValue::as_bytes)
{
Some(b"application/json" | b"application/json; charset=utf-8") => next.run(request).await,
Some(
b"application/json"
| b"application/json; charset=utf-8"
| b"application/vnd.dnpm.v2.mtb+json",
) => next.run(request).await,
_ => UnsupportedContentType.into_response(),
}
}

View File

@@ -1,6 +1,8 @@
use crate::check_content_type_header;
use crate::sender::DynMtbFileSender;
use crate::AppResponse::{Accepted, InternalServerError};
use axum::extract::Path;
use axum::middleware::from_fn;
use axum::response::{IntoResponse, Response};
use axum::routing::{delete, post};
use axum::{Extension, Json, Router};
@@ -35,6 +37,7 @@ pub fn routes(sender: DynMtbFileSender) -> Router {
delete(handle_delete),
)
.layer(Extension(sender))
.layer(from_fn(check_content_type_header))
}
#[cfg(test)]
@@ -104,4 +107,60 @@ mod tests {
assert_eq!(response.status(), StatusCode::ACCEPTED);
}
#[tokio::test]
#[allow(clippy::expect_used)]
async fn should_handle_post_request_with_custom_v2_media_type() {
let mut sender_mock = MockMtbFileSender::new();
sender_mock
.expect_send()
.withf(|mtb| mtb.patient.id.eq("fae56ea7-24a7-4556-82fb-2b5dde71bb4d"))
.return_once(move |_| Ok(String::new()));
let router = routes(Arc::new(sender_mock) as DynMtbFileSender);
let body = Body::from(include_str!("../test-files/mv64e-mtb-fake-patient.json"));
let response = router
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/mtb/etl/patient-record")
.header(CONTENT_TYPE, "application/vnd.dnpm.v2.mtb+json")
.body(body)
.expect("request built"),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::ACCEPTED);
}
#[tokio::test]
#[allow(clippy::expect_used)]
async fn should_not_accept_xml_request() {
let mut sender_mock = MockMtbFileSender::new();
sender_mock
.expect_send()
.withf(|mtb| mtb.patient.id.eq("fae56ea7-24a7-4556-82fb-2b5dde71bb4d"))
.return_once(move |_| Ok(String::new()));
let router = routes(Arc::new(sender_mock) as DynMtbFileSender);
let body = Body::from("<test>Das ist ein Test</test>");
let response = router
.oneshot(
Request::builder()
.method(Method::POST)
.uri("/mtb/etl/patient-record")
.header(CONTENT_TYPE, "application/xml")
.body(body)
.expect("request built"),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
}
}