cloudevents/binding/axum/
response.rs

1use crate::binding::http::builder::adapter::to_response;
2use crate::event::Event;
3use axum::{body::Body, http::Response, response::IntoResponse};
4use axum_lib as axum;
5use http;
6use http::{header, StatusCode};
7
8impl IntoResponse for Event {
9    fn into_response(self) -> Response<Body> {
10        match to_response(self) {
11            Ok(resp) => {
12                let (parts, body) = resp.into_parts();
13                Response::from_parts(parts, Body::new(body))
14            }
15            Err(err) => Response::builder()
16                .status(StatusCode::INTERNAL_SERVER_ERROR)
17                .header(header::CONTENT_TYPE, "text/plain")
18                .body(Body::from(err.to_string()))
19                .unwrap(),
20        }
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    use crate::test::fixtures;
29
30    #[test]
31    fn axum_test_response() {
32        let input = fixtures::v10::minimal_string_extension();
33
34        let resp = input.into_response();
35
36        assert_eq!(
37            resp.headers()
38                .get("ce-specversion")
39                .unwrap()
40                .to_str()
41                .unwrap(),
42            "1.0"
43        );
44        assert_eq!(
45            resp.headers().get("ce-id").unwrap().to_str().unwrap(),
46            "0001"
47        );
48        assert_eq!(
49            resp.headers().get("ce-type").unwrap().to_str().unwrap(),
50            "test_event.test_application"
51        );
52        assert_eq!(
53            resp.headers().get("ce-source").unwrap().to_str().unwrap(),
54            "http://localhost/"
55        );
56        assert_eq!(
57            resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
58            "10"
59        );
60    }
61
62    #[tokio::test]
63    async fn axum_test_response_with_full_data() {
64        let input = fixtures::v10::full_binary_json_data_string_extension();
65
66        let resp = input.into_response();
67
68        assert_eq!(
69            resp.headers()
70                .get("ce-specversion")
71                .unwrap()
72                .to_str()
73                .unwrap(),
74            "1.0"
75        );
76        assert_eq!(
77            resp.headers().get("ce-id").unwrap().to_str().unwrap(),
78            "0001"
79        );
80        assert_eq!(
81            resp.headers().get("ce-type").unwrap().to_str().unwrap(),
82            "test_event.test_application"
83        );
84        assert_eq!(
85            resp.headers().get("ce-source").unwrap().to_str().unwrap(),
86            "http://localhost/"
87        );
88        assert_eq!(
89            resp.headers()
90                .get("content-type")
91                .unwrap()
92                .to_str()
93                .unwrap(),
94            "application/json"
95        );
96        assert_eq!(
97            resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
98            "10"
99        );
100
101        let (_, body) = resp.into_parts();
102        let body = axum::body::to_bytes(body, usize::MAX).await.unwrap();
103
104        assert_eq!(fixtures::json_data_binary(), body);
105    }
106}