Skip to content

Commit 8d7c0eb

Browse files
authored
Added IntoResponse implementation for (StatusCode, IntoResponse) (#573)
1 parent 695340b commit 8d7c0eb

File tree

1 file changed

+115
-2
lines changed

1 file changed

+115
-2
lines changed

lambda-http/src/response.rs

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use aws_lambda_events::encodings::Body;
1111
use encoding_rs::Encoding;
1212
use http::header::CONTENT_ENCODING;
1313
use http::HeaderMap;
14-
use http::{header::CONTENT_TYPE, Response};
14+
use http::{header::CONTENT_TYPE, Response, StatusCode};
1515
use http_body::Body as HttpBody;
1616
use hyper::body::to_bytes;
1717
use mime::{Mime, CHARSET};
@@ -179,6 +179,71 @@ impl IntoResponse for serde_json::Value {
179179
}
180180
}
181181

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+
182247
pub type ResponseFuture = Pin<Box<dyn Future<Output = Response<Body>> + Send>>;
183248

184249
pub trait ConvertBody {
@@ -269,7 +334,7 @@ mod tests {
269334
use super::{Body, IntoResponse, LambdaResponse, RequestOrigin, X_LAMBDA_HTTP_CONTENT_ENCODING};
270335
use http::{
271336
header::{CONTENT_ENCODING, CONTENT_TYPE},
272-
Response,
337+
Response, StatusCode,
273338
};
274339
use hyper::Body as HyperBody;
275340
use serde_json::{self, json};
@@ -310,6 +375,54 @@ mod tests {
310375
}
311376
}
312377

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+
313426
#[tokio::test]
314427
async fn content_encoding_header() {
315428
// Drive the implementation by using `hyper::Body` instead of

0 commit comments

Comments
 (0)