@@ -11,7 +11,7 @@ use aws_lambda_events::encodings::Body;
11
11
use encoding_rs:: Encoding ;
12
12
use http:: header:: CONTENT_ENCODING ;
13
13
use http:: HeaderMap ;
14
- use http:: { header:: CONTENT_TYPE , Response } ;
14
+ use http:: { header:: CONTENT_TYPE , Response , StatusCode } ;
15
15
use http_body:: Body as HttpBody ;
16
16
use hyper:: body:: to_bytes;
17
17
use mime:: { Mime , CHARSET } ;
@@ -179,6 +179,71 @@ impl IntoResponse for serde_json::Value {
179
179
}
180
180
}
181
181
182
+ impl IntoResponse for ( StatusCode , String ) {
183
+ fn into_response ( self ) -> ResponseFuture {
184
+ let ( status, body) = self ;
185
+ Box :: pin ( ready (
186
+ Response :: builder ( )
187
+ . status ( status)
188
+ . body ( Body :: from ( body) )
189
+ . expect ( "unable to build http::Response" ) ,
190
+ ) )
191
+ }
192
+ }
193
+
194
+ impl IntoResponse for ( StatusCode , & str ) {
195
+ fn into_response ( self ) -> ResponseFuture {
196
+ let ( status, body) = self ;
197
+ Box :: pin ( ready (
198
+ Response :: builder ( )
199
+ . status ( status)
200
+ . body ( Body :: from ( body) )
201
+ . expect ( "unable to build http::Response" ) ,
202
+ ) )
203
+ }
204
+ }
205
+
206
+ impl IntoResponse for ( StatusCode , & [ u8 ] ) {
207
+ fn into_response ( self ) -> ResponseFuture {
208
+ let ( status, body) = self ;
209
+ Box :: pin ( ready (
210
+ Response :: builder ( )
211
+ . status ( status)
212
+ . body ( Body :: from ( body) )
213
+ . expect ( "unable to build http::Response" ) ,
214
+ ) )
215
+ }
216
+ }
217
+
218
+ impl IntoResponse for ( StatusCode , Vec < u8 > ) {
219
+ fn into_response ( self ) -> ResponseFuture {
220
+ let ( status, body) = self ;
221
+ Box :: pin ( ready (
222
+ Response :: builder ( )
223
+ . status ( status)
224
+ . body ( Body :: from ( body) )
225
+ . expect ( "unable to build http::Response" ) ,
226
+ ) )
227
+ }
228
+ }
229
+
230
+ impl IntoResponse for ( StatusCode , serde_json:: Value ) {
231
+ fn into_response ( self ) -> ResponseFuture {
232
+ let ( status, body) = self ;
233
+ Box :: pin ( async move {
234
+ Response :: builder ( )
235
+ . status ( status)
236
+ . header ( CONTENT_TYPE , "application/json" )
237
+ . body (
238
+ serde_json:: to_string ( & body)
239
+ . expect ( "unable to serialize serde_json::Value" )
240
+ . into ( ) ,
241
+ )
242
+ . expect ( "unable to build http::Response" )
243
+ } )
244
+ }
245
+ }
246
+
182
247
pub type ResponseFuture = Pin < Box < dyn Future < Output = Response < Body > > + Send > > ;
183
248
184
249
pub trait ConvertBody {
@@ -269,7 +334,7 @@ mod tests {
269
334
use super :: { Body , IntoResponse , LambdaResponse , RequestOrigin , X_LAMBDA_HTTP_CONTENT_ENCODING } ;
270
335
use http:: {
271
336
header:: { CONTENT_ENCODING , CONTENT_TYPE } ,
272
- Response ,
337
+ Response , StatusCode ,
273
338
} ;
274
339
use hyper:: Body as HyperBody ;
275
340
use serde_json:: { self , json} ;
@@ -310,6 +375,54 @@ mod tests {
310
375
}
311
376
}
312
377
378
+ #[ tokio:: test]
379
+ async fn json_with_status_code_into_response ( ) {
380
+ let response = ( StatusCode :: CREATED , json ! ( { "hello" : "lambda" } ) ) . into_response ( ) . await ;
381
+ match response. body ( ) {
382
+ Body :: Text ( json) => assert_eq ! ( json, r#"{"hello":"lambda"}"# ) ,
383
+ _ => panic ! ( "invalid body" ) ,
384
+ }
385
+ match response. status ( ) {
386
+ StatusCode :: CREATED => ( ) ,
387
+ _ => panic ! ( "invalid status code" ) ,
388
+ }
389
+
390
+ assert_eq ! (
391
+ response
392
+ . headers( )
393
+ . get( CONTENT_TYPE )
394
+ . map( |h| h. to_str( ) . expect( "invalid header" ) ) ,
395
+ Some ( "application/json" )
396
+ )
397
+ }
398
+
399
+ #[ tokio:: test]
400
+ async fn text_with_status_code_into_response ( ) {
401
+ let response = ( StatusCode :: CREATED , "text" ) . into_response ( ) . await ;
402
+
403
+ match response. status ( ) {
404
+ StatusCode :: CREATED => ( ) ,
405
+ _ => panic ! ( "invalid status code" ) ,
406
+ }
407
+ match response. body ( ) {
408
+ Body :: Text ( text) => assert_eq ! ( text, "text" ) ,
409
+ _ => panic ! ( "invalid body" ) ,
410
+ }
411
+ }
412
+
413
+ #[ tokio:: test]
414
+ async fn bytes_with_status_code_into_response ( ) {
415
+ let response = ( StatusCode :: CREATED , "text" . as_bytes ( ) ) . into_response ( ) . await ;
416
+ match response. status ( ) {
417
+ StatusCode :: CREATED => ( ) ,
418
+ _ => panic ! ( "invalid status code" ) ,
419
+ }
420
+ match response. body ( ) {
421
+ Body :: Binary ( data) => assert_eq ! ( data, "text" . as_bytes( ) ) ,
422
+ _ => panic ! ( "invalid body" ) ,
423
+ }
424
+ }
425
+
313
426
#[ tokio:: test]
314
427
async fn content_encoding_header ( ) {
315
428
// Drive the implementation by using `hyper::Body` instead of
0 commit comments