1
1
use axum_lib as axum;
2
2
3
3
use async_trait:: async_trait;
4
- use axum:: extract:: { FromRequest , RequestParts } ;
4
+ use axum:: extract:: FromRequest ;
5
+ use axum:: http:: Request ;
6
+ use http:: request:: Parts ;
5
7
use http:: StatusCode ;
6
8
use http_body:: Body ;
7
9
use hyper:: body;
@@ -12,26 +14,23 @@ use crate::event::Event;
12
14
type BoxError = Box < dyn std:: error:: Error + Send + Sync > ;
13
15
14
16
#[ async_trait]
15
- impl < B > FromRequest < B > for Event
17
+ impl < S , B > FromRequest < S , B > for Event
16
18
where
17
- B : Body + Send ,
19
+ B : Body + Send + ' static ,
18
20
B :: Data : Send ,
19
21
B :: Error : Into < BoxError > ,
22
+ S : Send + Sync ,
20
23
{
21
24
type Rejection = ( StatusCode , String ) ;
22
25
23
- async fn from_request ( req : & mut RequestParts < B > ) -> Result < Self , Self :: Rejection > {
24
- let req_body = req
25
- . take_body ( )
26
- . ok_or ( 0 )
27
- . map_err ( |_| ( StatusCode :: BAD_REQUEST , "unexpected empty body" . to_string ( ) ) ) ?;
26
+ async fn from_request ( req : Request < B > , _state : & S ) -> Result < Self , Self :: Rejection > {
27
+ let ( Parts { headers, .. } , req_body) = req. into_parts ( ) ;
28
28
let buf = body:: to_bytes ( req_body)
29
29
. await
30
30
. map_err ( |e| ( StatusCode :: BAD_REQUEST , format ! ( "{}" , e. into( ) ) ) ) ?
31
31
. to_vec ( ) ;
32
- let headers = req. headers ( ) ;
33
32
34
- to_event ( headers, buf) . map_err ( |e| ( StatusCode :: BAD_REQUEST , format ! ( "{}" , e) ) )
33
+ to_event ( & headers, buf) . map_err ( |e| ( StatusCode :: BAD_REQUEST , format ! ( "{}" , e) ) )
35
34
}
36
35
}
37
36
@@ -49,39 +48,35 @@ mod tests {
49
48
async fn axum_test_request ( ) {
50
49
let expected = fixtures:: v10:: minimal_string_extension ( ) ;
51
50
52
- let mut request = RequestParts :: new (
53
- Request :: builder ( )
54
- . method ( http:: Method :: POST )
55
- . header ( "ce-specversion" , "1.0" )
56
- . header ( "ce-id" , "0001" )
57
- . header ( "ce-type" , "test_event.test_application" )
58
- . header ( "ce-source" , "http://localhost/" )
59
- . header ( "ce-someint" , "10" )
60
- . body ( Body :: empty ( ) )
61
- . unwrap ( ) ,
62
- ) ;
51
+ let mut request = Request :: builder ( )
52
+ . method ( http:: Method :: POST )
53
+ . header ( "ce-specversion" , "1.0" )
54
+ . header ( "ce-id" , "0001" )
55
+ . header ( "ce-type" , "test_event.test_application" )
56
+ . header ( "ce-source" , "http://localhost/" )
57
+ . header ( "ce-someint" , "10" )
58
+ . body ( Body :: empty ( ) )
59
+ . unwrap ( ) ;
63
60
64
- let result = Event :: from_request ( & mut request) . await . unwrap ( ) ;
61
+ let result = Event :: from_request ( request, & ( ) ) . await . unwrap ( ) ;
65
62
66
63
assert_eq ! ( expected, result) ;
67
64
}
68
65
69
66
#[ tokio:: test]
70
67
async fn axum_test_bad_request ( ) {
71
- let mut request = RequestParts :: new (
72
- Request :: builder ( )
73
- . method ( http:: Method :: POST )
74
- . header ( "ce-specversion" , "BAD SPECIFICATION" )
75
- . header ( "ce-id" , "0001" )
76
- . header ( "ce-type" , "example.test" )
77
- . header ( "ce-source" , "http://localhost/" )
78
- . header ( "ce-someint" , "10" )
79
- . header ( "ce-time" , fixtures:: time ( ) . to_rfc3339 ( ) )
80
- . body ( Body :: empty ( ) )
81
- . unwrap ( ) ,
82
- ) ;
83
-
84
- let result = Event :: from_request ( & mut request) . await ;
68
+ let mut request = Request :: builder ( )
69
+ . method ( http:: Method :: POST )
70
+ . header ( "ce-specversion" , "BAD SPECIFICATION" )
71
+ . header ( "ce-id" , "0001" )
72
+ . header ( "ce-type" , "example.test" )
73
+ . header ( "ce-source" , "http://localhost/" )
74
+ . header ( "ce-someint" , "10" )
75
+ . header ( "ce-time" , fixtures:: time ( ) . to_rfc3339 ( ) )
76
+ . body ( Body :: empty ( ) )
77
+ . unwrap ( ) ;
78
+
79
+ let result = Event :: from_request ( request, & ( ) ) . await ;
85
80
assert ! ( result. is_err( ) ) ;
86
81
let rejection = result. unwrap_err ( ) ;
87
82
@@ -93,24 +88,22 @@ mod tests {
93
88
async fn axum_test_request_with_full_data ( ) {
94
89
let expected = fixtures:: v10:: full_binary_json_data_string_extension ( ) ;
95
90
96
- let mut request = RequestParts :: new (
97
- Request :: builder ( )
98
- . method ( http:: Method :: POST )
99
- . header ( "ce-specversion" , "1.0" )
100
- . header ( "ce-id" , "0001" )
101
- . header ( "ce-type" , "test_event.test_application" )
102
- . header ( "ce-source" , "http://localhost/" )
103
- . header ( "ce-subject" , "cloudevents-sdk" )
104
- . header ( "content-type" , "application/json" )
105
- . header ( "ce-string_ex" , "val" )
106
- . header ( "ce-int_ex" , "10" )
107
- . header ( "ce-bool_ex" , "true" )
108
- . header ( "ce-time" , & fixtures:: time ( ) . to_rfc3339 ( ) )
109
- . body ( Body :: from ( fixtures:: json_data_binary ( ) ) )
110
- . unwrap ( ) ,
111
- ) ;
112
-
113
- let result = Event :: from_request ( & mut request) . await . unwrap ( ) ;
91
+ let mut request = Request :: builder ( )
92
+ . method ( http:: Method :: POST )
93
+ . header ( "ce-specversion" , "1.0" )
94
+ . header ( "ce-id" , "0001" )
95
+ . header ( "ce-type" , "test_event.test_application" )
96
+ . header ( "ce-source" , "http://localhost/" )
97
+ . header ( "ce-subject" , "cloudevents-sdk" )
98
+ . header ( "content-type" , "application/json" )
99
+ . header ( "ce-string_ex" , "val" )
100
+ . header ( "ce-int_ex" , "10" )
101
+ . header ( "ce-bool_ex" , "true" )
102
+ . header ( "ce-time" , & fixtures:: time ( ) . to_rfc3339 ( ) )
103
+ . body ( Body :: from ( fixtures:: json_data_binary ( ) ) )
104
+ . unwrap ( ) ;
105
+
106
+ let result = Event :: from_request ( request, & ( ) ) . await . unwrap ( ) ;
114
107
115
108
assert_eq ! ( expected, result) ;
116
109
}
0 commit comments