@@ -4,12 +4,20 @@ pub type Response = http_service::Response;
4
4
5
5
/// Serialize `t` into a JSON-encoded response.
6
6
pub fn json < T : serde:: Serialize > ( t : T ) -> Response {
7
- // TODO: remove the `unwrap`
8
- http:: Response :: builder ( )
9
- . status ( http:: status:: StatusCode :: OK )
10
- . header ( "Content-Type" , "application/json" )
11
- . body ( Body :: from ( serde_json:: to_vec ( & t) . unwrap ( ) ) )
12
- . unwrap ( )
7
+ let mut res = http:: Response :: builder ( ) ;
8
+ match serde_json:: to_vec ( & t) {
9
+ Ok ( v) => res
10
+ . header ( "Content-Type" , "application/json" )
11
+ . body ( Body :: from ( v) )
12
+ . unwrap ( )
13
+ ,
14
+ Err ( e) => {
15
+ log:: error!( "{}" , e) ;
16
+ res. status ( http:: status:: StatusCode :: INTERNAL_SERVER_ERROR )
17
+ . body ( Body :: empty ( ) )
18
+ . unwrap ( )
19
+ }
20
+ }
13
21
}
14
22
15
23
/// A value that is synchronously convertable into a `Response`.
@@ -118,24 +126,57 @@ impl<R: IntoResponse> IntoResponse for WithStatus<R> {
118
126
#[ cfg( test) ]
119
127
mod tests {
120
128
use super :: * ;
129
+ use futures:: executor:: block_on;
121
130
122
131
#[ test]
123
132
fn test_status ( ) {
124
133
let resp = "foo"
125
134
. with_status ( http:: status:: StatusCode :: NOT_FOUND )
126
135
. into_response ( ) ;
127
136
assert_eq ! ( resp. status( ) , http:: status:: StatusCode :: NOT_FOUND ) ;
137
+ assert_eq ! ( block_on( resp. into_body( ) . into_vec( ) ) . unwrap( ) , b"foo" ) ;
128
138
}
129
139
130
140
#[ test]
131
141
fn byte_vec_content_type ( ) {
132
142
let resp = String :: from ( "foo" ) . into_bytes ( ) . into_response ( ) ;
133
143
assert_eq ! ( resp. headers( ) [ "Content-Type" ] , "application/octet-stream" ) ;
144
+ assert_eq ! ( block_on( resp. into_body( ) . into_vec( ) ) . unwrap( ) , b"foo" ) ;
134
145
}
135
146
136
147
#[ test]
137
148
fn string_content_type ( ) {
138
149
let resp = String :: from ( "foo" ) . into_response ( ) ;
139
150
assert_eq ! ( resp. headers( ) [ "Content-Type" ] , "text/plain; charset=utf-8" ) ;
151
+ assert_eq ! ( block_on( resp. into_body( ) . into_vec( ) ) . unwrap( ) , b"foo" ) ;
152
+ }
153
+
154
+ #[ test]
155
+ fn json_content_type ( ) {
156
+ use std:: collections:: BTreeMap ;
157
+
158
+ let mut map = BTreeMap :: new ( ) ;
159
+ map. insert ( Some ( "a" ) , 2 ) ;
160
+ map. insert ( Some ( "b" ) , 4 ) ;
161
+ map. insert ( None , 6 ) ;
162
+
163
+ let resp = json ( map) ;
164
+ assert_eq ! (
165
+ resp. status( ) ,
166
+ http:: status:: StatusCode :: INTERNAL_SERVER_ERROR
167
+ ) ;
168
+ assert_eq ! ( block_on( resp. into_body( ) . into_vec( ) ) . unwrap( ) , b"" ) ;
169
+
170
+ let mut map = BTreeMap :: new ( ) ;
171
+ map. insert ( "a" , 2 ) ;
172
+ map. insert ( "b" , 4 ) ;
173
+ map. insert ( "c" , 6 ) ;
174
+
175
+ let resp = json ( map) ;
176
+ assert_eq ! ( resp. status( ) , http:: status:: StatusCode :: OK ) ;
177
+ assert_eq ! (
178
+ block_on( resp. into_body( ) . into_vec( ) ) . unwrap( ) ,
179
+ br##"{"a":2,"b":4,"c":6}"##
180
+ ) ;
140
181
}
141
182
}
0 commit comments