rama_http/service/web/endpoint/extract/body/
mod.rs1use super::FromRequest;
4use rama_http_types as http;
5use rama_utils::macros::impl_deref;
6use std::convert::Infallible;
7
8mod bytes;
9#[doc(inline)]
10pub use bytes::*;
11
12mod text;
13#[doc(inline)]
14pub use text::*;
15
16mod json;
17#[doc(inline)]
18pub use json::*;
19
20mod csv;
21#[doc(inline)]
22pub use csv::*;
23
24mod form;
25#[doc(inline)]
26pub use form::*;
27
28#[derive(Debug)]
30pub struct Body(pub http::Body);
31
32impl_deref!(Body: http::Body);
33
34impl FromRequest for Body {
35 type Rejection = Infallible;
36
37 async fn from_request(req: http::Request) -> Result<Self, Self::Rejection> {
38 Ok(Self(req.into_body()))
39 }
40}
41
42#[cfg(test)]
43mod test {
44 use super::*;
45 use crate::dep::http_body_util::BodyExt;
46 use crate::service::web::WebService;
47 use crate::{Method, Request, StatusCode};
48 use rama_core::{Context, Service};
49
50 #[tokio::test]
51 async fn test_body() {
52 let service = WebService::default().get("/", async |Body(body): Body| {
53 let body = body.collect().await.unwrap().to_bytes();
54 assert_eq!(body, "test");
55 });
56
57 let req = Request::builder()
58 .method(Method::GET)
59 .body("test".into())
60 .unwrap();
61 let resp = service.serve(Context::default(), req).await.unwrap();
62 assert_eq!(resp.status(), StatusCode::OK);
63 }
64}