@@ -5,7 +5,7 @@ use std::ops::Not;
5
5
6
6
use http:: {
7
7
header:: { HeaderMap , HeaderValue } ,
8
- Response as HttpResponse ,
8
+ Response ,
9
9
} ;
10
10
use serde:: {
11
11
ser:: { Error as SerError , SerializeMap } ,
@@ -50,11 +50,11 @@ where
50
50
map. end ( )
51
51
}
52
52
53
- impl < T > From < HttpResponse < T > > for GatewayResponse
53
+ impl < T > From < Response < T > > for GatewayResponse
54
54
where
55
55
T : Into < Body > ,
56
56
{
57
- fn from ( value : HttpResponse < T > ) -> Self {
57
+ fn from ( value : Response < T > ) -> Self {
58
58
let ( parts, bod) = value. into_parts ( ) ;
59
59
let ( is_base64_encoded, body) = match bod. into ( ) {
60
60
Body :: Empty => ( false , None ) ,
@@ -70,11 +70,89 @@ where
70
70
}
71
71
}
72
72
73
+ /// A conversion of self into a `Response`
74
+ ///
75
+ /// Implementations for `Response<B> where B: Into<Body>`,
76
+ /// `B where B: Into<Body>` and `serde_json::Value` are provided
77
+ /// by default
78
+ ///
79
+ /// # example
80
+ ///
81
+ /// ```rust
82
+ /// use lambda_http::{Body, IntoResponse, Response};
83
+ ///
84
+ /// assert_eq!(
85
+ /// "hello".into_response().body(),
86
+ /// Response::new(Body::from("hello")).body()
87
+ /// );
88
+ /// ```
89
+ pub trait IntoResponse {
90
+ /// Return a translation of `self` into a `Response<Body>`
91
+ fn into_response ( self ) -> Response < Body > ;
92
+ }
93
+
94
+ impl < B > IntoResponse for Response < B >
95
+ where
96
+ B : Into < Body > ,
97
+ {
98
+ fn into_response ( self ) -> Response < Body > {
99
+ let ( parts, body) = self . into_parts ( ) ;
100
+ Response :: from_parts ( parts, body. into ( ) )
101
+ }
102
+ }
103
+
104
+ impl < B > IntoResponse for B
105
+ where
106
+ B : Into < Body > ,
107
+ {
108
+ fn into_response ( self ) -> Response < Body > {
109
+ Response :: new ( self . into ( ) )
110
+ }
111
+ }
112
+
113
+ impl IntoResponse for serde_json:: Value {
114
+ fn into_response ( self ) -> Response < Body > {
115
+ Response :: builder ( )
116
+ . header ( http:: header:: CONTENT_TYPE , "application/json" )
117
+ . body (
118
+ serde_json:: to_string ( & self )
119
+ . expect ( "unable to serialize serde_json::Value" )
120
+ . into ( ) ,
121
+ )
122
+ . expect ( "unable to build http::Response" )
123
+ }
124
+ }
125
+
73
126
#[ cfg( test) ]
74
127
mod tests {
75
128
76
- use super :: GatewayResponse ;
77
- use serde_json;
129
+ use super :: { Body , GatewayResponse , IntoResponse } ;
130
+ use serde_json:: { self , json} ;
131
+
132
+ #[ test]
133
+ fn json_into_response ( ) {
134
+ let response = json ! ( { "hello" : "lambda" } ) . into_response ( ) ;
135
+ match response. body ( ) {
136
+ Body :: Text ( json) => assert_eq ! ( json, r#"{"hello":"lambda"}"# ) ,
137
+ _ => panic ! ( "invalid body" ) ,
138
+ }
139
+ assert_eq ! (
140
+ response
141
+ . headers( )
142
+ . get( http:: header:: CONTENT_TYPE )
143
+ . map( |h| h. to_str( ) . expect( "invalid header" ) ) ,
144
+ Some ( "application/json" )
145
+ )
146
+ }
147
+
148
+ #[ test]
149
+ fn text_into_response ( ) {
150
+ let response = "text" . into_response ( ) ;
151
+ match response. body ( ) {
152
+ Body :: Text ( text) => assert_eq ! ( text, "text" ) ,
153
+ _ => panic ! ( "invalid body" ) ,
154
+ }
155
+ }
78
156
79
157
#[ test]
80
158
fn default_response ( ) {
0 commit comments