cloudevents/binding/warp/
reply.rs1use warp_lib as warp;
2
3use crate::binding::http_0_2::builder::adapter::to_response;
4
5use crate::Event;
6use http::StatusCode;
7use http_0_2 as http;
8use hyper_0_14 as hyper;
9use warp::reply::Response;
10pub fn from_event(event: Event) -> Response {
24 match to_response(event) {
25 Ok(response) => response,
26 Err(e) => warp::http::response::Response::builder()
27 .status(StatusCode::INTERNAL_SERVER_ERROR)
28 .body(hyper::body::Body::from(e.to_string()))
29 .unwrap(),
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use crate::test::fixtures;
36 use hyper_0_14 as hyper;
37
38 #[test]
39 fn test_response() {
40 let input = fixtures::v10::minimal_string_extension();
41
42 let resp = super::from_event(input);
43
44 assert_eq!(
45 resp.headers()
46 .get("ce-specversion")
47 .unwrap()
48 .to_str()
49 .unwrap(),
50 "1.0"
51 );
52 assert_eq!(
53 resp.headers().get("ce-id").unwrap().to_str().unwrap(),
54 "0001"
55 );
56 assert_eq!(
57 resp.headers().get("ce-type").unwrap().to_str().unwrap(),
58 "test_event.test_application"
59 );
60 assert_eq!(
61 resp.headers().get("ce-source").unwrap().to_str().unwrap(),
62 "http://localhost/"
63 );
64 assert_eq!(
65 resp.headers().get("ce-someint").unwrap().to_str().unwrap(),
66 "10"
67 );
68 }
69
70 #[tokio::test]
71 async fn test_response_with_full_data() {
72 let input = fixtures::v10::full_binary_json_data_string_extension();
73
74 let resp = super::from_event(input);
75
76 assert_eq!(
77 resp.headers()
78 .get("ce-specversion")
79 .unwrap()
80 .to_str()
81 .unwrap(),
82 "1.0"
83 );
84 assert_eq!(
85 resp.headers().get("ce-id").unwrap().to_str().unwrap(),
86 "0001"
87 );
88 assert_eq!(
89 resp.headers().get("ce-type").unwrap().to_str().unwrap(),
90 "test_event.test_application"
91 );
92 assert_eq!(
93 resp.headers().get("ce-source").unwrap().to_str().unwrap(),
94 "http://localhost/"
95 );
96 assert_eq!(
97 resp.headers()
98 .get("content-type")
99 .unwrap()
100 .to_str()
101 .unwrap(),
102 "application/json"
103 );
104 assert_eq!(
105 resp.headers().get("ce-int_ex").unwrap().to_str().unwrap(),
106 "10"
107 );
108
109 let (_, body) = resp.into_parts();
110 let body = hyper::body::to_bytes(body).await.unwrap();
111
112 assert_eq!(fixtures::json_data_binary(), body);
113 }
114}